In this section, we will learn the operator precedence in Java along with examples.
The operator precedence represents how two expressions are bind together. In an expression, it determines the grouping of operators with operands and decides how an expression will evaluate.
While solving an expression two things must be kept in mind the first is a precedence and the second is associativity.
Precedence is the priority for grouping different types of operators with their operands. It is meaningful only if an expression has more than one operator with higher or lower precedence. The operators having higher precedence are evaluated first. If we want to evaluate lower precedence operators first, we must group operands by using parentheses and then evaluate.
We must follow associativity if an expression has more than two operators of the same precedence. In such a case, an expression can be solved either left-to-right or right-to-left, accordingly.
The following table describes the precedence and associativity of operators used in Java.
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 |
Let's understand the operator precedence through an example. Consider the following expression and guess the answer.
You might be thinking that the answer would be 18 but not so. Because the multiplication (*) operator has higher precedence than the addition (+) operator. Hence, the expression first evaluates 5*3 and then evaluates the remaining expression i.e. 1+15. Therefore, the answer will be 16.
Let's see another example. Consider the following expression.
In the above expression, * and / operations are performed before + because of precedence. y is multiplied by z before it is divided by k because of associativity.
Next Topic Private Constructor in JavaFor Videos Join Our Youtube Channel: Join Now