Order of Precedence for Java Operators

Similar to Mathematics' PEMDAS or MDAS, Java also has its own Order of Precedence which is to be followed when understanding, creating or reading Java programs. It is a way for Java to determine which operator should it evaluate first before the others.

Below is the Order of Precedence displayed from the highest to the lowest precedence:


Priority  Operator  Use
1[ ]index of arrays

( )calling methods

.access members
2++increment

--decrement

+, -unary add, subtract

~NOT (bitwise)

!NOT (boolean / logical)

(type)new type cast

(new)new object
3*, /, %multiplication, division, modulo (remainder)
4+, - addition, subtraction 

+concatination
5<<signed bit shift left

>>signed bit shift right

>>>unsigned bit shift right
6<, <=less than, less than or equal to

>, >=greater than, greater than or equal to

instanceofcalling/testing references
7= =is equal to

! =is not equal to
8&AND (bitwise and logical)
9^XOR (bitwise and logical)
10|OR (bitwise and logical)
11&&Boolean AND
12||Boolean OR
13?, , , :conditional statements
14=assignment

*=, /=, +=, -=combinated assignment

%=combinated assignment

<<=, >>=combinated assignment

&=,^=, |=combinated assignment


Reminder:
  • Expressions within the parenthesis are first to be evaluated
  • Nested Parenthesis are evaluated from the inner to the outer parenthesis.
  • Higher precedence are done first before the lower precedence.

No comments:

Post a Comment

Extended Search

Custom Search
Hello World