Operators in java

Today we are going to learn operators in java.

Operators in java

Operator is symbol to perform operations. Here are types of operators,

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

Arithmetic Operator

Arithmetic operator is used in mathematical expressions to perform division, addition, subtraction and multiplication.

public class ArithmeticOperatorDemo 
{
   public static void main(String[] args) 
   {
      int i = 25;
      int j = 5;
      System.out.println(i + j);
      System.out.println(i - j);
      System.out.println(i * j);
      System.out.println(i / j);
      System.out.println(i % j);
   }
}

Output :

30
20
125
5
0


Unary operator

This operator need one operand. Using unary operator we can,

  1. increment or decrement a value by one
  2. negating expression
  3. inverting a value

In the post-increment operation, value of variable ‘a’ will be changed only after plus symbol. So this plus symbol will increment value of ‘a’ by 1.

While in pre-increment operation value of variable ‘a’ is increased first by 1.

public class UnaryPostIncrementDemo
{
   public static void main(String[] args)
   {
      int a = 22; 
      System.out.println(a++);
      System.out.println(++a); 
      System.out.println(a--); 
      System.out.println(--a);
      System.out.println(a++ + ++a);  
      System.out.println(b++ + b++);
   }
}

Output:

22
24
24
22
46
21


Negating and Inverting operator

public class UnaryOperator 
{
   public static void main(String[] args) 
   {
      int i = 50; 
      int j = -50; 
      boolean a = true; 
      boolean b = false; 
      System.out.println(~i);
      System.out.println(~j); 
      System.out.println(!a); 
      System.out.println(!b);
   }
}

Output:

-51
49
false
true


Logical Operator

Logical operator helps us understand the result of two operands X and Y for making a decision.

  • && – logical AND
  • || – logical OR

Logical AND (&&)

If both conditions are true then it returns true.

Logical OR (||)

If atleast one of the two condition is true then result is true.

Logical NOT (!)

This operator reverses the result. For example, returns true if the operand to the right evaluates to false.

public class LogicalOperatorDemo 
{
   public static void main(String[] args) 
   {
      System.out.println("Logical AND Operator");
      int num1 = 20;
      int num2 = 30;
      int num3 = 40;

      boolean output1 = num2 > num1 && num2 < num3;
      System.out.println(output1);

      boolean output2 = num2 < num1 && num2 < num3;
      System.out.println(output1);

      boolean output3 = num2 > num1 && num2 > num3;
      System.out.println(output3);

      boolean output4 = num2 < num1 && num2 > num3;
      System.out.println(output4);

      System.out.println("Logical OR Operator");
      output1 = num2 > num1 || num2 < num3;
      System.out.println(output1);

      output2 = num2 < num1 || num2 < num3;
      System.out.println(output2);

      output3 = num2 > num1 || num2 > num3;
      System.out.println(output3);

      output4 = num2 < num1 || num2 > num3;
      System.out.println(output4);

      System.out.println("Logical Not Operator");
      boolean temp = false ;
      boolean temp1 = !temp;
      System.out.println("Temp : " + temp);
      System.out.println("Logical not : " + temp1);

      int n1 = 30;
      int n2 = 45;
      int n3 = 50;

      boolean result1 = n1 < n2;
      boolean result2 = !(n1 < n2);
      System.out.println(result1);
      System.out.println(result2);

      boolean result3 = n1 < n2 && n2 < n3;
      boolean result4 = !(n1 < n2 && n2 < n3);
      System.out.println(result3);
      System.out.println(result4);
   }
}

Output:

Logical AND Operator
true
true
false
false
Logical OR Operator
true
true
true
false
Logical Not Operator
Temp : false
Logical not : true
true
false
true
false


Shift operator

  • << left shift operator
  • >> right shift operator
  • >>> zero fill right shift operator

Here 8 left shift 2 means, left operand value is moved left by number of bits specified by right operand.

public class LeftShiftOperatorDemo 
{
   public static void main(String[] args) 
   {
      System.out.println(8 << 2); // 100000
   }
}

Output:

32


Here 8 right shift 2 means, left operand value is moved by right by number of bits specified by right operand.

public class RightShiftOperatorDemo 
{
   public static void main(String[] args) 
   {
      System.out.println(8 >> 2); // 10
   }
}

Output:

2


Here 8 zero fill right shift 2 means, left operand value is moved right by number of bits specified by right operand and shifted values are filled with zero.

public class ZeroFillRightDemo 
{
   public static void main(String[] args) 
   {
      System.out.println(8 >>> 2); // 000010
   }
}

