Operators in Java

The operator set in Java is extremely rich. Broadly, the operators available in Java are divided in the following categories.

  • Relational Operators

  • Arithmetic Operators

  • Logical Operators

  • Bitwise Operators

  • Misc Operators

  • Assignment Operators

The Arithmetic Operators

Operations in Java are used in essentially the same manner as in algebra. They are used with variables for performing arithmetic operations. Here is a list of arithmetic operators available in Java.

​Operation​
​Operator​
​Description​

Addition

+

Adds the values of two variables

Subtraction

-

Subtracts the values of two variables

Multiplication

*

Multiplies the values of two variables

Division

/

Divides the values of two variables

Modulus

%

The resultant value is the the remainder of division

Increment

++

Increases the value by 1

Decrement

Decreases the value by 1


The Relational Operators

Java also supports several relational operators. The list of relational operators that are supported by Java are given below.

Operation

Operator
Description

Equal To

 

==

Compares the values of two variables for equality

Not Equal To

 

!=

Compares the values of two variables for inequality

Greater Than

 

>

Checks if one value is greater than the other value

Lesser Than

 

<

Checks if one value is lesser than the other value

Greater Than Equal To

Or

>=

Checks if one value is greater than or equal to the other value

Lesser Than Equal To

Or

<=

Checks if one value is lesser than or equal to the other value


The Bitwise Operators

The bitwise operators available in Java can be easily applied to a number of data types. These data types include byte, short, long, int and char. Typically, any bitwise operator performs the concerned operation bit-wise. For instance, if you consider the example of an integer x, which has the value 60. Therefore, the binary equivalent of x is 00111100. Consider another variable y, with the value 13 or 00001101. If we perform the bitwise operation & on these two numbers, then you will get the following result:

x&y = 0000 1100

The table shown below shows a list of bitwise operators that are available in Java.

Operation
Operator
Description

BINARY AND

&

Performs the AND operation

BINARY OR

|

Performs the OR operation

BINARY XOR

^

Performs the XOR operation

ONE’S COMPLEMENT

~

Performs the complementation operation on a unary variable

BINARY LEFT SHIFT

<<

Performs the left shifting of bits

BINARY RIGHT SHIFT

>>

Performs the right shifting of bits

In addition to the above mentioned, Java also supports right shift zero fill operator (>>>), which fills the shifted bits on the right with zero.


The Logical Operators

Logical operators are an integral part of any operator set. The logical operators supported by Java are listed in the table below.

Operation
Operator
Description

Logical AND

&&

Returns True if both the conditions mentioned are true

Logical OR

||

Returns True if one or both the conditions mentioned are true

Logical NOT

!

Returns True if the condition mentioned is False


The Assignment Operators

There are following assignment operators supported by Java language:

Operation
Operator
Description

Simple assignment operator

=

Assigns a value on the right to the variable in the left

Add - assignment operator

+=

Adds the value on the right to the value of the variable on the left and assigns the resultant to the variable on the left

Subtract - assignment operator

-=

Subtracts the value on the right to the value of the variable on the left and assigns the resultant to the variable on the left

Multiply - assignment operator

*=

Multiplies the value on the right to the value of the variable on the left and assigns the resultant to the variable on the left

Divide - assignment operator

/=

Divides the value on the right to the value of the variable on the left and assigns the resultant to the variable on the left

Modulus - assignment operator

%=

It takes the modulus of the LHS and RHS and assigns the resultant to the variable on the left

Left shift - assignment operator

<<=

It takes the left shift of the LHS and RHS and assigns the resultant to the variable on the left

Right shift - assignment operator

>>=

It takes the right shift of the LHS and RHS and assigns the resultant to the variable on the left

Bitwise - assignment operator

&=

It takes the bitwise AND of the LHS and RHS and assigns the resultant to the variable on the left

bitwise exclusive OR - assignment operator

^=

It takes the bitwise XOR of the LHS and RHS and assigns the resultant to the variable on the left

bitwise inclusive OR - assignment operator

|=

It takes the bitwise OR of the LHS and RHS and assigns the resultant to the variable on the left


Misc Operators

In addition to the above mentioned, there are several other operators, which are supported by Java.


Conditional Operator ( ? : ):

The conditional operator is a ternary operator that contains three operands. Essentially, this operator is used for the evaluation of boolean expressions. The operator tests the first operand or condition and if the condition is true, then the second value is assigned to the variable. However, if the condition is false, the third operand is assigned to the variable. The syntax of this operator is as follows:

variable a = (<condition>) ? valueiftrue : valueiffalse Sample implementation:

public class myTest {
    public static void main(String args[]) {
        int x, y;
        x = 5;
        y = (x == 5) ? 15 : 40;
        System.out.println(“y = ”+y);
        y = (x == 34) ? 60 : 95;
        System.out.println(“x = ”+y);
    }
}

The compilation and execution of this code shall give the following result: 

y = 15
y = 95


instanceof Operator:

Only object reference variables can be used with this operator. The objective of this operator is to check is an object is an instance of an exiting class. The syntax of this operator is as follows:

(<object reference variable>) instanceof(<interface/class>)

Sample implementation of this operator and its purpose of use is given below: 

public class myTest {
  public static void main(String args[]) {
    int x = 4;
    boolean resultant = x instanceof int;
    System.out.println(resultant);
  }
}

The output of this code shall be true. This operator can also be used in comparison. A sample implementation of this is given below:

class Animal {}
public class Monkey extends Animal {
  public static void main(String args[]) {
    Animal newa = new Monkey();
    boolean resultant = newa instanceof Monkey;
    System.out.println(resultant);
  }
}

The output for this code will also be true. Precedence of Java Operators


More often than not, operators are used in combinations in expressions. However, you must have also realized that it becomes difficult to predict the order in which operations will take place during execution. The operator precedence table for Java shall help you predict operator operations in an expression deduction. For instance, if you are performing addition and multiplication in the same expression, then multiplication takes place prior to addition. The following table illustrates the order and hierarchy of operators in Java. The associativity for all the operators is left to right. However, the unary, assignment and conditional operator follows right to left associativity.

 

Operator
Operator

() [] . (dot operator)

Postfix

++ - - ! ~

Unary

*   / %

Multiplicative

+  -

Additive

>> >>> <<

Shift

>  >= < <=

Relational

== !=

Equality

&

Bitwise AND

|

Bitwise OR

^

Bitwise XOR

&&

Logical AND

||

Logical OR

?:

Conditional

=   += -= *= /= %= >>= <<= &= ^= |=

Assignment

,

Comma