Operators

Java provides many types of operators which are given below:

  • Unary operators
  • Arithmetic operators
  • Assignment operators
  • Relational operators
  • Logical operators
  • Shift operators
  • Ternary Operators
  • Bitwise operators
  • The instanceof operator

1. Unary operators
The unary operators are require only one operand to perform operation. They perform various operation like increment/decrement value by one, shift operation or inverse the value of a boolean etc.
Lets see an example:

class UnaryTest {

  public static void main(String args[]) {
    int num = 20;

    System.out.println("num++ :" + num++);
    System.out.println("num-- :" + num--);
    System.out.println("++num :" + ++num);
    System.out.println("--num :" + --num);
    System.out.println("-num :" + -num);
    System.out.println("+num :" + +num);
    System.out.println("~num :" + ~num);

  }
}
Output:

num++ :20
num– :21
++num :21
–num :20
-num :-20
+num :20
~num :-21

2. Arithmetic operators
Arithmetic operators are used to perform arithmetic operations on primitive data types.
Lets see an example:

class ArithmeticTest {

  public static void main(String args[]) {
    int num1 = 20;
    int num2 =5;

    System.out.println("num1 + num2 :" + (num1 + num2));
    System.out.println("num1 - num2 :" + (num1 - num2));
    System.out.println("num1 * num2 :" + (num1 * num2));
    System.out.println("num1 / num2 :" + (num1 / num2));
    System.out.println("num1 % num2 :" + (num1 % num2));

  }
}
Output:

num1 + num2 :25
num1 – num2 :15
num1 * num2 :100
num1 / num2 :4
num1 % num2 :0

3. Assignment operators
Assignment operators are used to assign the value in the variable. It has a right to left associativity, i.e the value given on right side is assigned to the variable of left side.
Lets see an example:

class AssignmentTest {

  public static void main(String args[]) {
    int num1 = 20;
    int num2 =5;

    System.out.println("num1+=num2 :" + (num1 += num2));
    System.out.println("num1-=num2 :" + (num1 -= num2));
    System.out.println("num1*=num2 :" + (num1 *= num2));
    System.out.println("num1/= num2 :" + (num1 /= num2));
    System.out.println("num1%=num2 :" + (num1 %= num2));

  }
}
Output:

num1+=num2 :25
num1-=num2 :20
num1*=num2 :100
num1/= num2 :20
num1%=num2 :0

4. Relational operators
Relational operators are used to check for relations like greater than, less than, equity etc.
Lets see an example:

class RelationalTest {

  public static void main(String args[]) {
    int num1 = 20;
    int num2 =5;

    System.out.println("num1==num2 :" + (num1==num2));
    System.out.println("num1>num2 :" + (num1>num2));
    System.out.println("num1=num2 :" + (num1>=num2));
    System.out.println("num1<=num2 :" + (num1<=num2)); System.out.println("num1!=num2 :" + (num1!=num2));
  }
}
Output:

num1==num2 :false num1>num2 :true
num1=num2 :true
num1<=num2 :false
num1!=num2 :true

5. Logical operators
A logical operator in Java is an operator that returns a boolean value based on boolean result of one or two other expression.
Lets see an example:

class RelationalTest {

  public static void main(String args[]) {
    boolean b1 = true;
    boolean b2 = false;

    System.out.println("b1 && b2:" + (b1&&b2));
    System.out.println("b1 || b2:" + (b1||b2));
    System.out.println("!b1 :" + (!b1));
  }
}
Output:

b1 && b2:false
b1 || b2:true
!b1 :false

6. Shift operators
Shift operators are used to shift the individual bits of an integer by a specified integer amount.
Left Shift Operator:
The Java left shift operator is used to shift all the bits to the left side.
Lets see an example:

class LeftShiftTest {

  public static void main(String args[]) {
    int a = 20;
    int b = 5;

    System.out.println("a<<b:" + (a<<b));
  }
}
Output:

a<<b:640

Right Shift Operator:

Java support below two right shift operators.

1. Signed Right Shift(>>) Operators

This shift operator ‘>>’ uses the sign bit(left most bit) to fill the trailing position after shift. If the given number is positive then 0 is used as left side filler and if the number is negative then 1 is used as filler. If binary representation of the the number is 00………..11100(28) then right shifting by 2 using >> will make it 00…………111(7).

Lets see an example:

class SignedRightShiftTest {
  public static void main(String args[]) {

    int num1 = 28;
    System.out.println(num1>>2);

    int num2 = -4;
    System.out.println(num2>>2);
  }
}
Output:

7
-1

2. Unsigned Right Shift(>>) Operators
In Java Unsigned Right Shift Operator(>>) always fills 0 irrespective of the sign of the number. The value of n>>>s is n right-shifted s bit positions with zero-extension.

Lets see an example:

class UnsignedRightShiftTest {
  public static void main(String args[]) {

    int num1 = -28;
    System.out.println(Integer.toBinaryString(num1));

    // Right shifted 16 bit position with zero-extension.
    System.out.println(Integer.toBinaryString(num1 >>> 16));

  }
}
Output:

11111111111111111111111111100100
1111111111111111

7. Ternary operators
Ternary operator is also known as conditional operator which can be used as an alternative of if-then-else statement. The First operand in Java ternary operator is basically a boolean or a statement with the boolean result. If the first operand is true then it returns second operand else it return third operand. For example, return age >= 18 ? “Eligible for voting” : “Not eligible for voting”.

Lets see an example:

class TernaryTest {
  public static void main(String args[]) {

    int age = 19;
    System.out.println(age >= 18 ? "Eligible for voting" : "Not eligible for voting");

    age = 17;
    System.out.println(age >= 18 ? "Eligible for voting" : "Not eligible for voting");

  }
}
Output:

Eligible for voting
Not eligible for voting

8. Bitwise operators
In Java, Bitwise operators allow manipulating data at bit level. Bitwise operators can be further classified into two categories. Bitwise logical operators and Bitwise shift operators.

Bitwise Logical Operators : Bitwise logical operators are given below.
& Bitwise AND Operator
| Bitwise OR Operator
^ Bitwise Exclusive OR Operator
~ Bitwise NOT Operator

Lets see an example:

class BitwiseLogicalTest {
  public static void main(String args[]) {

    int num1 = 20;
    int num2 = 10;

    // Binary representation
    // num1 = 0001 0100
    // num2 = 0000 1010

    // num1 & num2: 0000 0000
    System.out.println("num1 & num2: " + (num1 & num2));

    // num1 | num2: 0001 1110
    System.out.println("num1 | num2: " + (num1 | num2));

    // num1 ^ num2: 0001 1110
    System.out.println("num1 ^ num2: " + (num1 ^ num2));

    // ~num1: 1110 1011
    System.out.println("~num1: " + ~num1);

  }
}
Output:

num1 & num2: 0
num1 | num2: 30
num1 ^ num2: 30
~num1: -21

Bitwise Shift Operators: Bitwise shift operators are given below.
>> Right Shift Operator
<< Left Shift Operator >>> Right Shift with zero fill operator(Unsigned Right Shift Opearator)
Note: We have discussed these operators above at point 6.

9. The instanceof operators
The Java instanceof operator is used to test whether the object is an instance of the specified class or subclass or interface.

class InstanceOfTest {
	public static void main(String args[]) {

		String str = new String("Test");
		// return true as str is an instance of String
		System.out.println(str instanceof String);

	}
}
Output:

true