Wednesday, October 14, 2015

Operator Precedence in Java.

Java has well-defined rules for execution order in which the operators in an expression are evaluated when the expression has several operators.
Below are the execution orders i.e. precedence in Java Coding.

LevelOperatorDescriptionAssociativity
1=
+=
-=
*=
/=
%=
Assignment
Addition assignment
Subtraction assignment
Multiplication assignment
Division assignment
Modulus assignment
Right
2? :Ternary conditionalRight
3||Logical ORLeft
4&&Logical ANDLeft
5|Bitwise inclusive ORLeft
6^Bitwise exclusive ORLeft
7&Bitwise ANDLeft
8==
!=
Relational is equal to
Relational is not equal to
Left
9<
<=
>
>=
instanceof
Relational less than
Relational less than or equal
Relational greater than
Relational greater than or equal
Type comparison (objects only)
Left
10<<
>>
>>>
Bitwise left shift
Bitwise right shift with sign extension
Bitwise right shift with zero extension
Left
11+
-
Addition
Subtraction
Left
12*
/
%
Multiplication
Division
Modulus
Left
13++
--
+
-
!
~
type )
Unary pre-increment
Unary pre-decrement
Unary plus
Unary minus
Unary logical negation
Unary bitwise complement
Unary type cast
Right
14++
--
Unary post-increment
Unary post-decrement
Right
15()
[]
·
Parentheses
Array subscript
Member selection
Left