Operators in Java With Examples

In programming, data processing and manipulation is highly dependent on the operators available. The operators in Java can be divided into the following groups:

  • Unary Operator: The operators applicable to one operand is called a unary operator.
  • Binary Operator: The operators applicable to exactly two operands is called a binary operator.
  • Ternary Operator: It is a linear replacement to if-else, and is the only conditional operator applicable on 3 operands.

Unary Operator

The operations for which a unary operator is applicable are:

  • Incrementing the value by 1
  • Decrementing the value by 1
  • Inverting the Boolean
  • Negating an expression

Let us look into the operators

Operator Name Expression
Pre-increment/Pre-decrement ++value, --value
Post-increment/Post-decrement value++ ,value--
Inversion ~value
Negation !value

Pre-increment/Pre-decrement:

In the case of pre-increment/decrement, the value is incremented/decremented by 1, saved into memory, and then used, wherever required.

Example:

public class MyOperators{
     public static void main(String []args){
        int myInt = 5;
        int myInt2 = 10;

        System.out.println("Value of myInt is "+myInt);
        ++myInt;
        System.out.println("Value of myInt after pre increment is "+ myInt);
        System.out.println();
        System.out.println("Value of myInt2 is "+myInt2);
         --myInt2;
        System.out.println("Value of myInt after pre decrement is "+myInt2);
     }
}

Output:

Value of myInt is 5
Value of ++myIntis 6

Value of myInt2 is 10
Value of --myInt2 is 9

Post-increment/Post-decrement:

In case of post-increment/decrement, is first used wherever required, and then the value is incremented/decremented by 1, saved into memory.

Example:

public class MyOperators{
     public static void main(String []args){
        int myInt = 5;
        int myInt2 = 10;

        System.out.println("Value of myInt is "+myInt);
        myInt++;
        System.out.println("Value of myInt after post increment is "+ myInt);
        System.out.println();
        System.out.println("Value of myInt2 is "+myInt2);
         myInt2--;
        System.out.println("Value of myInt after post decrement is "+myInt2);
     }
}

Output:

Value of myInt is 5
Value of ++myIntis 6

Value of myInt2 is 10
Value of --myInt2 is 9

We obtain the same result. Then what is the difference? Let us look deep into it. In the below example we are incrementing the value using operator while printing only.

public class MyOperators{
     public static void main(String []args){
        int myInt = 5;
        int myInt2 = 10;

        System.out.println("Value of myInt is "+myInt);
        System.out.println("Value of myInt after pre increment is "+ ++myInt);
        System.out.println();
        System.out.println("Value of myInt2 is "+myInt2);
        System.out.println("Value of myInt after pre decrement is "+ --myInt2);
       
        //Changing it back to the original value
        myInt = 5;
        myInt2= 10;       

        System.out.println("\nValue of myInt is "+myInt);
        System.out.println("Value of myInt after post increment is "+ myInt++);
        System.out.println();
        System.out.println("Value of myInt2 is "+myInt2);
        System.out.println("Value of myInt after post decrement is "+ myInt2--);      

        System.out.println("\nIn case of post, after being used in the above print statement, the value was changed and saved in the memory");

        System.out.println("\nValue of myInt is "+ myInt);
        System.out.println("Value of myInt2 is "+ myInt2);
     }
}

Output:

Value of myInt is 5
Value of myInt after pre increment is 6

Value of myInt2 is 10
Value of myInt after pre decrement is 9

Value of myInt is 5
Value of myInt after post increment is 5

Value of myInt2 is 10
Value of myInt after post decrement is 10

In case of post, after being used in the above print statement,the value was changed and saved in the memory

Value of myInt is 6
Value of myInt2 is 9

As you can see, the result shows all. The operand with post-increment/decrement is first used and then incremented/decremented, while in case of pre-increment/decrement, the value is first changed and then used. As a result, the change was visible right away in the case of pre, but not in post.

Inversion:

As the name indicates, the inversion operator inverts the sign of the operand after incrementing the value.

Example:

public class MyOperators{
     public static void main(String []args){
        int myInt = 5;

        System.out.println("myInt = "+myInt);
        System.out.println("~myInt ="+~myInt);     
     }
}

Output:

myInt = 5
~myInt =-6

Negation:

The negation operator simply toggles the Boolean value i.e true becomes false and vice versa.

Example:

public class MyOperators{
     public static void main(String []args){
        boolean myBool = false;

        System.out.println("myBool = "+myBool);
        System.out.println("!myBool = "+!myBool);     
     }
}

Output:

myBool = false
!myBool = true

Binary Operators

The operations for which binary operators are applicable are:

  • Arithmetic Operator ( * ,  / ,  % ,  + , - )
  • Shift Operator ( <<  ,  >>  ,  >>> )
  • Relational Operator( <  ,  > ,  <= , >= ,  instanceof  ,  == ,  != )
  • Bitwise Operator( & ,  ^ , | )
  • Logical Operator( && , || )

