JAVA Operators

An operator is a symbol in programming language which represent a specific task or operation or operation which performed on operands. e.g. symbol + (pronounced as plus) is an arithmetic operator which adds two numbers such as the below statement.

4+5=9
 here 4 and 5 are called operands and symbols + is called operator and it operates on two numbers and produce sum.

Above examples fully explains the concept of operator. Actually operator creates some specific relationship between the operands and produce some useful result. Each kind of operator is represented by a specific symbol and have specific task or action

There many kinds of operator which are used in JAVA to create useful statement.
these are divided into following categories. 

  • Arithmetic Operators
  • Logical Operators
  • Assignment Operators
  • Relational Operators
  • Bitwise Operators (Will be discussed at some later stage as these are not needed much more)
  • Misc Operators

The Arithmetic Operators

Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators −
Assume integer variable A holds 10 and variable B holds 20, then −

Operator Description Example
+ (Addition) Adds values on either side of the operator. A + B will give 30
- (Subtraction) Subtracts right-hand operand from left-hand operand. A - B will give -10
* (Multiplication) Multiplies values on either side of the operator. A * B will give 200
/ (Division) Divides left-hand operand by right-hand operand. B / A will give 2
% (Modulus) Divides left-hand operand by right-hand operand and returns remainder. B % A will give 0
++ (Increment) Increases the value of operand by 1. B++ gives 21
-- (Decrement) Decreases the value of operand by 1. B-- gives 1


Logical Operators

Logical Operators are used with binary variables. They are mainly used in conditional statements and loops for evaluating a condition.
Logical operators in java are: &&, ||, !
Let’s say we have two boolean variables b1 and b2.
b1&&b2 will return true if both b1 and b2 are true else it would return false.
b1||b2 will return false if both b1 and b2 are false else it would return true.
!b1 would return the opposite of b1, that means it would be true if b1 is false and it would return false if b1 is true.

Example of Logical Operators

public class LogicalOperatorDemo {
   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 && b2): " + !(b1&&b2));
   }
}
Output:
b1 && b2: false
b1 || b2: true
!(b1 && b2): true

Assignment Operators

Assignments operators in java are: =, +=, -=, *=, /=, %=
num2 = num1 would assign value of variable num1 to the variable.
num2+=num1 is equal to num2 = num2+num1
num2-=num1 is equal to num2 = num2-num1
num2*=num1 is equal to num2 = num2*num1
num2/=num1 is equal to num2 = num2/num1
num2%=num1 is equal to num2 = num2%num1

Example of Assignment Operators

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

      num2 = num1;
      System.out.println("= Output: "+num2);

      num2 += num1;
      System.out.println("+= Output: "+num2);
       
      num2 -= num1;
      System.out.println("-= Output: "+num2);
       
      num2 *= num1;
      System.out.println("*= Output: "+num2);
       
      num2 /= num1;
      System.out.println("/= Output: "+num2);
       
      num2 %= num1;
      System.out.println("%= Output: "+num2);
   }
}
Output:
= Output: 10
+= Output: 20
-= Output: 10
*= Output: 100
/= Output: 10
%= Output: 0

The Relational or Comparison Operators

There are following relational operators supported by Java language.

Assume variable A holds 10 and variable B holds 20, then −


Operator Description Example
== (equal to) Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is not true.
!= (not equal to) Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true.
> (greater than) Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true.
< (less than) Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true.
>= (greater than or equal to) Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true.
<= (less than or equal to) Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.


Miscellaneous Operator

Ternary Operator

This operator evaluates a boolean expression and assign the value based on the result.
Syntax:
variable num1 = (expression) ? value if true : value if false
If the expression results true then the first value before the colon (:) is assigned to the variable num1 else the second value is assigned to the num1.

Example of Ternary Operator

public class TernaryOperatorDemo {

   public static void main(String args[]) {
        int num1, num2;
        num1 = 25;
        /* num1 is not equal to 10 that's why
	 * the second value after colon is assigned
	 * to the variable num2
	 */
	num2 = (num1 == 10) ? 100: 200;
	System.out.println( "num2: "+num2);

	/* num1 is equal to 25 that's why
	 * the first value is assigned
	 * to the variable num2
	 */
	num2 = (num1 == 25) ? 100: 200;
	System.out.println( "num2: "+num2);
   }
}
Output:
num2: 200
num2: 100

Bitwise Operators

There are six bitwise Operators: &, |, ^, ~, <<, >>
num1 = 11; /* equal to 00001011*/
num2 = 22; /* equal to 00010110 */
Bitwise operator performs bit by bit processing.
num1 & num2 compares corresponding bits of num1 and num2 and generates 1 if both bits are equal, else it returns 0. In our case it would return: 2 which is 00000010 because in the binary form of num1 and num2 only second last bits are matching.
num1 | num2 compares corresponding bits of num1 and num2 and generates 1 if either bit is 1, else it returns 0. In our case it would return 31 which is 00011111
num1 ^ num2 compares corresponding bits of num1 and num2 and generates 1 if they are not equal, else it returns 0. In our example it would return 29 which is equivalent to 00011101
~num1 is a complement operator that just changes the bit from 0 to 1 and 1 to 0. In our example it would return -12 which is signed 8 bit equivalent to 11110100
num1 << 2 is left shift operator that moves the bits to the left, discards the far left bit, and assigns the rightmost bit a value of 0. In our case output is 44 which is equivalent to 00101100
Note: In the example below we are providing 2 at the right side of this shift operator that is the reason bits are moving two places to the left side. We can change this number and bits would be moved by the number of bits specified on the right side of the operator. Same applies to the right side operator.
num1 >> 2 is right shift operator that moves the bits to the right, discards the far right bit, and assigns the leftmost bit a value of 0. In our case output is 2 which is equivalent to 00000010

Example of Bitwise Operators

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

     int num1 = 11;  /* 11 = 00001011 */
     int num2 = 22;  /* 22 = 00010110 */
     int result = 0;

     result = num1 & num2;   
     System.out.println("num1 & num2: "+result);

     result = num1 | num2;   
     System.out.println("num1 | num2: "+result);
    
     result = num1 ^ num2;   
     System.out.println("num1 ^ num2: "+result);
    
     result = ~num1;   
     System.out.println("~num1: "+result);
    
     result = num1 << 2;   
     System.out.println("num1 << 2: "+result); result = num1 >> 2;   
     System.out.println("num1 >> 2: "+result);
  }
}
Output:
num1 & num2: 2
num1 | num2: 31
num1 ^ num2: 29
~num1: -12
num1 << 2: 44 num1 >> 2: 2

Auto-increment and Auto-decrement Operators

++ and —
num++ is equivalent to num=num+1;
num–- is equivalent to num=num-1;

Example of Auto-increment and Auto-decrement Operators

public class AutoOperatorDemo {
   public static void main(String args[]){
      int num1=100;
      int num2=200;
      num1++;
      num2--;
      System.out.println("num1++ is: "+num1);
      System.out.println("num2-- is: "+num2);
   }
}
Output:
num1++ is: 101
num2-- is: 199

Operator Precedence in Java

This determines which operator needs to be evaluated first if an expression has more than one operator. Operator with higher precedence at the top and lower precedence at the bottom.
Unary Operators
++  – –  !  ~
Multiplicative
*  / %
Additive
+  –
Shift
<<  >>  >>>
Relational
>  >=  <  <=
Equality
==  !=
Bitwise AND
&
Bitwise XOR
^
Bitwise OR
|
Logical AND
&&
Logical OR
||
Ternary
?:
Assignment
=  +=  -=  *=  /=  %=  >  >=  <  <=  &=  ^=  |=