Java Operator Precedence

Here the table given below shows the order of precedence for the Java operators, from highest to lowest. Operators in the same row are equal in priority or precedence.

In binary operations, the order of evaluation is left to right (except for assignment, evaluates from right to left). Even though they technically separators, the [], (), and . (dot operator) can also act like operators. In that play, they would have the highest precedence. Also, notice the arrow operator (->). It was added by the JDK 8 and is used in lambda expressions.

Java Operator Precedence Table

Highest
++ (postfix) -- (postfix)
++ (prefix) -- (prefix) ~ ! + (unary) - (unary) (type-cast)
* / %
+ -
>> >>> <<
> >= < <= instanceof
== !=
&
^
|
&&
||
?:
->
= op=
Lowest

Java Operator Precedence Example

Let's consider the following simple Java program, demonstrates Java's operators precedence:

/* Java Program Example - Java Operators Precedence
 * This simple program illustrates 
 * operator precedence
 */

public class JavaProgram
{ 
   public static void main(String args[])
   {
       
      int a;
      
      a = 2 + 3 - 4 * 5 / 6;
      
      System.out.print(a);
      
   }
}

When the above Java program is compile and run, it will produce the following output:

java operator precedence

As you will see, this will produce output as 2, from above table, the calculation of the above program takes in the following steps:

  1. first, 4 * 5 will perform, results in 20
  2. next, 4 * 5 / 6 i.e., 20 / 6 will perform, results in 3
  3. next, 2 + 3 - 3 will perform, results in 2

Java Online Test


« Previous Tutorial Next Tutorial »