Arithmetic Operators ( * ,  / ,  % ,  + , - ):

The arithmetic operator acts as a basic mathematical model. The following example will be self-explanatory:

public class MyOperators{
     public static void main(String []args){
        int myInt = 30;
        int myInt2 = 6;       

        System.out.println("myInt = "+myInt);
        System.out.println("myInt2 = "+myInt2);

        //This is a division operator. It returns myInt*myInt2 i.e 180
        System.out.println("myInt * myInt2 = "+ (myInt * myInt2));

        //This is a division operator. It returns myInt/myInt2 i.e 5
        System.out.println("myInt / myInt2 = "+ (myInt / myInt2));

        //This is a modulo operator. It returns the remainder of myInt/myInt2 i.e 30%6 has no remainder. Hence, 0
        System.out.println("myInt % myInt2 = "+ (myInt % myInt2));

        //This is a addition operator. It returns myInt+myInt2 i.e 36
        System.out.println("myInt + myInt2 = "+ (myInt + myInt2));

        //This is a minus operator. It returns myInt-myInt2 i.e 24
        System.out.println("myInt - myInt2 = "+ (myInt - myInt2));

        System.out.println();
        System.out.println("The value of expression 30/6*2-7+8%4 is "+(30/6*2-7+8%4));
     }
}

Output:

myInt = 30
myInt2 = 6
myInt * myInt2 = 180
myInt / myInt2 = 5
myInt % myInt2 = 0
myInt + myInt2 = 36
myInt - myInt2 = 24
The value of expression 30/6*2-7+8%4 is 3

One more interesting thing about the + operator is that it is applicable to strings as well. The + operator concatenates two string.

Shift Operator ( <<  ,  >>  ,  >>> ):

Say we have operand 7 and 2 for every shift operator i.e 7<<2, 7>>2, 7>>>2)

The Shift operator works in the following steps for the given operands:

  1. Convert the number in binary format ( 7 is  111 in binary format ).
  2. The next step depends upon the specific operator: 
    • In case of << (left shift ), the digits shift to the left by 2 positions. After shifting, the void spaces are filled with 0.     
    • In case of >> (signed right shift ), the digits shift to the right by 2 positions. After shifting, the void spaces are filled with 0 for positive integers and 1 for negative integers. In our case 7 is positive, hence it is to be filled with 0.  ( 111 after <<2, turns into **1, the void spaces are filled with 0, hence it is 001)
    • In case of >>> (unsigned right shift ), the digits shift to the right by 2 positions. After shifting, the void spaces are filled with 0 always, irrespective of the sign of the number.
  3. The binary is again converted to decimal, and this is our output ( 001 is 1 in decimal format)

 Example:

public class MyOperators{
     public static void main(String []args){
        int myInt = 7;
        int myInt2 = 2;       

        System.out.println("myInt = "+myInt);
        System.out.println("myInt2 = "+myInt2);

        //Left Shift
        System.out.println("myInt << myInt2 = "+ (myInt << myInt2));

        //Signed Right Shift
        System.out.println("myInt >> myInt2 = "+ (myInt >> myInt2));

        //Unsigned Right Shift
        System.out.println("myInt >>> myInt2 = "+ (myInt >>> myInt2));
         //Changing the value of myInt to -7 and restoring myInt2 to 2
        myInt = -7;
        myInt2 = 2;
        
        System.out.println("\nmyInt = "+myInt);
        System.out.println("myInt2 = "+myInt2);
        //Left Shift
        System.out.println("myInt << myInt2 = "+ (myInt << myInt2)); 
        //Signed Right Shift
        System.out.println("myInt >> myInt2 = "+ (myInt >> myInt2));
        //Unsigned Right Shift
        System.out.println("myInt >>> myInt2 = "+ (myInt >>> myInt2));         
     }
}

Output:

myInt = 7
myInt2 = 2
myInt << myInt2 = 28
myInt >> myInt2 = 1
myInt >>> myInt2 = 1

myInt = -7
myInt2 = 2
myInt << myInt2 = -28
myInt >> myInt2 = -2
myInt >>> myInt2 = 1073741822

Relational Operator ( <  ,  > ,  <= , >= ,  instanceof  ,  == ,  != ):

The output of the relational operator is Boolean and self-explanatory. Let us just dive into the examples:

