Friday, 10 February 2012

OPERATOR PRECEDENCE


Java has well-defined rules for specifying the order in which the operators in an expression are evaluated when the expression has several operators.
Precedence Order: When two operators share an operand the operator with the higher precedence goes first.
Associativity: When two operators with the same precendence the expression is evaluated according to its associativity. That is associativity of an operator is a property that determines how operators of the same precedence are grouped.
The table below shows all Java operators from highest to lowest precedence, along with their associativity.

Operator
Description
Associativity
[]
.
()
++
--
access array element
access object member
invoke a method
post-increment
post-decrement
left to right
++
--
+
-
!
~
pre-increment
pre-decrement
unary plus
unary minus
logical NOT
bitwise NOT
right to left
()
new
cast
object creation
right to left
*
/
%
multiplicative
left to right
+ -
+
additive
string concatenation
left to right
<< >>
>>>
shift
left to right
<  <=
 >=   >
instanceof
Relational

Type comparision
left to right
==
!=
Equality
left to right
&
bitwise AND
left to right
^
bitwise XOR
left to right
|
bitwise OR
left to right
&&
conditional AND
left to right
||
conditional OR
left to right
?:
conditional
right to left
==  +=  -=
*=  /=
%=
&=  ^=
>>=  <<=
>>>=
assignment
Right to left

Order of evaluation: In Java, the left operand is always evaluated before the right operand. Also applies to function arguments.
Short circuiting: When using the conditional AND and OR operators (&& and ||), Java does not evaluate the second operand unless the first argument does not suffice to determine the value of expression. 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Labels