x

Java – Operator

Prev     Next

An operator is used to perform an operation over one or more operands. Operators in Java are classified as,


  1. Assignment Operator
  2. Arithmetic Operator
  3. Unary Operator
  4. Relational Operator
  5. Logical / Conditional Operator
  6. Bitwise Operator

1. Arithmetic Operators:

They are used to perform arithmetic operations over two operands.

Operator
Description
+ Adds both of the operands and returns the result.
Example:
int a = 10, b=2, c;
c = a + b; //c = 12
Subtracts the right hand side operand from the left hand side operand.
Example:
c = a – b; //c = 2
* Multiplies both the operands and returns the result.
Example:
c = a * b; //c = 20
/ Divides left hand side operand by the right hand side operand.
Example:
c = a / b; //c =5
% Divides left hand side operand by the right hand side operand and returns the reminder.
Example:
c = a % b; //c = 0

2. Unary Operators:

They are used to perform operations over a single operand.

Operator
Description
+ (Unary plus) For referring +ve values. By default all values are positive.

Example: int a = +5;

 

 

– (Unary Minus) Negates a value.
Example: int a = -5;
 

++operand
(Pre_Increment)operand++
(Post_Increment)

First increments the operand value by 1 and then the new value is used in the expression. The value of the operand is first used in the expression and then incremented by 1.

Example: Pre-increment:
Int a = 5;
int b = ++a + 1;    /* a = 6, after incrementing ‘a’, incremented value is added to 1 and ‘b’ becomes  7 */

Example: Post-increment:
Int a =5;
b = a++ +1;           /* b = 6. After calculating ‘b’, i.e. after executing this line of code,  ‘a’ becomes 6 */

– – operand
(Pre_Decrement)operand – –
(Post_Decrement)
First decrements the operand value by 1 and then the new value is used in the expression. The value of the operand is used in the expression first and then decremented by 1.

Example: Pre-decrement:
int a = 5;
int b = – – a + 1;    /* a = 4, after decrementing ‘a’, decremented value is added to 1 and b = 5 */

Example: Post-decrement:
int a = 5;
b = a – – +1;           /* b = 6. After calculating ‘b’, i.e. after executing this line of code,  ‘a’ becomes 4 */

(Logical NOT operator) Complement operator. It inverts the logical state of an operand.
Example:
boolean flag = !true;  /* flag will contain false */
!(A= =B)                       /* inverts the result of the expression */

3. Relational Operators:

They are used to compare the values of two operands.

Operator
Description
== Checks whether values of both side of the operands are equal.
Example:
int a = 10, b = 10;
boolean var = (a == b);Can be used in conditional statements like below:
if (a == b)
{
System.out.println(“values are equal”);
}Can be used in loops like below:
for(i=0; a==b;i++){
}
while(a==b) {
}
!= Checks whether values of both the operands are unequal.
Example:
check = (a! = b); // true if ‘a’ is not equal to ‘b’
 

>

Checks whether the operand in the left hand side is greater than the right hand side operand.
Example:
int a = 5, b = 2;
(a > b) would return true.
< Checks whether the operand in the left hand side is less than the right hand side operand.
Example:
int a = 5, b = 2;
(a < b) would return false.
>= Checks whether the operand in the left hand side is greater than or equal to the right hand side operand.
Example:
int a = 2, b = 2;
(a >= b) would return true.
<= Checks whether the operand in the left hand side is less than or equal to the right hand side operand.
Example:
int a = 3, b = 2;
(a <= b) would return false.

4. Logical / Conditional Operators:

Operator
Description
&&
(Logical AND)
If both the operands are true, then the condition evaluates to true. Otherwise as false.
Consider X = true, Y = true.
(X && Y) evaluates to true
| |
(Logical OR)
If any of the operand is true, then the condition evaluates to true. Otherwise as false.
Consider X = true, Y = true
(X | | Y) evaluates to true
? :
(Ternary operator)
This operator requires 3 operands. It represents short hand form of ‘if else’ loop.

Syntax:
variable = (expression) ? value1 : value2;
The expression before the ‘?’ is evaluated. If it is true, the value1 is assigned to the variable, otherwise value2 is assigned.