public class MyOperators{
     public static void main(String []args){
        int myInt = 7;
        int myInt2 = 2;       

        System.out.println("myInt = "+myInt);
        System.out.println("myInt2 = "+myInt2);
        System.out.println("myInt < myInt2 = "+ (myInt < myInt2));
        System.out.println("myInt > myInt2 = "+ (myInt > myInt2));
        System.out.println("myInt <= myInt2 = "+ (myInt <= myInt2));
        System.out.println("myInt >= myInt2 = "+ (myInt >= myInt2));
        System.out.println("myInt == myInt2 = "+ (myInt == myInt2));
        System.out.println("myInt != myInt2 = "+ (myInt != myInt2));       

        MyOperators myOp=new MyOperators(); 
        System.out.println("myOp instanceof MyOperators = "+(myOp instanceof MyOperators));
     }

}

Output:

myInt = 7
myInt2 = 2
myInt < myInt2 = false
myInt > myInt2 = true
myInt <= myInt2 = false
myInt >= myInt2 = true
myInt == myInt2 = false
myInt != myInt2 = true
myOp instanceof MyOperators = true

One thing to notice is that while checking the equality, we use == instead of == is used for assignment only. That is why == was introduced so that LHS and RHS can be checked for equality.

Bitwise Operator ( & ,  ^ , | ):

  • The bitwise & is read to be bitwise and operator. It performs the and operator bitwise. It yields true(1) only if corresponding bits of both the operand is true(1), else it returns false.
  •  The bitwise & is read to be bitwise xor operator. It performs the xor operator bitwise. It yields true(1) only if corresponding bits of both the operand are distinct, else it returns false.
  • The bitwise & is read to be bitwise or operator. It performs the or operator bitwise. It yields true(1) if either of the corresponding bits of both the operand is true(1), else it returns false.

   One important thing to note is that both the bits are checked in all the bitwise operators irrespective of the bit value of the first operand being true(1)/false(0).

Example:

public class MyOperators{
     public static void main(String []args){
        int myInt = 7;
        int myInt2 = 2;       

        System.out.println("myInt = "+myInt);
        System.out.println("myInt2 = "+myInt2);
        System.out.println("myInt & myInt2 = "+ (myInt & myInt2));
        System.out.println("myInt ^ myInt2 = "+ (myInt ^ myInt2));
        System.out.println("myInt | myInt2 = "+ (myInt | myInt2));
     }
}

Output:

myInt = 7
myInt2 = 2
myInt & myInt2 = 2
myInt ^ myInt2 = 5
myInt | myInt2 = 7

Logical Operator ( && , || ):

  • The && is read as and operator. It performs the and operator. It yields true(1) only if both the operands are true(1), else it returns false. The second operator is not evaluated in the first operand is false.
  •  The || is read as or operator. It performs the or operator. It yields true(1) even if either of the operands is true(1), else it returns false. . The second operator is not evaluated in the first operand is true.

Example:

public class MyOperators{
     public static void main(String []args){
        boolean myBool = true;
        boolean myBool2 = false;       

        System.out.println("myBool = "+myBool);
        System.out.println("myBool2 = "+myBool2);
        System.out.println("myBool && myBool2 = "+ (myBool && myBool2));
        System.out.println("myBool || myBool2 = "+ (myBool || myBool2));
     }
}

Output:

myBool = true
myBool2 = false
myBool && myBool2 = false
myBool || myBool2 = true

Ternary Operator (?:)

The format of ternary is as follows:

(Java-Expression)? <block of code if expression evaluates true> : < block of code if expression evaluates false>

Example:

public class MyOperators{
     public static void main(String []args){
        int myInt = 7;
        int myInt2 = 2;        

        System.out.println("myInt = "+myInt);
        System.out.println("myInt2 = "+myInt2);     
        System.out.println((myInt>myInt2)?"myInt is greater than myInt2":"myInt is less than myInt2");       
        System.out.println((myInt2>myInt)?"myInt2 is greater than myInt":"myInt2 is less than myInt");
     }
}

Output:

myInt = 7
myInt2 = 2
myInt is greater than myInt2
myInt2 is less than myInt

Assignment Operator

Apart from so many operators mentioned above, there is another section of the operator which is used to manipulate the value of the left operand on the basis of the assignment operator and the right operand.

The use of <any of the arithmetic, shifting, bitwise operator> followed by = makes it an assignment operator.

Example: 7+=2 , 24/=3 , 7*=3 , 7<<=3

public class MyOperators{
     public static void main(String []args){
        int a=7;
        int b=2;
        int c=24;
        int d=3;

        System.out.println("7+=2 is "+(a+=b));  // a+=b mean a=a+b  so now a = 9
        System.out.println("24/=3 is "+(c/=d)); // c/=d mean c=c/d so now c = 8
        System.out.println("9*=3 is "+(a*=d));  // a*=d mean a=a*d so now a = 27
        System.out.println("27<<=3 is "+(a<<=d));  // a<<=d mean a=a<<d so now a = 216
     }
}

Output:

7+=2 is 9
24/=3 is 8
9*=3 is 27
27<<=3 is 216

This was a detailed overview of Operators. We will discuss Java Decision-Making in the next section.