- 0shares
- Facebook0
- Twitter0
- Google+0
- Pinterest0
- LinkedIn0

## Operators in Java

An operator is used to perform certain operation on values or variables. In other words operators are the symbols that are used to perform certain operations on data. In Java there are many kinds of operators to perform operations. The most common operators that are used in Java are listed below:

- Arithmetic operator
- Relational operator
- Logical operators
- Assignment operator
- Compound Assignment operator
- Unary operator
- Conditional or Ternary operator
- bitwise operator
- Shift operator

These operators are described below:

### 1. Arithmetic operator:

Arithmetic operator is a symbol that performs mathematical operation on data. The Java programming language provides many arithmetic operators. Following is a list of all operators used in Java:

Operation |
Symbol |
Description |

Addition | + | Adds two values |

Subtraction | – | Subtracts one value from other |

Multiplication | * | Multiplies two values |

Division | / | Divides one values by other |

Modulus | % | Gives the remainder of division of two integers |

### 2. Relational operator:

The relational operators are used to specify conditions in programs. A relational operator compares two values. It produces result as true or false. The relational operators are sometimes called comparison operators as they test that are either true or false.

Java provides the following six basic relational operators:

Operator |
Description |

> | Greater than operator returns false if the value on right side of > is less than the value on the left side. Otherwise returns true. |

< | Less than operator returns true if the value on left side of < is less than the value on right side. Otherwise returns false. |

== | Equal to operator returns true if the values on both side of == are equal. Otherwise returns false. |

>= | Greater than or equal to returns false if value on the right side of >= is less than the value on the left side. Otherwise returns true. |

<= | Less than or equal to returns true if value on the left side of <= is less than the value on the right side. Otherwise returns false. |

!= | The not equal to operator returns true if the value on the left side of != is not equal to the value on the right side. Otherwise returns false. |

### 3. Logical operator:

The logical operators are used to evaluate compound conditions. There are three logical operators in Java programming language:

- AND operator (&&)
- OR operator (||)
- NOT operator (!)

The symbol for AND operator is (&&). It is used to evaluate two conditions. It produces true result if both conditions are true. It produces false result if any condition is false. Consider the following table:

Condition 1 |
Operator |
Condition 2 |
Result |

False | && | False | False |

False | && | True | False |

True | && | False | False |

True | && | True | True |

The symbol used for OR operator is ( | | ). It is used to evaluate two conditions. It gives true result if either condition is true. It gives false if either condition is false. Consider the following table:

Condition 1 |
Operator |
Condition 2 |
Result |

False | && | False | False |

False | && | True | True |

True | && | False | True |

True | && | True | True |

The symbol for not operator is ( ! ). It is used to reverse the result of a condition. Consider the following table:

Operator |
Condition |
Result |

! | True | False |

! | False | True |

### 4. Assignment operator:

The assignment (=) is used in assignment statement to assign a value or computational result to a variable.

The name of the variable is written on the left side of the operator and the value is written on the right side of the operator. A statement that assigns a value to a variable is called assignment statement.

Variable = expression;

A = 100;

C = A + B;

### 5. Compound assignment operator:

The Java programming language provides compound assignment operators that combine assignment operator with arithmetic operators. Compound assignment operators are used to perform mathematical operations more easily.

Variable op = expression

N += 10 which is equivalent to N = N + 10 and it will add the value 10 to variable n and will store the result into the variable n.

There are various other compound assignment operators. Consider the following table in which other compound assignment operators are described:

Operator |
Description |
Equivalent to |

+=
A+=B |
This operator will add the operand on the right side to the operand on the left side and will store the result into the operand or variable at the left side. It is also called add AND assignment operator. | A = A+B |

-=
A-=B |
This operator will subtract the operand on the right side from the operand on the left side and will store the result into the operand or variable at the left side. It is also called subtract AND assignment operator. | A = A-B |

*=
A*=B |
This operator will multiply the operand on the right side with the operand on the left side and will store the result into the operand or variable at the left side. It is also called multiply AND assignment operator. | A = A*B |

/=
A/=B |
This operator will divide the operand on the left side with the operand on the right side and will store the result into the operand or variable at the left side. It is also called divide AND assignment operator. | A = A/B |

%=
A%=B |
This operator will take the modulus of the two operands and will store the result into the operand or variable at the left side. It is also called modulus AND assignment operator. | A = A%B |

### 6. Unary Operator:

### Increment and Decrement operator:

The increment operator is used to increment the value by 1. It is denoted by the symbol ++. It is a unary operator and works with single variable. Increment operator can be used in two forms.

**Prefix form:**

The increment operator is written before the variable as ++y.

**Postfix form:**

The increment operator is written after the variable as y++.

The decrement operator is used to decrease the value by 1. It is denoted by the symbol –. It is a unary operator and works with single variable. Decrement operator can be used in two forms.

