Operators in Java

Operators are special symbols that perform specific operation on one, two or three operands and give the results.

OperatorPrecedence
postfixexpr++   expr--
unary++expr   --expr   +expr   -expr
arithmetic*   /   %   +   -
shift<<   >>   >>>
Relational<   >   <=   >=   instanceof
equality==   !=
bitwise&   ^   |
logical&&   | |
conditional?   :
assignment=   +=   -=   *=   /=   %=   &=   ^=   |=   <<=   >>=   >>>=

Assignment Operator

  • Assignment operator is used for assigning the value of any variable.
  • It assigns the value on its right to the operand on its left.
Example: int a =10;

Arithmetic Operator

Arithmetic operators are used to perform arithmetic operation.
Example: +, -, *, /, %

Example: Java program for arithmetic and assignment operator

class ArithmeticDemo
{
     public static void main(String args[])
     {
          int num1 = 15, num2 = 6;
          System.out.println( "num1 + num2 : " +  (num1+num2) );
          System.out.println( "num1 - num2 : " +  (num1-num2) );
          System.out.println( "num1 * num2 : " +  (num1*num2) );
          System.out.println( "num1 / num2 : " +  (num1/num2) );
          System.out.println( "num1 % num2 : " +  (num1%num2) );
     }
}


Output:
num1 + num2 : 21
num1 - num2 : 9
num1 * num2 : 90
num1 / num2 : 2
num1 % num2 : 3

Unary Operator

It requires only one operand. Unary operator performs the operations like increment, decrement etc.

Example: Simple program for unary operator

class IncreDecreDemo
{
     public static void main(String[] args)
     {
          int i = 5;
          i++
          System.out.println(i);     // prints 6
          ++i;    
          System.out.println(i);     // prints 7
          System.out.println(++i);     // prints 8
          System.out.println(i++);     // prints 8
          System.out.println(++i);     // prints 10
     }
}

Relational Operator

Relational operators are used to comparison between two operands. They generate the result in form of true and false i.e. boolean value.

Example: Simple program for relational operator

class RelationalDemo
{
     public static void main(String args[])
     {
          int num1 = 15;
          int num2 = 30;
          System.out.println("num1 == num2 = " + (num1 == num2) );
          System.out.println("num1 != num2 = " + (num1 != num2) );
          System.out.println("num1 > num2 = " + (num1 > num2) );
          System.out.println("num1 < num2 = " + (num1 < num2) );
          System.out.println("num1 >= num2 = " + (num1 >= num2) );
          System.out.println("num1 <= num2 = " + (num1 <= num2) );
     }
}


Output:
num1 == num2 = false
num1 != num2 = true
num1 > num2 = false
num1 < num2 = true
num2 >= num1 = false
num2 <= num1 = true

Bitwise Operator

Bitwise operator works on the binary value. It performs only on 1 and 0.

Example: Simple program for bitwise operator

class BitDemo
{
     public static void main(String args[])
     {
          int x = 5;
          int y = 3;
          System.out.println("x | y = "+(x | y));
          System.out.println("x & y = "+(x & y));
          System.out.println("x ^ y = "+(x ^ y));
          System.out.println("x >> 1 = "+(x >> 1));
          System.out.println("x << 1 = "+(x << 1));
          System.out.println("x >>> 1 = "+(x >>> 1));
     }
}


Output:
x | y = 7
x & y = 1
x ^ y = 6
x >> 1 = 2
x << 1 = 10
x >>> 1 = 2

Logical Operator

  • Logical operator gives the true or false result.
  • It is used for checking more than one condition.

Example: Program for logical operator

class LogicalTest
{
     public static void main(String args[])
     {
          boolean b1 = false;
          boolean b2 = true;
          System.out.println("b1 && b2 = " + (b1&&b2));
          System.out.println("b1 || b2 = " + (b1||b2) );
          System.out.println("! (b1 && b2) = " + ! (b1 && b2));
     }
}


Output:
b1 && b2 = false
b1 || b2 = true
! (b1 && b2) = true

Conditional Operator (? :)

  • Conditional operator is also known as ternary operator in Java. It works like if else statement.
  • Expr1 ? Expr2 : Expr3 (if  Expr1 is true it return Expr2 else Expr3)
Write a java program to find maximum and minimum number from given three numbers using conditional operator.

Example: Program for conditional operator

class MaxMinTest
{
     public static void main(String args[])
     {
          int a = 15;
          int b = 30;
          int c = 25;
          int max = (a > b & a > c) ? a : (b > c) ? b : c;
          int min = (a<b & a<c) ? a : (b<c) ? b : c;
          System.out.println("Maximum value is: "+max);
          System.out.println("Minimum value is: "+min);
     }
}


Output:
Maximum value is: 30
Minimum value is: 15