Example:
int num = 10;
int b = (num == 10)? 10 : 20;    /* b will have 10 */

5. Bitwise Operators:

Operator
Description
~
(Bitwise complement)

This operator inverts a bit pattern making 0’s to 1 and 1’s to 0.
Example:
00001100 would be change to 11110011

&
(Bitwise AND)
Performs bitwise AND operation.
Example:
consider x = 15, y = 6 (x = 0000 1111, y = 0000 0110)
x&y = 6 (0000 0110)
|
(Bitwise inclusive OR)
Performs bitwise OR operation.
Example:
consider x = 14, y = 12 (x = 0000 1110, y = 0000 1100)
x|y = 14 (0000 1110)
^
(Bitwise exclusive OR)
Performs bitwise XOR operation.
Example:
consider x = 15, y = 12 (x = 0000 1111, y = 0000 1100)
x^y = 3 (0000 0011)
<<
(Left shift)
The operand’s value is moved left by the number of bits specified in the right side of the operator <<.
Example:
consider x = 7 (0000 0111)
x<<2 = 24 (0001 1100)
>>
(Right shift)
The operand’s value is moved right by the number of bits specified in the right side of the operator >>.
Example:
consider x = 7 (0000 0111)
x>>2 = 1 (0001)
 >>>
(Zero fill right shift)
Same as right shift, fills zero in the shifted places.
Example:
consider x = 7 (0000 0111)
x>>>2 = 1 (0000 0001)

6. Assignment Operator:

The Assignment operator ‘=’ is used to assign the value on the right hand side to the operand on the left.

Example:

int i = 1;

The value 1 is assigned to the variable ‘i’.

Operator
Description
+= Performs addition & assigns the result to the left hand side.
Example:
x+=2;        //(x = x+2)
-= Performs subtraction and assigns the result to the left hand side.
Example:
x-=2;        //(x = x-2)
*= Performs multiplication and assigns the result to the left hand side.
Example:
x*=2;        //(x = x*2)
/= Performs division and assigns the result to the left hand side.
Example:
x/=2;        //(x = x/2)
%= Performs division and assigns the remainder to the left hand side.
Example:
x%=2;        //(x = x%2)
<<= Performs left shift and assigns the result to the left hand side.
Example:
x<<=2;        //(x = x<<2)
>>= Performs right shift and assigns the result to the left hand side.
Example:
x>>=2;        //(x = x>>2)
&= Performs Bitwise & and assigns the result to the left hand side.
Example:
x&=2;        //(x = x&2)
^= Performs Bitwise ^ and assigns the result to the left hand side.
Example:
x^=2;        //(x = x^2)
|= Performs Bitwise | and assigns the result to the left hand side.
Example:
x|=2;        //(x = x|2)

The instanceof Operator (Type comparison operator):

The instanceof operator is used to check whether an object is an instance of a particular type (class, array).

Example:

Output:

Is e1 object of Employee: true

Operator Precedence:

Operator
Associativity
() (Paranthesis)
[] (Array Subscript)
.(dot) (Member selection)
Left to right
++ (Unary pre/post increment)
— (Unary pre/post decrement)
! (Unary negation operator)
~ (Bitwise complement)
Right to left
* (Multiplication)
/ (Division)
% (Modulus)
Left to right
+ (Addition)
–  (Subtraction)
Left to right
>> (Right shift)
>>> (Zero right shift)
<< (Left shift)
Left to right
> (Greater than)
> (Less than)
>= (Greater than or equal to)
<= (Less than or equal to)
Left to right
== (Equal to)
!= (Not equal to)
Left to right
& (Bitwise AND)
^ (Bitwise XOR)
| (Bitwise OR)
&& (Logical AND)
|| (Logical OR)
Left to right
?: (Ternary) Right to left
= (Assignment)
+= (Addition Assignment)
-= (Subtraction Assignment)
*= (Multiplication Assignment)
/= (Division Assignment)
%= (Modulus Assignment)
>>= (Bitwise Right shift Assignment)
<<= (Bitwise Left shift Assignment)
&= (Bitwise AND Assignment)
^= (Bitwise XOR Assignment)
|= (Bitwise OR Assignment)
Right to left

Prev     Next



Like it? Please Spread the word!