Output:

2


Relational operator

Relational operators give us the ability to compare two things usually numbers. There are six relational operators,

  • equal to (==)
  • less than (<)
  • greater than (>)
  • not equal to (!=)
  • greater than or equal to (≥)
  • less than or equal to (≤)

The output of relational operator is boolean value, that is, true or false.

public class RelationalOperatorDemo
{
   public static void main(String[] args)
   {
      boolean bool1 = 10 < 6;
      System.out.println(bool1);
      boolean bool2 = 10 > 6;
      System.out.println(bool2);
      boolean bool3 = 23.5 != 15;
      System.out.println(bool3);
      boolean bool4 = 14 * 5.5 >= 67.0 - 42;
      System.out.println(bool4);
      boolean bool5 = 6.8 * 54 <= 654;
      System.out.println(bool5);
      boolean bool6 = 5 * 6 == 6 * 9;
      System.out.println(bool6);
      boolean bool7 = 3 * 2 <= 2 * 6;
      System.out.println(bool7);
      boolean bool8 = 2 * 6 < 6 * 6;
      System.out.println(bool8);
   }
}

Output:

false
true
true
true
true
false
true
true


instanceof operator

It is an operator through which we can test whether an object is an instance of class type, subclass or interface type at runtime.

Also read – comments in java

instanceof operator is also known for casting object at runtime. It returns boolean value “true” if reference is of specified type else “false”.

class Demo
{
   public static void main(String[] args) 
   {
      Demo obj = new Demo();
      System.out.println(obj instanceof Demo);
   }
}

Output:

true


Bitwise operator

Bitwise operator is similar to logic gate. It works on bits and executed bit by bit of a number. Bitwise operator can be used on datatypes int, short, char etc.

Here are bitwise operators in java,

  • Bitwise AND (&) – if both bits are 1, it gives 1, else 0.
  • Bitwise OR (|) – if either of the bits is 1, it gives 1, else 0.
  • Bitwise XOR (^) – if corresponding bits are different, it gives 1, else 0.
  • Bitwise COMPLIMENT(~) – return’s complement, that is, it makes every 0 to 1, and every 1 to 0.
public class BitwiseOperatorDemo 
{
   public static void main(String[] args) 
   {
      int x = 9; 
      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 : " + (~x));
   }
}

Output:

x & y : 1
x | y : 11
x ^ y : 10
~x : -10


Ternary operator

Ternary operator uses three operands and ternary operator is shorthand for if-then-else statement. This operator decides which value must be assigned to variable.

Syntax:

variable var = (expression) ? value if true : value if false

public class TernaryOperatorDemo
{
   public static void main(String[] args)
   {
      int x, y;
      x = 20;
      y = (x == 1) ? 60: 100;
      System.out.println("Value of y : " + y);
      y = (x == 20) ? 60: 100;
      System.out.println("Value of y : " + y);
   }
}

Output:

Value of y : 100
Value of y : 60


Assignment operator

Assignment operators are used to assign some value to variable. It has a combination of left and right. That is, value on left hand side of assignment operator is assigned to variable on right hence left hand side value should be declared before using it.

Here is the list of assignment operators,

  • (+=) : add left operand with right operand and assign it to variable on left. For example, a+= 1 is equal to a = a + 1.
  • (-=) : subtract left operand with right operand and assign it to variable on left. For example, a-= 1 is equal to a = a – 1.
  • (*=) : multiply left operand with right operand and assign it to variable on left. For example, a*= 1 is equal to a = a * 1.
  • (/=) : divide left operand with right operand and assign it to variable on left. For example, a/= 1 is equal to a = a / 1.
  • (%=) : assign modulo of left operand with right operand then assign to variable on left. For example, a%= 1 is equal to a = a % 1.

Let’s see a java program,

public class AssignmentOperatorDemo
{
   public static void main(String[] args)
   {
      int numOne = 60;
      int numTwo = 30;

      numTwo += numOne;
      System.out.println("(+=) : " + numTwo);

      numTwo -= numOne;
      System.out.println("(-=) : " + numTwo);

      numTwo *= numOne;
      System.out.println("(*=) : " + numTwo);

      numTwo /= numOne;
      System.out.println("(/=) : " + numTwo);

      numTwo %= numOne;
      System.out.println("(%=) : " + numTwo);
   }
}

Output:

(+=) : 90
(-=) : 30
(*=) : 1800
(/=) : 30
(%=) : 30


Now let’s see precedence of above discussed operators,

operators in java