Types of Operator in Java?


Operator

  • It is a special symbol which is used to perform logical or mathematical operation on data or variable.

Operand

  • It is a data or variable on which the operation is to be performed.

Types of Operator

  • ⇒Arithmetic Operators
  • ⇒Relational Operators
  • ⇒Logical Operators
  • ⇒Assignment Operators
  • ⇒Bitwise Operators
  • ⇒Increment/Decrement Operators
  • ⇒Conditional Operators

Arithmetic Operators

SymbolOperationExample
+Additionx+y
-Subtractionx-y
*Multiplicationx*y
/Divisionx/y
%Modulusx%y
class Easy
{
 public static void main(String[] args) 
 {
 int a=5,b=3;
 System.out.println("Add="+(a+b));
 System.out.println("Sub="+(a-b));
 System.out.println("Multi="+(a*b));
 System.out.println("Div="+(a/b));
 //Note:-modulus(%) always holds remainder value
 System.out.println("Mod="+(a%b));
 }
}
/*
### Output ###
Add=8
Sub=2
Multi=15
Div=1
Mod=2
*/

Relational Operators

SymbolOperationExample
==Equal to2==3 returns 0
!=Not equal to2!=3 returns 1
>Greater than2>3 returns 0
<Less than2<3 returns 1
>=Greater than or equal to2>=3 returns 0
<=Less than or equal to2<=3 returns 1

Logical Operators

(x>y)&&(x>z)Here this expression returns true if both conditions are true.
(x>y)||(x>z)Here this expression returns true if any one or both conditions are true.
!(x>y)Not operator reverses the state means if the condition is true it returns false and if the condition is false it returns true.
class Easy
{
 public static void main(String[] args) 
 {
 int a=10,b=60,c=40;
 if(a>b&&a>c)
  System.out.println("a is greatest");
 if(b>a&&b>c)
  System.out.println("b is greatest");
 if(c>a&&c>b)
  System.out.println("c is greatest");
 }
}
/*
### Output ###
b is greatest
*/

Assignment Operators

SymbolExampleSame as
=x=yx=y
+=x+=yx=x+y
-=x-=yx=x-y
*=x*=yx=x*y
/=x/=yx=x/y
%=x%=yx=x%y
class Easy
{
 public static void main(String[] args) 
 {
 int x1=5,y1=3;
 x1+=y1;//x1=x1+y1
 System.out.println(x1);

int x2=5,y2=3;
x2-=y2;//x2=x2-y2
System.out.println(x2);

int x3=5,y3=3;
x3*=y3;//x3=x3*y3
 System.out.println(x3);

int x4=5,y4=3;
x4/=y4;//x4=x4/y4
 System.out.println(x4);

int x5=5,y5=3;
x5%=y5;//x5=x5%y5
 System.out.println(x5);
 }
}
/*
### Output ###
8
2
15
1
2
*/

Bitwise Operators

SymbolOperationExample
&Bitwise ANDx&y
|Bitwise ORx|y
<<Shift Leftx<<2
>>Shift Rightx>>2
^X-ORx^y
class Easy
{
 public static void main(String[] args) 
 {//variable declaration
  int a=5,b=3,c;
  c=a&b;//AND Operation
  System.out.println("a&b="+c);
  c=a|b;//OR Operation
  System.out.println("a|b="+c);
  c=a>>2;//Shift right Operation
  System.out.println("a>>2="+c);
  c=a<<2;//Shift left Operation
  System.out.println("a<<2="+c);
  c=a^2;//X-OR Operation
  System.out.println("a^2="+c);
 }
}
/*
### Output ###
a&b=1
a|b=7
a>>2=1
a<<2=20
a^2=7
*/
For more details click here

Increment/Decrement Operators

SymbolNameFunctionExample
++IncrementIt increments the value by 1++x
--DecrementIt decrements the value by 1--x
class Easy
{
 public static void main(String[] args) 
 {//variable declaration
  int a=5,b=10;
   System.out.println(++a);
   System.out.println(--b);
 }
}
/*
### Output ###
6
9
*/

Conditional Operators

  • If the condition is true second part will execute otherwise third part will execute.
class Easy
{
 public static void main(String[] args) 
 {//variable declaration
  int a=5,b=10,max;
  max=a>b?a:b;
  //don't be confused,here + is separator in java
  System.out.println("Greater value is "+max);
  }
}
/*
### Output ###
Greater value is 10
*/
Next Post Previous Post
No Comment
Add Comment
comment url