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:
- Arithmetic operator
- Bitwise operator
- Relational operator
- 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;
Operator | meaning | description | example | result |
---|---|---|---|---|
+ | Addition | Adds numbers or variables or both | M+N | 150 |
- | Subtraction | subtract numbers or variables or both | M-N | 50 |
* | Multiplication | Multiply numbers or variables or both | M*N | 5000 |
/ | Division | Divides numbers or variables or both and returns quotient of division | M/N | 2 |
% | Modulus | modulus division of numbers or variables or both and returns remainder of division | M%N | 0 |
++ | Increment | Increments value of variable by 1 | ++M | M=101 |
- - | Decrements | Decrements value of variable by 1 | --M | M=99 |
+= | Addition assignment | Adds left hand side variable with right hand side variable and stores result in left hand side variable. Its is equivalent to M=M+N | M+=N | M=150; |
-= | Substraction assignment | Substracts right hand side variable from left side variable and stores result in left hand side variable. Its is equivalent to M=M-N | M-=N | M=50; |
*= | Multiplication assignment | Multiplies left hand side variable with right hand side variable and stores result in left hand side variable. Its is equivalent to M=M*N | M*=N | M=5000; |
/= | Division assignment | Divides 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/N | M/=N | M=2; |
%= | Modulus assignment | Divides 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%N | M%=N | M=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
Operator | meaning | description | example | result |
---|---|---|---|---|
~ | Bitwise NOT | Performs one's complement operation | ~M | 1100100 (M) 0011011(~M) |
& | Bitwise AND | Performs bitwise AND i.e bit by bit binary multiplication between two variables or numbers or both | M & N | 1100100(M) 0110010(N) 0100000(&) |
| | Bitwise OR | Performs bitwise OR between two variables or numbers or both | M | N | 1100100(M) 0110010(N) 1110110(|) |
^ | Bitwise EX-OR | Performs bitwise EX-OR between two variables or numbers or both | M ^ N | 1100100(M) 0110010(N) 1010110(^) |
>> | Right shift | The right shift operator, >> , shifts all of the bits in a value to the right a specified number of times. Syntax: value >> shift number | M>>1 | 1100100 ( M ) 0110010(M>>1) |
<< | Left shift | The left shift operator, << , shifts all of the bits in a value to the left a specified number of times. Syntax: value << shift number | M<<1 | 01100100 ( M ) 11001000(M<<1) |
&= | Bitwise AND assignment | Performs 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&=N | 1100100(M) 0110010(N) 0100000(&) |
|= | Bitwise OR assignment | Performs 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|=N | 1100100(M) 0110010(N) 1110110(|) |
^= | Bitwise EX-OR assignment | Performs 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^=N | 1100100(M) 0110010(N) 1010110(^) |
<<= | Left shift assignment | The 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<<= N | 01100100 ( M ) 11001000(M<<1) |
>>= | right shift assignment | The 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>>= N | 1100100 ( 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;
Operator | meaning | description | example | result |
---|---|---|---|---|
== | Equal to | If value of left hand side variable is equal to value of right hand side then returns true else returns false | A==B | 100==100 true |
!= | Not equal to | If value of left hand side variable is not equal to value of right hand side then returns true else returns false | A!=B | 100==100 false |
< | Less than | If value of left hand side variable is less than value of right hand side then returns true else returns false | A< B | 100<100 false |
<= | Less than equal to | If 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<= B | 100<100 true |
> | Greater than | If value of right hand side variable is greater than value of left hand side then returns true else returns false | B>A | 100>100 false |
>= | Greater than equal to | If value of right hand side variable is greater than or equal to the value of left hand side then returns true else returns false | B>=A | 100>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;
Operator | meaning | description | example | result |
---|---|---|---|---|
&& | Logical AND | Performs logical AND operation on two operands. If both operands are true then returns true else returns false | A&&B | true false false |
|| | Logical OR | Performs logical OR operation on two operands. If one or more than operand are true then returns true else returns false | A||B | true false true |
! | Logical NOT | Converts 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