Java Operators: Precedence and Associativity

Java Operator Types

A Java operator is a special symbol that performs specific operation on one, two, or three operands depending upon the type of the operator and returns a result.

Java provides a rich set of operators that are classified on two bases. First, on the basis of number of operands an operator performs upon. Second, on the type or nature of operation an operator performs.

By first classification, Java operators can be unary, binary, or ternary. Unary operators operate on one operand e.g., ++, and --. Binary operators operator on two operands. All arithmetic operators are example of binary operators. Third, special type operator is ternary operator. Ternary operator operates on three operands. Java has only one ternary operator, which is also called conditional operator. It is a combination of two symbols ? and :.

Another classification is based on the type of operation they perform. On the basis of the type of operation operators can be classified in following categories:

  1. Arithmetic Operators
  2. Increment Decrement Operators
  3. Bitwise Operators
  4. Relational Operators
  5. Logical Operators
  6. Assignment Operators
  7. Miscellaneous Operators

Evaluation Order of an Expression

In Java when an expression is evaluated, there may be more than one operators involved in an expression. When more than one operator has to be evaluated in an expression Java interpreter has to decide which operator should be evaluated first. Java makes this decision on the basis of the precedence and the associativity of the operators.

Java Operators Precedence and Associativity

Java operators have two properties those are precedence, and associativity. Precedence is the priority order of an operator, if there are two or more operators in an expression then the operator of highest priority will be executed first then higher, and then high. For example, in expression 1 + 2 * 5, multiplication (*) operator will be processed first and then addition. It's because multiplication has higher priority or precedence than addition.

Alternatively, you can say that when an operand is shared by two operators (2 in above example is shared by + and *) then higher priority operator picks the shared operand for processing. From above example you would have understood the role of precedence or priority in execution of operators. But, the situation may not be as straightforward every time as it is shown in above example. What if all operators in an expression have same priority? In that case the second property associated with an operator comes into play, which is associativity. Associativity tells the direction of execution of operators that can be either left to right or right to left. For example, in expression a = b = c = 8 the assignment operator is executed from right to left that means c will be assigned by 8, then b will be assigned by c, and finally a will be assigned by b. You can parenthesize this expression as (a = (b = (c = 8))).

Note that, you can change the priority of a Java operator by enclosing the lower order priority operator in parentheses but not the associativity. For example, in expression (1 + 2) * 3 addition will be done first because parentheses has higher priority than multiplication operator.

Before discussing individual classes of operators, below table presents all Java operators from highest to lowest precedence along with their associativity.

Table 1: Java operators - precedence chart highest to lowest
Precedence Operator Description Associativity
1 []
array index
method call
member access
Left -> Right
2 ++
+ -
pre or postfix increment
pre or postfix decrement
unary plus, minus
bitwise NOT
logical NOT
Right -> Left
3 (type cast)
type cast
object creation
Right -> Left
4 *
modulus (remainder)
Left -> Right
5 + -
addition, subtraction
string concatenation
Left -> Right
6 <<
left shift
signed right shift
unsigned or zero-fill right shift
Left -> Right
7 <
less than
less than or equal to
greater than
greater than or equal to
reference test
Left -> Right
8 ==
equal to
not equal to
Left -> Right
9 &
bitwise AND Left -> Right
10 ^ bitwise XOR Left -> Right
11 | bitwise OR Left -> Right
12 && logical AND Left -> Right
13 || logical OR Left -> Right
14 ? : conditional (ternary) Right -> Left
15 =
assignment and short hand assignment operators Right -> Left

Last Word

In this tutorial we talked of Java operators' precedence and associativity. Hope you have enjoyed reading this tutorial. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!


Get Free Tutorials by Email

About the Author

is the main author for He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.

Today's Tech News

UK regulator has 'huge concerns' over Uber breachPosted on Wednesday November 22, 2017

A data breach affecting 57 million customers and drivers should not have been concealed, the information commissioner says.

Android phones 'betray' user location to GooglePosted on Wednesday November 22, 2017

The devices send location data back to Google even when location services are switched off.

Budget 2017: Funding for AI, 5G and digital skillsPosted on Wednesday November 22, 2017

The chancellor acknowledged the digital revolution would change the way people lived and worked.

Courtesy BBC News


Advertisements help running this site for free.

To view the content please disable AdBlocker and refresh the page.