Operators in Java programming Hands on Coding

 

Operator;

Operator is a special symbol which can be used to perform logical and arithmetic operations on data or variable.


Operand; Operand is a data or variable on which the operation is to be performed.

Types of Operators;

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Bitwise Operators
  • Increment/Decrement Operators
  • Conditional Operators
Let's see each type in detail.


1. Arithmetic Operators;

symbol 

operation 

example 

+ 

addition 

x + y 

- 

subtraction 

x-y 

* 

multiplication 

x*y 

/ 

division 

x/y 

% 

modulus 

x % 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;

     symbol 

        Operation 

Example  

= = 

Equal to  

2==3  

returns 0 

!= 

Not equal  

to 

2! =3  

returns 1 

> 

Greater  

than 

2>3  

returns 0 

< 

Less than 

2<3  

returns 1 

>= 

Greater  

than or  

equal to  

2>=3  

returns 0 

<= 

Less than  

or equal to 

 

2<=3  

returns 1 

 Logical Operators;

Symbol              

Operation     

  Example 

    &&   

Logical 

 AND       

if(x>y&&x>z) 

      | |   

Logical 

 OR 

if(x>y || x>z) 

       !       

Logical 

 NOT    

if(!(x>y)) 

(x>y)&&(x>z);

This expression will return true if both conditions are true.


(x>y)||(x>z);

This expression will return true if any one or both conditions are true or 1.


!(x>y);

This operator will reverse 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;

 

Symbol              

example   

Same as 

  = = 

x=y    

x=y 

  +=  

x+=y 

x=x + y 

  - =       

x-= y 

x=x-y 

  *= 

x*=y 

x=x*y 

  / =  

x/=y 

x=x/y 

  %= 

x%=y 

x=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;

 

symbol 

operation 

example 

& 

Bitwise AND 

x &y 

| 

Bitwise OR 

x| y 

<< 

Left shift 

<<2 

>> 

Right shift 

>>2 

^ 

X-or 

x ^ 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   


Increment/Decrement Operators;

 

symbol 

name 

function 

example 

++ 

Increment 

Increments the value by 1 

++x 

-- 

Decrement 

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);    
  }    
  }   

For Videos Join Our Youtube Channel: Join Now


Thanks for reading!