Programming |
What is an Operator?
An operator is usually an instruction given to the computer. In mathematics we use +, -, x, etc to perform various operations. Then those +, -, x are different types of operators and they have been assigned with different tasks like addition, subtraction, multiplication respectively. For e.g.
A + B = C
Here + and = symbols are two operators.
What is an Operand?
The alphabets or numbers which are used to perform the instruction are called as an operand. In the above example A, B, and C are the operands. Depending on the number of operands there are three types of operators defined in Java:
Here is a list of different types of operators in Java:
Explanation in Detail
Operator |
Description |
Example |
++ |
Increases the value of operand by 1 |
A++ is equivalent to A = A+1 |
-- |
Decreases the value of operand by 1 |
A-- is equivalent to A = A11 |
! (NOT) |
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false |
!A is true |
Operator |
Description |
Example |
= |
Simple assignment operator, Assigns values from right side operands to left side operand |
C = A + B will assign value of A + B into C |
+= |
Addition and assignment operator. It adds right operand to the left operand and assigns the result to left operand |
A += B is equivalent to A = A + B |
-= |
Subtract and assignment operator, It subtracts right operand from the left operand and assigns the result to left operand |
A -= B is equivalent to A = A – B |
*= |
Multiply and assignment operator, It multiplies right operand with the left operand and assigns the result to left operand |
A *= B is equivalent to A = A * B |
/= |
Divide and assignment operator, It divides left operand with the right operand and assigns the result to left operand |
A /= B is equivalent to A = A / B |
%= |
Modulus and assignment operator, It takes modulus using two operands and assign the result to left operand |
A %= B is equivalent to A = A % B |
<<= |
Left shift and assignment operator |
A <<= 2 is same as A = A << 2 |
>>= |
Right shift and assignment operator |
A >>= 2 is same as A = A >> 2 |
&= |
Bitwise and assignment operator |
A &= 2 is same as A = A & 2 |
^= |
bitwise exclusive OR and assignment operator |
A ^= 2 is same as A = A ^ 2 |
|= |
bitwise inclusive OR and assignment operator |
A |= 2 is same as A = A | 2 |
Operator |
Description |
Example |
== |
Checks if the value of two operands is equal, if yes then condition becomes true else it is false. |
(A == B) is not true |
!= |
Checks if the value of two operands is not equal, if values are not equal then the condition becomes true. |
(A != B) is true |
> |
Checks if the value of the left operand is greater than the value of right operand, if yes then condition becomes true. |
(A > B) is not true |
< |
Checks if the value of the left operand is less than the value of right operand, if yes then condition becomes true. |
(A < B) is true |
>= |
Checks if the value of the left operand is greater than or equal to the value of right operand, if yes then condition becomes true. |
(A >= B) is not true |
<= |
Checks if the value of the left operand is less than or equal to the value of right operand, if yes then condition becomes true. |
(A <= B) is true |
Operator |
Description |
Example |
&& (AND) |
Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. |
(A && B) is false. |
|| (OR) |
Called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true. |
(A || B) is true. |
! (NOT) |
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. |
!A is true. |
Operator |
Description |
Example |
& |
Binary AND Operator copies a bit to the result if it exists in both operands. |
(A & B) will give 12 which is 0000 1100 |
| |
Binary OR Operator copies a bit if it exists in either operand. |
(A | B) will give 61 which is 0011 1101 |
^ |
Binary XOR Operator copies the bit if it is set in one operand but not both. |
(A ^ B) will give 49 which is 0011 0001 |
^ |
Binary XOR Operator copies the bit if it is set in one operand but not both. |
(A ^ B) will give 49 which is 0011 0001 |
~ |
Binary One's Complement Operator is unary and has the effect of 'flipping' bits. |
(~A ) will give -60 which is 1100 0011 |
<< |
Binary Left Shift Operator. The left operand's value is moved left by the number of bits specified by the right operand. |
A << 2 will give 240 which is 1111 0000 |
>> |
Binary Right Shift Operator. The left operand's value is moved right by the number of bits specified by the right operand. |
A >> 2 will give 15 which is 1111 |
>>> |
Shift right zero-fill operators. The left operands value is moved right by the number of bits specified by the right An operand and shifted values are filled up with zeros. |
A >>>2 will give 15 which is 0000 1111 |
instanceof operator will return true if both the objects are of the same type. It works for class, subclass, and interface types. It is used to check if the reference and the specified type have some inheritance (is-a) relation. Hence it is also called a type comparison operator. instanceof is also a keyword in java. This operator is a binary operator. The left side operand must be a reference and the right side operand must be a type name.
It is used to return the value based on some condition. For e.g.:
<booleanValue>?<value1>:<value2>
The first operand should result in a Boolean type. If the Boolean value is true then the value1 will be returned otherwise value2 will be returned. The type of value1 and value2 must be assignment compatible with the variable where the returned value will be stored. It can be used as a replacement for the if-else condition.
Java Operator Precedence
Operator precedence means if there is more than one operator in an expression then which expression will be solved first and what will be the direction of solving the expression. It is similar to BODMAS rule in mathematics. This is a tabular representation of the precedence and associativity of Java Operators.
Precedence* |
Operator |
Type |
Associativity |
15 |
() |
Parentheses |
Left to Right |
14 |
++ |
Unary post-increment |
Right to left |
13 |
++ |
Unary pre-increment |
Right to left |
12 |
* |
Multiplication |
Left to right |
11 |
+ |
Addition |
Left to right |
10 |
<< |
Bitwise left shift |
Left to right |
9 |
< |
Relational less than |
Left to right |
8 |
== |
Relational is equal to |
Left to right |
7 |
& |
Bitwise AND |
Left to right |
6 |
^ |
Bitwise exclusive OR |
Left to right |
5 |
| |
Bitwise inclusive OR |
Left to right |
4 |
&& |
Logical AND |
Left to right |
3 |
|| |
Logical OR |
Left to right |
2 |
? : |
Ternary conditional |
Right to left |
1 |
= |
Assignment |
Right to left |
*Larger number means higher precedence.