In Java Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result.
Java supports various operators, and they can be classified into following categories.

  1. Arithmetic Operators
  2. Increment and Decrement Operators
  3. Relational Operators
  4. Logical Operators
  5. Assignment Operators
  6. Ternary Conditional Operator
  7. Bitwise Operator

Arithmetic Operators in Java

In Java we use Arithmetic Operators to perform basic operations like addition, subtraction, multiplication and division.
They are used in mathematical expressions, in the same way they are used in algebra. Some of the operators are

  • Addition Operator :-
    This operator is used to add two integer values. Like we add two values:
    a = 5 and b = 10 so, a+b = 15 or b+a = 15
  • Subtraction Operator :-
    This operator will subtract the left value from the right value. Like:
    a – b where a = 20 and b = 10 so the result will be 10.
  • Multiplication Operator :-
    In this operator it will multiply the two values. Like:
    a*b or b*a such as a = 2 and b = 4 so the result will be a*b = 8 and b*a = 8.
  • Division Operator :-
    In this operator the left hand operand is divided by the right hand operand, based on the value which variable value is greater. Like:
    a = 10 and b = 5 so, a/b = 10/5 = 2.
  • Modulus Operator :-
    In this operator the left hand operand is divided by the right hand operand and it returns the remainder, based on the value which value is greater. Like:
    a%b where a = 10 and b = 5 so, a%b = 0, the remainder will be 0.

Example Java program on Arithmetic Operators

public class arithmetic{ 
        public static void main(String[] args) {
               System.out.println("Some arithmetic operators are:");
               
               int a = 5 + 10;
               int b = a - 10;
               int c = b * 5;
               int d = c / 5;
               
               System.out.println("Value of addition = " +a );
               System.out.println("Value of subtraction = " + b);
               System.out.println("Value of multiplication = " + c);
               System.out.println("Value of division = " + d);
        }
}

Increment and Decrement Operators

In Java, we use increment and decrement operators to increase and decrease the value.

  • Increment Operator – In this operator the value will be incremented by 1. Like, a = 3 so after increment a++ the value of a will be 4.
  • Decrement Operator – In this operator the value will be decremented by 1. Like, a = 7 so after decrement a- – the value of a will be 6.

Example Java program on Increment and Decrement Operators

public class incredecre{
         public static void main(String[] args) {          
                 int a = 11;
                 int b = 10;
                 a++;
                 ++b;
                 
                 System.out.println("a = " +a);
                 System.out.println("b = " + b);
                 
                 int c = a--;
                 int d = --b;
                 
                 System.out.println("c = " + c);
                 System.out.println("d = " + d);
              }
       }

Relational Operators

In Java we use relational operator to compare 2 variable values Below are some of the Java’s relational operators

  • Equal to (= =) – In this operator it checks whether both the values are equal are not, if both the values are equal then the condition is true. Like a = 10 and b = 10 so a= = b, the condition is true.
  • Not equal to (!=) – In this operator it also checks whether both the values are equal or not but if the values are not equal then the condition is true. Like a= 10 and b = 20 so a != b, the condition is true.
  • Greater than (>) – In this operator it checks that the left operand value is greater than the right operand or not. If the value is greater than the condition is true, if it is not than the condition is false. Like a = 20 and b = 10 so a>b then the condition is true.
  • Less than (<) – In this operator it checks whether the left operand value is less than the right operand. If the value is less than the right operand then the condition is true. Like a = 10 and b = 20 so a<b then the condition is true.
  • Greater than or equal to (>=) – In this operator it checks whether the left operand value is greater than or equal to the right operand and if it is then the condition is true. Like a = 30 and b = 20 so a>= b so the condition is true.
  • Less than or equal to (<=) – In this operator it checks whether the left operand value is less than or equal to the right operand and if it is true then the condition is true. Like a = 20 and b = 30 so a<=b so the condition is true

Let us have a look on the example in Java

public class relational {
   public static void main(String args[]) {
      int x = 30;
      int y = 20;
      System.out.println(" The value of x == y is =  " +(x==y) );
      System.out.println("The value of x != y is = " + (x != y) );
      System.out.println("The value of x &gt; y is = " + (x &gt; y) );
      System.out.println("The value of x &lt; y is = " + (x &lt; y) ); System.out.println("The value of x &gt;= y is = " + (x &gt;= y) );
      System.out.println("The value of x &lt;= y is = " + (x &lt;= y) );
   }
}

Logical Operators

The logical operators are also known as Boolean operator where it operates only on Boolean operands and return Boolean value. In this we combine two or more condition into a single expression to form a resultant Boolean value, i.e. true to false or vice versa. Some of the logical operators provided in Java are:

  • Logical AND – In the logical AND operator it returns the value 1 if both the operands value are 1 and 0 if the values are not same.
    For example:
    1010     //binary conversion of 10
    0111     //binary conversion of 7
    0010     //binary conversion of 2
  • Logical OR – In the logical OR operator it returns the value 1 if either of the value of the operand is 1 and if not then the value will be 0. For example:
    1010    //binary conversion of 10
    0110    //binary conversion of 6
    1110    //binary conversion of 14
  • Logical NOT – In the logical NOT operator it will convert the value it will make 1 to 0 and vice versa.
    For example:
    1010   //binary conversion of 10
    It will convert this value into:
    0101   //binary conversion of 5
  • Logical XOR – In the logical XOR operator if there is exactly one operand value 1 then it will return 1 otherwise the result will be 0.
    For example:
    1010   //binary conversion of 10
    0111   //binary conversion of 7
    1101   //binary conversion of 13

