Learn operators in java

Hello everyone!! Welcome to flower brackets blog. Today we are going to learn 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 can be used to execute 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 AND operator

Logical operator is used when you want to evaluate more than one condition. Here the condition is, logical AND operator does not check second condition if first condition is false. It checks second condition only if first condition is true.

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

Output:

false

Logical OR operator

Here the condition is, logical OR operator does not check second condition if first condition is true. It checks second condition only if first condition is false.

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

Output: 

true


Shift operator

There are two shift operators,

  • << left shift operator
  • >> right shift operator

a = 8

b = a << 2

Here “a” left shift 2 means, “a” specifies the value to be shifted and 2 specifies number of positions to be shifted in bits. So each bit in “a” will be moved to left side by 2 positions.

public class LeftShiftOperatorDemo 
{
   public static void main(String[] args) 
   {
      int a = 8;
      int b = a << 2;
      System.out.println(b);
   }
}

Output:

32


a = 8

b = a >> 2

Here “a” right shift 2 means, “a” specifies the value to be shifted and 2 specifies number of positions to be shifted in bits. So each bit in “a” will be moved to right side by 2 positions.

public class RightShiftOperatorDemo 
{
   public static void main(String[] args) 
   {
      int a = 8;
      int b = a >> 2;
      System.out.println(b);
   }
}

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 (≤)

In java these operators allow us to take two values and determine whether they fulfill the specified relationship.

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("To check if value of x and y are equal: " + (x == y));
      System.out.println("To check if the value of x and y are not equal: " + (x != y));
      System.out.println("To check if the value of x greater than y: " + (x > y));
      System.out.println("To check if the value of x is lesser than y: " + (x < y));
      System.out.println("To check if the value of x is greater than or equal to y: " + (x >= y));
      System.out.println("To check if the value of x is lesser than or equal to y: " + (x <= y));
   }
}

Output:

Learn Operators In Java


Bitwise operator

Bitwise operator is similar to logic gate. Bitwise operator checks both conditions even if first condition is true or false.

Mainly there are four types of bitwise operator in java,

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

Also read – add two numbers with bitwise operator

public class BitwiseOperatorDemo
{
   public static void main(String[] args)
   {
      int x = 1;
      int y = 5;
      System.out.println("Bitwise AND : " + (x & y));
      System.out.println("Bitwise OR : " + (x | y));
      System.out.println("Bitwise XOR : " + (x ^ y));
      System.out.println("Bitwise NOT : " + (~x));
   }
}

Output:

Learn Operators In Java


Ternary operator

Ternary operator uses three operands and ternary operator is shorthand for if-then-else statement. Now let’s understand ternary operator with an example,

public class TernaryOperatorDemo
{
   public static void main(String[] args)
   {
      int a = 36; 
      int b = 89; 
      int flag = (a < b) ? a : b; 
      System.out.println(flag);
   }
}

Output:

36


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

Learn Operators In Java


Now let’s see the precedence of above discussed operators,

Learn Operators In Java

Related Posts