Precedence order.

When two operators share an operand the operator with the higher precedence goes first. For example, 1 + 2 * 3 is treated as 1 + (2 * 3), whereas 1 * 2 + 3 is treated as (1 * 2) + 3 since multiplication has a higher precedence than addition.
If consecutive operators in an expression have the same precedence, a rule called associativity is used to decide the order in which those operators are evaluated. An operator can be left-associative, right-associative, or non-associative:
- Left-associative operators of the same precedence are evaluated in order from left to right. For example, addition and subtraction have the same precedence and they are left-associative. For more additional info Java Online Course
- In the expression 10-4+2, the subtraction is done first because it is to the left of the addition, producing a value of 8.
- Right-associative operators of the same precedence are evaluated in order from right to left. For example, assignment is right-associative. Consider the following code fragment:
- int a = 3;
- int b = 4;
- a = b = 5;
After the code has been evaluated, both a and b contain 5 because the assignments are evaluated from right to left.
- A non-associative operator cannot be combined with other operators of the same precedence.
Precedence and associativity of Java operators.
The table below shows all Java operators from highest to lowest precedence, along with their associativity. Most programmers do not memorize them all, and even those that do still use parentheses for clarity. Learn more skills from Java Certification Course
Order of evaluation of subexpressions.
Associativity and precedence determine in which order Java applies operators to subexpressions but they do not determine in which order the subexpressions are evaluated. In Java, subexpressions are evaluated from left to right (when there is a choice). So, for example in the expression A() + B() * C(D(), E()), the subexpressions are evaluated in the order A(), B(), D(), E(), and C(). Although, C() appears to the left of both D() and E(), we need the results of both D() and E() to evaluate C(). It is considered poor style to write code that relies upon this behavior (and different programming languages may use different rules).
Short circuiting. When using the conditional and and or operators (&& and ||), Java does not evaluate the second operand unless it is necessary to resolve the result. This allows statements like if (s != null && s.length() < 10) to work reliably. Programmers rarely use the non short-circuiting versions (& and |) with boolean expressions.
Larger number means higher precedence.
| Precedence | Operator | Type | Associativity |
| 15 | () [] · | Parentheses Array subscript Member selection | Left to Right |
| 14 | ++ — | Unary post-increment Unary post-decrement | Right to left |
| 13 | ++ — + – ! ~ ( type ) | Unary pre-increment Unary pre-decrement Unary plus Unary minus Unary logical negation Unary bitwise complement Unary type cast | Right to left |
| 12 | * / % | Multiplication Division Modulus | Left to right |
| 11 | + – | Addition Subtraction | Left to right |
| 10 | << >> >>> | Bitwise left shift Bitwise right shift with sign extension Bitwise right shift with zero extension | Left to right |
| 9 | < <= > >= instanceof | Relational less than Relational less than or equal Relational greater than Relational greater than or equal Type comparison (objects only) | Left to right |
| 8 | == != | Relational is equal to Relational is not equal to | Left to right |
| 7 | & | Bitwise AND | Left to right |
| 6 | ^ | Bitwise exclusive OR | Left to right |
| 5 | | | Bitwise inclusive OR | Left to right |
| 4 | && | Logical AND | Left to right |
| 3 | || | Logical OR | Left to right |
| 2 | ? : | Ternary conditional | Right to left |
| 1 | = += -= *= /= %= | Assignment Addition assignment Subtraction assignment Multiplication assignment Division assignment Modulus assignment | Right to left |
To get in-depth knowledge, enroll for live free demo on Java Online Training