Operators in java

Let’s learn operators in java.

Operators in java

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

  • Arithmetic operators in java
  • Unary operators in java
  • Logical operator
  • Shift operator
  • Relational operators in java or java relational operators
  • Bitwise operators in java
  • Ternary operator in java or java conditional operator
  • Assignment operator

Arithmetic operators in java

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 operators in java

Unary operator needs only one operand. Using unary operator we can,

  1. unary plus (+) operator: numbers are positive
  2. unary minus (-) operator: negates an expression
  3. increment (++) operator: increments value by 1
  4. decrement (–) operator: decrements value by 1
  5. Logical complement (!) operator: inverts a value

In increment and decrement operator, ++ and — operator can be applied before (prefix) and after (postfix) an operand.

In prefix operation (++a) first value is incremented and new value is stored. But in postfix operation (a++) first value is stored and then value is incremented.

Let’s see java unary operators with examples,

public class JavaUnaryOperator
{
   public static void main(String[] args) 
   {
      int output = +6;
      System.out.println("unary plus (+) operator: " + output);
      output--;
      System.out.println("decrement (--) operator: " + output);
      output++;
      System.out.println("increment (++) operator: " + output);
      output = -output;
      System.out.println("unary minus (-) operator: " + output);
      boolean bool = false;
      System.out.println(bool);
      System.out.println("Logical complement (!) operator: " + !bool);

      int a = 6;
      a++;
      System.out.println(a);
      ++a;
      System.out.println(a);
      System.out.println(++a);
      System.out.println(a++);
      System.out.println(a);
   }
}



Output:

unary plus (+) operator: 6
decrement (–) operator: 5
increment (++) operator: 6
unary minus (-) operator: -6
false
Logical complement (!) operator: true
7
8
9
9
10


Logical Operator

Logical operator helps us understand the result value of two operands X and Y for making a decision. Below are logical operators,

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

Now let’s see logical operators with examples,

Logical AND(&&) operator

If both operands holds non-zero then result will be true and if even one operand is false then the result is false.

Logical OR(||)

If any of the operands holds non-zero then result will be true and if even one operand is true then the result is true.

Logical NOT(!)

Logical NOT(!) operator reverses the result. For example, returns true if the condition is false and returns false if condition is true.

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(unsigned right shift)

<< left shift operator

In << left shift operator bits of number is shifted to the left and fills 0 on void places.

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



Output:

-80
24


>> bitwise (signed) right shift operators in java

In >> right shift operator bits of number is shifted to right and fills 0 on void places.

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



Output:

-10
3


unsigned right shift (>>>) operator

In unsigned right shift (>>>) operator bits of number is shifted to the right and fills 0 on void. Here leftmost bit is set to zero and will extend sign bit.

public class UnsignedRightShiftDemo
{
   public static void main(String[] args) 
   {
      int a = 6;
      int b = -20;
      System.out.println(b >>> 1);
      System.out.println(a >>> 1);
   }
}



Output:

2147483638
3


Relational operator or java relational operators

Relational operators give us the ability to compare two operands for equality, greater than, less than etc., and return boolean output. There are six relational operators,

  • equal to (==): check whether two given operands are equal or not.
  • less than (<): check whether first operand is less than second operand.
  • greater than (>): check whether first operand is greater than second operand.
  • not equal to (!=): function of this operator is opposite of equal-to operator.
  • greater than or equal to (≥): checks whether first operand is greater than or equal to second operator or not.
  • less than or equal to (≤): checks whether first operand is lesser than or equal to second operator or not.

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

This is an operator through which we can test whether an object is an instance of class type, subclass type 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 returns “false”.

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



Output:

true


Bitwise operators in java

Bitwise operator works on bits and executes bit by bit of a number. Bitwise operator can be used on datatypes like int, short, char etc.

Bitwise operator is similar to logic gate. Here are bitwise operation java operators,

  • 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 or conditional operators in java

Ternary operator or java conditional operator decides which value must be assigned to variable based on condition or expression. This operator is shorthand for if-then-else statement.

Java conditional operator or ternary operator uses three operands, condition which evaluates to true or false, value that is returned if condition is true and value that is returned if condition is false.

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 java assignment operator with examples,

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