Operator Precedence
In Java, operator precedence determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated before operators with lower precedence. If operators have the same precedence, they are evaluated from left to right.
Here's a general overview of operator precedence in Java, from highest to lowest precedence:
Postfix operators:
expr++
,expr--
Unary operators:
++expr
,--expr
,+expr
,-expr
,!expr
,~expr
,(type)expr
Multiplicative operators:
*
,/
,%
Additive operators:
+
,-
Shift operators:
<<
,>>
,>>>
Relational operators:
<
,<=
,>
,>=
,instanceof
Equality operators:
==
,!=
Bitwise AND:
&
Bitwise XOR:
^
Bitwise OR:
|
Logical AND:
&&
Logical OR:
||
Ternary operator:
? :
Assignment operators:
=
,+=
,-=
,*=
,/=
,%=
,&=
,^=
,|=
,<<=
,>>=
,>>>=
Parentheses ()
can be used to override the default precedence and force evaluation of parts of an expression first.
For example:
int result = 10 + 2 * 3;
In this expression, multiplication has higher precedence than addition, so 2 * 3
is evaluated first, resulting in 6
, and then added to 10
, yielding 16
.
Understanding operator precedence is crucial for writing expressions that produce the expected results and avoiding bugs related to incorrect evaluation order.
Last updated
Was this helpful?