Let us have a look on the Java example on Logical Operator:

public class logical {
   public static void main(String args[]) {
      boolean a = true;
      boolean b = false;
      System.out.println("a &amp;&amp; b = " + (a&amp;&amp;b));
      System.out.println("a || b = " + (a||b) );
      System.out.println("!(a &amp;&amp; b) = " + !(a &amp;&amp; b));
      System.out.println("a ^ b = " + (a ^ b));
   }
}

Assignment Operators

The assignment operators are used to assign the value of a given expression to a variable. Some of the assignment operators in Java are:

  • Add and assign (+ =) – This operator is used to add the right operand to the left operand and assign the result to the left operand
  • Subtraction and assign (- =) – This operator is used to multiply the right operand from the left operand and assign the result to the left operand
  • Divide and assign (/=) – This operator is used to divide the left operand with the right operand and assign the result to the left operand
  • Modulus and assign (%=) – This operator is used to divide the left operand with right operand and assign the remainder as a result to the left operand
  • Shift left and assign (<<=) – This operator is used to shift the bits to left and assign the result to the operand
  • Shift right and assign (>>=) – This operator is used to shift the bits to right and assign the result to the operand

Let us have a look on the example:

public class Test {
   public static void main(String args[]) {
      int a = 20;
      int b = 10;
      int c = 0;

      c += a ;
      System.out.println("c += a  = " + c );

      c -= a ;
      System.out.println("c -= a = " + c );

      c *= a ;
      System.out.println("c *= a = " + c );

      a = 20;
      c = 15;
      c /= a ;
      System.out.println("c /= a = " + c );

      a = 20;
      c = 15;
      c %= a ;
      System.out.println("c %= a  = " + c );

      c &lt;&lt;= 2 ;
      System.out.println("c &lt;&lt;= 2 = " + c ); c &gt;&gt;= 2 ;
      System.out.println("c &gt;&gt;= 2 = " + c );    
   }
}

Bitwise Operators

There are many bitwise operators defined by Java which are used in the integer types like int, short, long etc. The bitwise operator works on bits and performs bit by bit operation. The java provides some of the bitwise operators like AND bitwise operation, OR bitwise operation, XOR bitwise operation, Complement operation, etc.

  • AND bitwise operation – The AND bitwise operator is similar to AND operator where it returns 1 when both the operands are 1 otherwise if the values are different then it returns 0. The difference in logical AND operator and AND bitwise operator is the sign, the AND operator is denoted by && whereas AND bitwise operator is denoted with &.
  • OR bitwise operation – The OR bitwise operator is similar to logical OR operator where it returns 1 if either of the value is 1 and if both the values of the operands are same then it returns 0. The difference between logical OR operator and OR bitwise operator is of sign, the logical OR operator is denoted with || whereas the OR bitwise operator is denoted with |.
  • XOR bitwise operation – The XOR bitwise operator will return 1 where both the values of the operands are different and will return 0 when both the values are same. The XOR bitwise operator can be denoted by using ^.
  • Compliment bitwise operation – The unary compliment bitwise operator changes the bits from 0 to 1 and 1 to 0. This operator can be applied only on integer types; it cannot be used on Boolean types. This operator can be denoted by using ~.
  • Fill right shift zero – In the fill right shift zero operator the left operand value is shifted right by the number of bits specified by the right operand and those shifted values are filled with zeros. The fill right shift zero can be denoted by >>>.
  • Left shift operation – In the left shift operator the values are moved left by the number of bits specified by the right operand. The left shift operator is denoted by <<.
  • Right shift operation – In the right shift operator the left operand value is shifted right by the number of bits specified by the right operand. The right shift operator is denoted by >>.

Let us understand these operators though an example:

public class bitwise{
   public static void main(String args[]) {
      int a = 10;
      int b = 5;	
      int c = 0;

      c = a &amp; b;        
      System.out.println("AND operation a &amp; b = " + c );

      c = a | b;        
      System.out.println("OR operation a | b = " + c );

      c = a ^ b;       
      System.out.println("XOR operation a ^ b = " + c );

      c = ~a;           
      System.out.println("Complement operation ~a = " + c );

      c = a &lt;&lt; 3;       
      System.out.println("Left shift a &lt;&lt; 3 = " + c ); c = a &gt;&gt; 3;      
      System.out.println("Right shift a &gt;&gt; 3  = " + c );

      c = a &gt;&gt;&gt; 3;     
      System.out.println("Zero fill right shift a &gt;&gt;&gt; 3 = " + c );
   }
}

Ternary Conditional Operator

The ternary conditional operator is used in the conditional expressions. This operator consists of three operands which are presented in this form:
exp1 ? exp2 : exp3
In the above expression the first operand represents the condition which is evaluated and it must be either true which is represented by second operand exp2 or false which is represented by exp3.

Let us have a look on the example:

public class ternary{
   public static void main(String args[]) {
      int a, b;
      a = 1;
      b = (a == 1) ? 2: 3;
      System.out.println( "The value of b is : " +  b );
      b = (a == 0) ? 2: 3;
      System.out.println( "The value of b is : " + b );
   }
}