Java operator

Operators are symbols which is use to perform some specific operations. Java has rich set of operators. Java operator are divided into the following four groups:

  1. Arithmetic operator
  2. Bitwise operator
  3. Relational operator
  4. Logical operator

It is used to perform mathematical operations on numbers.The following table lists the arithmetic operators. Here variables M and N are defined as integer type of variables which holds integer value.
int M=100, N=50;

Operatormeaningdescriptionexampleresult
+AdditionAdds numbers or variables or bothM+N150
-Subtractionsubtract numbers or variables or bothM-N50
*MultiplicationMultiply numbers or variables or bothM*N5000
/DivisionDivides numbers or variables or both and returns quotient of divisionM/N2
%Modulusmodulus division of numbers or variables or both and returns remainder of divisionM%N0
++IncrementIncrements value of variable by 1++MM=101
- -DecrementsDecrements value of variable by 1--MM=99
+=Addition assignmentAdds left hand side variable with right hand side variable and stores result in left hand side variable. Its is equivalent to M=M+NM+=NM=150;
-=Substraction assignmentSubstracts right hand side variable from left side variable and stores result in left hand side variable. Its is equivalent to M=M-NM-=NM=50;
*=Multiplication assignmentMultiplies left hand side variable with right hand side variable and stores result in left hand side variable. Its is equivalent to M=M*NM*=NM=5000;
/=Division assignmentDivides left hand side variable with right hand side variable and stores quotient of division in left hand side variable. Its is equivalent to M=M/NM/=NM=2;
%=Modulus assignmentDivides left hand side variable with right hand side variable and stores remainder of division in left hand side variable. Its is equivalent to M=M%NM%=NM=0;




Bitwise operator use to perform operation on one or more bits of operands.Operand is one on which we perform operation.Here variables M and N are defined as integer type of variables which holds integer value.
int M=100, N=50;

binary value of M=100 is 1100100
binary value of N= 50 is  0110010

Operatormeaningdescriptionexampleresult
~Bitwise NOTPerforms one's complement operation~M1100100 (M)
0011011(~M)
&Bitwise ANDPerforms bitwise AND i.e bit by bit binary multiplication between
two variables or numbers or both
M & N1100100(M)
0110010(N)
0100000(&)
|Bitwise ORPerforms bitwise OR between two variables or numbers or bothM | N1100100(M)
0110010(N)
1110110(|)
^Bitwise EX-ORPerforms bitwise EX-OR between two variables or numbers or bothM ^ N1100100(M)
0110010(N)
1010110(^)
>>Right shiftThe right shift operator, >> , shifts all of the bits in a value to the right a specified number of times.
Syntax: value >> shift number
M>>11100100 ( M )
0110010(M>>1)
<<Left shiftThe left shift operator, << , shifts all of the bits in a value to the left a specified number of times.
Syntax: value << shift number
M<<101100100 ( M )
11001000(M<<1)
&=Bitwise AND assignmentPerforms bitwise AND left hand side variable and right hand side variable and assigns result to left hand side variable
M&=N is equvivalent to M=M&N;
M&=N1100100(M)
0110010(N)
0100000(&)
|=Bitwise OR assignmentPerforms bitwise OR left hand side variable and right hand side variable and assigns result to left hand side variable
M|=N is equvivalent to M=M|N;
M|=N1100100(M)
0110010(N)
1110110(|)
^=Bitwise EX-OR assignmentPerforms bitwise EX-OR left hand side variable and right hand side variable and assigns result to left hand side variable
M^=N is equvivalent to M=M^N;
M^=N1100100(M)
0110010(N)
1010110(^)
<<=Left shift assignmentThe left shift operator, << , shifts all of the bits of left hand side variable to the left a specified number of times and stores result in left hand side variable
Syntax: value << shift number
M<<=N is equvivalent to M=M << N;
M<<= N01100100 ( M )
11001000(M<<1)
>>=right shift assignmentThe right shift operator, >> , shifts all of the bits of left hand side variable to the right a specified number of times and stores result in left hand side variable
Syntax: value >> shift number
M>>=N is equvivalent to M=M >> N;
M>>= N1100100 ( M )
0110010(M>>1)




Relational operator use to determine the relationship between two operands i.e variables or numbers or both.Here variables A and B are defined as integer type of variables which holds integer value.
int A=100, B=100;

Operatormeaningdescriptionexampleresult
==Equal toIf value of left hand side variable is equal to value of right hand side then returns true else returns falseA==B100==100
true
!=Not equal toIf value of left hand side variable is not equal to value of right hand side then returns true else returns falseA!=B100==100
false
<Less thanIf value of left hand side variable is less than value of right hand side then returns true else returns false A< B100<100
false
<=Less than equal toIf value of left hand side variable is less than or equal to the value of right hand side then returns true else returns false A<= B100<100
true
>Greater thanIf value of right hand side variable is greater than value of left hand side then returns true else returns falseB>A100>100
false
>=Greater than equal toIf value of right hand side variable is greater than or equal to the value of left hand side then returns true else returns falseB>=A100>100
true

Use to perform logical operation on two or more boolean values. Operand is one on which we perform operation. Here variables A and B are defined as boolean type of variables which holds boolean value.
boolean A=true, B=false;

Operatormeaningdescriptionexampleresult
&&Logical ANDPerforms logical AND operation on two operands. If both operands are true then returns true else returns falseA&&Btrue
false
false
||Logical ORPerforms logical OR operation on two operands. If one or more than operand are true then returns true else returns falseA||Btrue
false
true
!Logical NOTConverts true value to false and vice versa!A!true
false

Assignment operator use to assign right hand side value to left hand side variable. Assignment operator is the equal sign (=). General form of assignment operator is shown below.
variable = expression or values;

Example:
int a=20; // Equal operator assigns 20 to variable a.

Ternary operator replaces if else statement in java. General form of ternary operator is given below.
Expression1 ? Expression2 : Expression 3 ; OR
Expression1 ? value1 : value2;
If Expression1 is true, then value1 one will return else value2 will return.

Example:
System.out.println( 10>5? “true” : “false” );

Output:
true

 

You may also like...

Leave a Reply