Operators in java

Hello everyone!! 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 namely division, addition, subtraction and multiplication.

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

Output :

30
20
125
5
0


Unary operator

Here we learn post-increment and pre-increment operations.

// post-increment operator
public class UnaryPostIncrementDemo
{
   public static void main(String[] args)
   {
      int a = 5;
      a++;
      System.out.println(a);
   }
}

Output: 6

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.

// pre-increment operator
public class UnaryPreIncrementDemo
{
   public static void main(String[] args)
   {
      int a = 5;
      ++a;
      System.out.println(a);
   }
}

Output: 6

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

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. Here are few logical operators,

  • && – logical AND
  • || – logical OR
  • ! – logical NOT

Logical AND operator

If both operands (X and Y) have same value or non-zero value then result will be false.

Logical OR operator

If any of two operands (X and Y) holds non-zero value then result will be true.

Logical NOT operator

This operator reverses value of operand. For example, if X = true then logical NOT of “X” will be false.

public class LogicalOperatorDemo 
{
   public static void main(String[] args) 
   {
      int num1 = 50; 
      int num2 = 10; 
      int num3 = 70; 
      System.out.println(num1 < num2 && num1 < num3);
      System.out.println(num1 < num2 || num1 < num3);
      System.out.println(!(num1 < num2));
   }
}

Output:

false
true
true


Shift operator

  • << left shift operator
  • >> right shift operator
  • >>> zero fill right shift operator
public class LeftShiftOperatorDemo 
{
   public static void main(String[] args) 
   {
      System.out.println(8 << 2); // 100000
   }
}

Output:

32

Here 8 left shift 2 means, left operand value is moved left 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 right shift 2 means, left operand value is moved by right by number of bits specified by right operand.


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

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.


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)
   {
      int x = 10;
      int y = 20;
      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 < y) : " + (x < y));
      System.out.println("(x >= y) : " + (x >= y));
      System.out.println("(x <= y) : " + (x <= y));
   }
}

Output:

(x == y) : false
(x != y) : true
(x > y) : false
(x < y) : true
(x >= y) : false
(x <= y) : true


instanceof operator

It is an operator through which we can test whether the object is an instance of class type, subclass or interface type. instanceof operator is also known as type comparison operator.

reference variable object  instanceof  class or interface type

NOTE: If a class extends another class then object of subclass is also type of parent class.

class Shape
{

}
public class Square extends Shape
{
   public static void main(String[] args) 
   {
      Shape obj = new Square();
      boolean output = obj instanceof Square;
      System.out.println(output);
   }
}

Output:

true


Bitwise operator

Bitwise operator is similar to logic gate. It works on bits and executed bit by bit. Here are types of bitwise operators in java,

  • AND (&)
  • OR (|)
  • XOR (^)
  • COMPLIMENT(~)

It can applied to char, byte, short, long and integer datatypes.

Also read – add two numbers with bitwise operator

public class BitwiseOperatorDemo 
{
   public static void main(String[] args) 
   {
      int x = 80;
      int y = 20;
      int z = 0;
      z = x & y;
      System.out.println((z));
      z = x | y;
      System.out.println((z));
      z = x ^ y;
      System.out.println((z));
      z = ~x;
      System.out.println((z));
   }
}

Output:

16
84
68
-81


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 mainly used to assign some value to variable. It may be after doing operation to the value you assigned. Here is the list of assignment operators,

  • equal to (=)
  • add and assign (+=)
  • subtract and assign (-=)
  • multiply and assign (*=)
  • divide and assign (/=)
  • modulus and assign (%=)
  • left shift and assign (<<=)
  • right shift and assign (>>=)
  • bitwise AND assignment(&=)
  • bitwise exclusive OR and assign (^=)
  • bitwise inclusive OR and assign (|=)

Let’s see an example,

public class AssignmentOperatorDemo
{
   public static void main(String[] args)
   {
      int a = 10;
      int b = 20;
      int assign = 30;
      
      int c = 40;
      c += a;
 
      int d = 50;
      d -= b;
 
      int e = 100;
      e *= a;
 
      int f = 3000;
      f /= assign;
 
      int g = 300;
      g %= b;
 
      int h = 21;
      h <<= 3;
 
      int i = 17;
      i >>= 2;
 
      int j = 13;
      j &= 13;
 
      int k = 21;
      k ^= 14;
 
      int l = 29;
      l |= 12;
 
      System.out.println("Equal : " + assign);
      System.out.println("Add and assign : " + c);
      System.out.println("Subtract and assign : " + d);
      System.out.println("Multiply and assign : " + e);
      System.out.println("Divide and assign : " + f);
      System.out.println("Modulus and assign : " + g);
      System.out.println("Left shift and assign : " + h);
      System.out.println("Right shift and assign : " + i);
      System.out.println("Bitwise AND and assign : " + j);
      System.out.println("Bitwise exclusive OR and assign : " + k);
      System.out.println("Bitwise inclusive OR and assign : " + l); 
   }
}

Output:

Equal : 30
Add and assign : 50
Subtract and assign : 30
Multiply and assign : 1000
Divide and assign : 100
Modulus and assign : 0
Left shift and assign : 168
Right shift and assign : 4
Bitwise AND and assign : 13
Bitwise exclusive OR and assign : 27
Bitwise inclusive OR and assign : 29


Now let’s see precedence of above discussed operators,

operators in java

Related Posts