**Prefix form:**

The decrement operator is written before the variable as –y.

**Postfix form:**

The decrement operator is written after the variable as y–.

### 7. Conditional Operator or ternary Operator:

Conditional operator is a decision making structure. It can be used in place of simple if else structure. It is also called ternary operator as it uses three operands.

The syntax of conditional operator is as follows:

(condition) ? true case statement: false case statement;

**Condition:** is specified as relational or logical expression. The condition is evaluated as true or false.

**True case:** it is executed if expression evaluates to true.

**False case:** it is executed if expression evaluates to false.

Suppose we have a variable A. the following statement:

X = (A>50) ? 1 : 0;

Will assign 1 to X if the condition A>50 is true. It will assign 0 to X if the condition is false. The above statement can be written using if else statement as follows:

If (A>50)

X=1;

Else

X=0;

### 8. Bitwise Operator:

There are some bitwise operators that are supported by Java. These operators can be applied to integer data type, long, short, char and byte. The bitwise operator performs the bit by bit operation and it works on bits.

Suppose that you have two variables ‘a’ and ‘b’:

a = 0011 1100

b = 0000 1101

Then expression (a & b) will be equal to 0000 1100. The bitwise AND operator results a 1 if the bits in both of the operands are 1, and 0 if the bits in the operands are different or 0.

Suppose that you have two variables ‘a’ and ‘b’:

a = 0011 1100

b = 0000 1101

Then the expression (a | b) will be equal to 0011 1101. The bitwise OR operator results a 1 if one of the bits in the operands is 1 and 0if both of the bits in the operand is 0.

Suppose that you have two variables ‘a’ and ‘b’:

a = 0011 1100

b = 0000 1101

Then the expression (a ^ b) will be equal to 0011 0001. The bitwise XOR operator results 1 if the bits in the two operands are different and a 0 if the bits in the two operands are same.

Suppose that you have two variables ‘a’ and ‘b’:

a = 0011 1100

b = 0000 1101

Then the expression (~ a) will be equal to 1100 0011. The bitwise compliment operator is a unary operator and is used to flip each bit in the operand.

### 9. Shift operator:

In Java there are three types of shift operators that are left shift, right shift and zero fill shift.

It is also called the binary left shift operator. The left shift is used to move the value of the left operand to number of bits specified by the right operand to the left side.

Suppose that you have two variables ‘a’ and ‘b’:

a = 0011 1100

b = 0000 1101

Then the expression (a << 2) will be equal to 1111 0000.

It is also called the binary right shift operator. The right shift operator is used to move the value of the left operand to number of bits specified by the right operand to the right side.

Suppose that you have two variables ‘a’ and ‘b’:

a = 0011 1100

b = 0000 1101

Then the expression (a >> 2) will be equal to 1111.

**Zero fill right shift operator:**

It is also called shift right zero fill operator. It is used to move the value of the left operand to number of bits specified by the right operand to the right side and the values that are shifted are filled with zeros.

Suppose that you have two variables ‘a’ and ‘b’:

a = 0011 1100

b = 0000 1101

Then the expression (a >>> 2) will be equal to 0000 1111.

**Shift AND assignment operators:**

Consider the following table in which the shift operators in the assignment statement are defined:

Operator |
Description |
Equivalent to |

<< =
A << = 2 |
It is called left shift AND assignment operator. | A = A << = 2 |

>> =
A >> = 2 |
It is called bitwise AND assignment operator. | A = A >> = 2 |

& =
A & = 2 |
It is called right shift AND assignment operator. | A = A & 2 |

### Operator Precedence in Java:

The operator precedence is used to show or indicate that how the expression will be evaluated, it is used to determine the group of terms in an expression. In an expression some operators have higher precedence and some have lower meaning that the operators having the higher precedence will be evaluated first.

For example in the expression a = 3+4*5, multiplication will be performed first that is 4 will be multiplied to 5 first and then the result will be added to 3. Hence we can say that the operator (*) has the highest precedence in the above expression.

Consider the following table in which the operators having the highest frequency are at the top and the operators having the lowest frequency at the bottom:

Operator |
Precedence |
Associativity |

Postfix | expression ++, expression — | Left to right |

Unary | ++ expression, — expression, + expression, – expression, ~, ! | Right to left |

Multiplicative | *, /, % | Left to right |

Additive | +, – | Left to right |

Shift | <<, >>, >>> | Left to right |

Relational | <, >, <=, >=, instanceof | Left to right |

Equality | ++, != | Left right |

Bitwise AND | & | Left to right |

Bitwise exclusive OR | ^ | Left to right |

bitwise inclusive OR | | | Left to right |

Logical AND | && | Left to right |

Logical OR | || | Left to right |

Ternary | ? : | Right to left |

Assignment | =, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>= | Right to left |