Operators In Java

Explore the realm of operators in Java with our comprehensive guide. From the fundamental Assignment Operator to Arithmetic, Relational, Logical, Bitwise, Unary, and Ternary Operators, delve into coding examples that demystify their functionality. Elevate your Java programming skills by understanding how these operators manipulate data, making your code efficient and concise.


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
Java Operators

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 EqualTo Explanation
= a = b; a = b; It takes the value of b and assigns into a.
+= a += b; a = a+b; The a += b operator in Java is a shortcut for adding the value of b to the current value of a, updating a in the process.
-= a -= b; a = a-b; The a-=b operator in Java subtracts the value of b from the current value of a, updating a in the process.
*= a *= b; a = a*b; The a *= b operator in Java multiplies the current value of a by the value of b, updating a accordingly.
/= a /= b; a = a/b; The a /= b operator in Java divides the current value of a by the value of b, and the result is assigned back to a.
%= a %= b; a = a%b; The a %= b operator in Java calculates the remainder when the current value of a is divided by the value of b, updating a with the result.

An example of assignment operator:


  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.

An example of arithmetic operator:


  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.

An example of relational operator:


  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.

An example of logical operator:


  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 :-

In Java, unary operators perform operations on a single operand. Examples include ++, -- for increment and decrement, and - for negation. These operators allow concise manipulation of variables in various expressions. Unary operators in Java are given below:

Operator Description Example
++ Increment Operator If a = 4 then a++ will be 5.
  • The pre-increment (++a) operator in Java increases the value of a by 1 before any other operation. The updated value is then used in the expression.
  • The post-increment (a++) operator in Java also increases the value of a by 1, but it uses the current value of a in the expression before updating it.
-- Decrement Operator If a = 4 then a-- will be 3.
  • The pre-decrement (--a) operator decreases the value of a by 1 before any other operation. The updated value is then used in the expression.
  • The post-decrement (a--) operator in Java also decreases the value of a by 1, but it uses the current value of a in the expression before updating it.

An example of unary operator:


  import java.io.*;
  Class Main{  
    Public static void main(String args[]){  
    int a = 5, b = 5, c = 5, d = 5;
    System.out.println("Pre-Increment: "+ (++a));
    System.out.println("Post-Increment: "+ (a++));
    System.out.println("Pre-Decrement: "+ (--c));
    System.out.println("Post-Decrement: "+ (d--));
    }
  }

Output

 
  Pre-Increment: 6
  Post-Increment: 5
  Pre-Decrement: 4
  Post-Decrement: 5
 

Ternary Operators In Java :-

In Java, the ternary operator (conditional operator) is a concise way to write a simple conditional expression. It has the form condition ? expression1 : expression2, and it evaluates to expression1 if the condition is true, or expression2 otherwise. For an example:

  
  variable = expression ? value1 : value2;
   

the expression variable = expression ? value1 : value2; uses the ternary operator. It evaluates the condition represented by expression. If the condition is true, it assigns value1 to variable; otherwise, it assigns value2. This concise form is a shorthand way to express a simple conditional assignment in Java.

An example of ternary operator:


  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