Java Operator Precedence

Before you start reading this article, you should have a basic knowledge of Java Operators.


Now, take a look at the statement below:

int myInt = 12 - 4 * 2;

What will be the value of myInt? Will it be (12 - 4)*2, that is, 16? Or it will be 12 - (4 * 2), that is, 4?

When two operators share a common operand, 4 in this case, the operator with the highest precedence is operated first.

In Java, the precedence of * is higher than that of -. Hence, the multiplication is performed before subtraction, and the value of myInt will be 4.


Operator Precedence Table

The table below lists the precedence of operators in Java; higher it appears in the table, the higher its precedence.

Java Operator Precedence
Operators Precedence
postfix increment and decrement ++ --
prefix increment and decrement, and unary ++ -- + - ~ !
multiplicative * / %
additive + -
shift << >> >>>
relational < > <= >= instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ? :
assignment = += -= *= /= %=
&= ^= |= <<= >>= >>>=

Example: Operator Precedence

class Precedence {
    public static void main(String[] args) {
    	
    	int a = 10, b = 5, c = 1, result;
    	result = a-++c-++b;
    	
    	System.out.println(result);
    }
}

Output:

2

The operator precedence of prefix ++ is higher than that of - subtraction operator. Hence,

result = a-++c-++b;

is equivalent to

result = a-(++c)-(++b);

When dealing with multiple operators and operands in a single expression, you can use parentheses like in the above example for clarity. The expression inside the parentheses is evaluated first.


Associativity of Operators in Java

If an expression has two operators with similar precedence, the expression is evaluated according to its associativity (either left to right, or right to left). Let's take an example.

a = b = c;

Here, the value of c is assigned to variable b. Then the value of b is assigned of variable a. Why? It's because the associativity of = operator is from right to left.

The table below shows the associativity of Java operators along with their associativity.

Java Operator Precedence and Associativity
Operators Precedence Associativity
postfix increment and decrement ++ -- left to right
prefix increment and decrement, and unary ++ -- + - ~ ! right to left
multiplicative * / % left to right
additive + - left to right
shift << >> >>> left to right
relational < > <= >= instanceof left to right
equality == != left to right
bitwise AND & left to right
bitwise exclusive OR ^ left to right
bitwise inclusive OR | left to right
logical AND && left to right
logical OR || left to right
ternary ? : right to left
assignment = += -= *= /= %=
&= ^= |= <<= >>= >>>=
right to left

You don't need to memorize everything here. Most of the time, the precedence and associativity of operators makes sense in itself. You can always come back to this article for reference when in doubt. Also, you can use parenthesis if you think it makes your code easier to understand.

Did you find this article helpful?