Operator Precedence

When more than one operator is used in an expression, Java has an established precedence hierarchy to determine the order in which operators are evaluated. In many cases, this precedence determines the overall value of the expression.

In general, the order of evaluation from first to last is the following:

  • Increment and decrement operations
  • Arithmetic operations
  • Comparisons
  • Logical operations
  • Assignment expressions

If two operations have the same precedence, the one on the left in the actual expression is handled before the one on the right. This following table shows the specific precedence of the various operators in Java. Operators farther up the table are evaluated first.

Operator Notes
. [] () Parentheses (“()“) are used to group expressions; a period (“.“) is used for access to methods and variables within objects and classes; square brackets (“[]“) are used for arrays.
++ -- ! ~ instanceof The instanceof operator returns true or false based on whether the object is an instance of the named class or any of that class’s subclasses.
new (type)expression The new operator is used for creating new instances of classes; “()” in this case are for casting a value to another type.
* / % Multiplication, division, modulus.
+ - Addition, subtraction.
<< >> >>> Bitwise left and right shift.
< > <= >= Relational comparison tests.
== != Equality.
& AND
^ XOR
| OR
&& Logical AND
|| Logical OR
? : Shorthand for if-then-else.
= += -= *= /= %= ^= Various assignments.
&= |= <<= >>= >>>= More assignments.

Some useful notes:

  • When you use postfix operators, as in y = x++, y receives the value of x before it is incremented by one.

  • When using prefix operators, as in z = ++x, x is incremented by one before the value is assigned to z.

  • The difference between “&” and “&&” lies in how much work Java does on the combined expression. If “&” is used, the expressions on either side of the “&” are evaluated no matter what. If “&&” is used and the left side of the “&&” is false, the expression on the right side of the “&&” never is evaluated.

  • Same rule goes for “|” and “||“.

  • The XOR combination has one logical operator, “^“. This results in a true value only if both Boolean expressions it combines have opposite values. If both are true or both are false, the “^” operator produces a false value.

Source: Sams Teach Yourself Java 6 in 21 Days (5th Edition)

Published by Eric Gunawan

Happiness Engineer. WordPress Ambassador. Remote Worker. Soccer News Follower. Movie Lover. Proud Father. Lucky Husband.

One thought on “Operator Precedence

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: