Learn operators in java

Hello everyone!! Welcome to flower brackets blog. Today we are going to learn operators in java.

Operator is nothing but a symbol to perform operations. Here are types of operators in java,

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

Arithmetic Operator

These are some of the important arithmetic operators in java,

// "+" operator
public class SampleExample
{
   public static void main(String[] args)
   {
      int a = 5;
      int b = 9;
      int result;
      result = a + b;
      System.out.println(result);
   }
}

Output: 14

// "-" operator
public class SampleExample
{
   public static void main(String[] args)
   {
      int a = 10;
      int b =  5;
      int result;
      result = a - b;
      System.out.println(result);
   }
}

Output: 5

// "/" operator
public class SampleExample
{
   public static void main(String[] args)
   {
      int a = 10;
      int b =  5;
      int result;
      result = a / b;
      System.out.println(result);
   }
}

Output: 2

// "%" operator
public class SampleExample
{
   public static void main(String[] args)
   {
      int a = 10;
      int b =  5;
      int result;
      result = a % b;
      System.out.println(result);
   }
}

Output: 0


Unary operator

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

// post-increment operator
public class SampleExample
{
   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 SampleExample
{
   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.


Logical operator

Logical operator is used when you want to evaluate more than one condition.

// && -> AND logical operator
public class SampleExample
{
   public static void main(String[] args)
   {
      int num1 = 40;
      int num2 = 60;
      if((num1 >= 35) && (num2 >= 35))
      {
         System.out.println("condition is true!");
      }
      else
      {
         System.out.println("condition is false!");
      }
   }
}

Output: condition is true!

// || -> OR logical operator
public class SampleExample
{
   public static void main(String[] args)
   {
      int num1 = 20;
      int num2 = 25;
      if((num1 >= 35) || (num2 >= 35))
      {
         System.out.println("condition is true!");
      }
      else
      {
         System.out.println("condition is false!");
      }
   }
}

Output: condition is false!


Shift operator

There are two shift operators,

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

Let us understand left shift operator with an example,

Left Shift

m = 00110011
n = 2
m << n = 11001100

Here “m” left shift “n” means, “m” specifies the value to be shifted and “n” specifies number of positions in bits to be shifted.

So each bit in “m” will be moved to left side by 2 positions. And the output is m << n = 11001100.

// << - Left shift operator
public class SampleExample
{
   public static void main(String[] args)
   {
      int x = 51;
      System.out.println("Binary value of x is : " + Integer.toBinaryString(x) + "\n");
      int y = 2;
      int result = x << y;
      System.out.println("Binary value after left shift(<<) operator : " + Integer.toBinaryString(result));
      System.out.println("Result : " + result);
   }
}

Output:

Learn Operators In Java

Now let’s understand right shift operator,

Right Shift

m = 11001100
n = 2
m >> n = 00110011

Here “m” right shift “n” means, “m” specifies the value to be shifted and “n” specifies number of positions in bits to be shifted.

So each bit in the “m” will be moved to right side by 2 positions. And the output is m >> n = 00110011.

// >> - Right shift operator
public class SampleExample
{
   public static void main(String[] args)
   {
      int x = 204;
      System.out.println("Binary value of x is : " + Integer.toBinaryString(x) + "\n");
      int y = 2;
      int result = x >> y;
      System.out.println("Binary value after right shift(>>) operator : " + Integer.toBinaryString(result));
      System.out.println("Result: " + result);
   }
}

Output:

Learn Operators In Java


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 RelationalDemo
{
   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. In this we use the same truth table that we use with logic gates.

Mainly there are four types of bitwise operator in java,

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

Also read – add two numbers with bitwise operator

public class BitwiseDemo
{
   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 TernaryDemo
{
   public static void main(String[] args)
   {
      int a = 10;
      int b = 20;
      int result;
      boolean flag = true;
      result = flag ? a : b;
      System.out.println("If flag is true: " + result);
      flag = false;
      result = flag ? a : b;
      System.out.println("If flag is false: " + result);
   }
}

Output:

Learn Operators In Java

As you can see in the above example if boolean flag is true, we will assign the value of “a” to result. Else assign the value of “b” to result.


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

Related Posts