The Java operator offers a rich set of environment Java operators can be divided into the following categories:
- Arithmetic operators
- Relation operators
- Logical operators
- Bitwise operators
- Assignment operators
- Conditional operators
- Misc operators
Arithmetic operators
Operator | Description |
---|---|
+ | adds two operands |
- | subtract second operands from first |
* | multiply two operand |
/ | divide numerator by denumerator |
% | remainder of division |
++ | Increment operator increases integer value by one |
-- | Decrement operator decreases integer value by one |
Example -
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
System.out.println(a+b);//15
System.out.println(a-b);//5
System.out.println(a*b);//50
System.out.println(a/b);//2
System.out.println(a%b);//0
}}
public static void main(String args[]){
int a=10;
int b=5;
System.out.println(a+b);//15
System.out.println(a-b);//5
System.out.println(a*b);//50
System.out.println(a/b);//2
System.out.println(a%b);//0
}}
Example -
class OperatorExample{
public static void main(String args[]){
System.out.println(10*10/5+3-1*4/2);
}}
public static void main(String args[]){
System.out.println(10*10/5+3-1*4/2);
}}
Relation operators
Operator | Description |
---|---|
== | Check if two operand are equal |
!= | Check if two operand are not equal. |
> | Check if operand on the left is greater than operand on the right |
< | Check operand on the left is smaller than right operand |
>= | check left operand is greater than or equal to right operand |
<= | Check if operand on left is smaller than or equal to right operand |
Example -
class OperatorExample{
public static void main(String args[]){
System.out.println(10<<2);//10*2^2=10*4=40
System.out.println(10<<3);//10*2^3=10*8=80
System.out.println(20<<2);//20*2^2=20*4=80
System.out.println(15<<4);//15*2^4=15*16=240
}}
public static void main(String args[]){
System.out.println(10<<2);//10*2^2=10*4=40
System.out.println(10<<3);//10*2^3=10*8=80
System.out.println(20<<2);//20*2^2=20*4=80
System.out.println(15<<4);//15*2^4=15*16=240
}}
Example -
class OperatorExample{
public static void main(String args[]){
//For positive number, >> and >>> works same
System.out.println(20>>2);
System.out.println(20>>>2);
//For negative number, >>> changes parity bit (MSB) to 0
System.out.println(-20>>2);
System.out.println(-20>>>2);
}}
public static void main(String args[]){
//For positive number, >> and >>> works same
System.out.println(20>>2);
System.out.println(20>>>2);
//For negative number, >>> changes parity bit (MSB) to 0
System.out.println(-20>>2);
System.out.println(-20>>>2);
}}
Logical operators
Operator | Description | Example |
---|---|---|
&& | Logical AND | (a && b) is false |
|| | Logical OR | (a || b) is true |
! | Logical NOT | (!a) is false |
Example -
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
System.out.println(a<b&&a<c);//false && true = false
System.out.println(a<b&a<c);//false & true = false
}}
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
System.out.println(a<b&&a<c);//false && true = false
System.out.println(a<b&a<c);//false & true = false
}}
Bitwise operators
Operator | Description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise exclusive OR |
>> | left shift |
<< | right shift |
Now lets see truth table for bitwise
&
, |
and ^
a | b | a & b | a | b | a ^ b |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
Assignment Operators
Operator | Description | Example |
---|---|---|
= | assigns values from right side operands to left side operand | a = b |
+= | adds right operand to the left operand and assign the result to left | a+=b is same as a=a+b |
-= | subtracts right operand from the left operand and assign the result to left operand | a-=b is same as a=a-b |
*= | mutiply left operand with the right operand and assign the result to left operand | a*=b is same as a=a*b |
/= | divides left operand with the right operand and assign the result to left operand | a/=b is same as a=a/b |
%= | calculate modulus using two operands and assign the result to left operand | a%=b is same as a=a%b |
Example -
class OperatorExample{
public static void main(String[] args){
int a=10;
a+=3;//10+3
System.out.println(a);
a-=4;//13-4
System.out.println(a);
a*=2;//9*2
System.out.println(a);
a/=2;//18/2
System.out.println(a);
}}
public static void main(String[] args){
int a=10;
a+=3;//10+3
System.out.println(a);
a-=4;//13-4
System.out.println(a);
a*=2;//9*2
System.out.println(a);
a/=2;//18/2
System.out.println(a);
}}
Conditional operator
It is also known as ternary operator and used to evaluate Boolean expression,
epr1 ? expr2 : expr3
0 comments:
Post a Comment