Operators In Java

In this page, we will learn about Operators In Java, What are the Operators in Java?, Assignment Operators In Java, Arithmetic Operators In Java, Relational Operators In Java, Logical Operators In Java, Bitwise Operators In Java, Unary Operators In Java, Ternary Operators In Java.


What are the Operators in Java?

Operator in Java is a symbol or a sign which is used for performing operations. For example: -, +, /, * etc. In Java there are many different types of operators which are given below:-

  • Assignment Operator
  • Arithmetic Operator
  • Relational Operator
  • Logical Operator
  • Bitwise Operator
  • Unary Operator
  • Ternary Operator

Assignment Operators In Java :-

Assignment = operators are used to assigns the value on its left variable to the right side variable or data. Assignment operators in Java are given below:

Operator Example Equal To Explain
= a = b; a = b; It takes the value of b and assigns into a.
+= a += b; a = a + b; It takes the value of a + b and assigns into a.
-= a -= b; a = a - b; It takes the value of a - b and assigns into a.
*= a *= b; a = a * b; It takes the value of a * b and assigns into a.
/= a /= b; a = a / b; It takes the value of a / b and assigns into a.
%= a %= b; a = a % b; It takes the value of a % b and assigns into a.

Example:

 
    import java.io.*;
    Class Main{  
     Public static void main(String args[]){  
       int a = 12, b = 5;
       int c = 12, d = 5; 

       a += b;
       c %= d;  

       System.out.println(a);
       System.out.println(c);         
     }
    }
 
 

Output

    17
    2

Arithmetic Operators In Java :-

Arithmetic operators are used to perform arithmetic operations on two or more variables. Arithmetic operators in Java are given below:

Operator Operation
+ It adds two or more variables / values one with others.
- It subtracts two or more variables / values on from another
* It multiplies two or more variables / values one with others.
/ It division two or more variables / values one by anothers.
[N:B If you use the division operator with two integers, then the result will also be an integer. And, if atleast one of the operands is a floating point number, you will get the result will also be in floating point. ]
% Modulo Operation is basically keep the remainder.

Example:


    import java.io.*;
    Class Main{  
     Public static void main(String args[]){  
      int a = 12, b = 5;
      float c = 12, d = 5;
      int Addition = a + b; // addition operator
      int Substract = a - b; // subtraction operator
      int Multiplication = a * b; // multiplication operator
      float Division = c / d; // division operator
      int Modulo = a % b; // modulo operator
    
      System.out.println("a + b = "+ Addition);
      System.out.println("a - b = "+ Substract);
      System.out.println("a * b = "+ Multiplication);
      System.out.println("c / d = "+ Division);
      System.out.println("a % b = "+ Modulo);   
     }
    }

Output

    a + b = 17
    a - b = 5
    a * b = 60
    c / d = 2.4
    a + b = 2

Relational Operators In Java :-

Relational operators are used to check the relationship or comparison between two operands or variables. Relational operators in Java are given below:

Operator Description Example
== Is Equal To 5 == 5 will return true.
!= Is Not Equal To 5 != 4 will return true.
> Greater Than 5 > 4 will return true.
< Less Than 5 < 4 will return false.
>= Greater Than Equal To 5 >= 4 will return true.
<= Less Than Equal To 5 <= 4 will return false.

Example:


    import java.io.*;
    Class Main{  
     Public static void main(String args[]){  
      int a = 5, b = 4;
      System.out.println(a == b);
      System.out.println(a >= b);         
     }
    }

Output

    false
    true

Logical Operators In Java :-

Logical operators are used to check 2 or more expression is true or not. Logical operators in Java are given below:

Operator Example Description
&& (Logical AND) 5 > 4 && 9 > 3 If both side expression of && (AND) is true, then it returns true.
|| (Logical OR) 5 > 4 || 9 < 3 If any one side expression of || (OR) is true, then it returns true.
! (Logical NOT) !expression If expression is true, then it returns false and if expression is false then it returns true and vice versa.

Example:


  import java.io.*;
  Class Main{  
    Public static void main(String args[]){  
     int a = 5, b = 4, c = 1;
     System.out.println(a > b && b > c);
     System.out.println(a > b || b < c);
     System.out.println( !(a > b) );      
    }
 }

Output

    true
    true
    false

Bitwise Operators In Java :-

Bitwise operators in Java are used to perform operations on every bits. Bitwise operators in java are:

Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise Complement
<< Bitwise Left Shift
>> Bitwise Right Shift
>>> Unsigned Right Shift

Unary Operators In Java :-

Unary operators are worked with one variable or operands. Unary operators in Java are given below:

Operator Description Example
++ Increment Operator If a = 4 then a++ will be 5.
-- Decrement Operator If a = 4 then a-- will be 3.

Example:


  import java.io.*;
  Class Main{  
    Public static void main(String args[]){  
     int a = 5, b = 4;
     System.out.println(a++);
     System.out.println(b--);   
    }
 }

Output

    6
    3

Ternary Operators In Java :-

Ternary operators is basically a conditional operator and sign is ?. For an example:

    variable = expression ? value1 : value2;

In the above example if expression is true then value1 will be stored in variable and if expression is false then value2 will be stored in variable.

Example:


    import java.io.*;
    Class Main{  
     Public static void main(String args[]){  
        int a = 5, b;
        b = a < 3 ? 2 : 4
        System.out.println(b);  
     }
    }

Output

   4