[go: up one dir, main page]

0% found this document useful (0 votes)
33 views4 pages

Precedence Level Operator Operation Associates: Figure D.1

Java operators are evaluated based on their precedence level, with lower precedence operators evaluated first. Operators at the same precedence level are evaluated left to right or right to left depending on their associativity. The type of operands determines if an operator performs arithmetic, string, bitwise, or boolean operations. Parentheses can be used to override the normal precedence evaluation order.

Uploaded by

Mouhamad Bazzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views4 pages

Precedence Level Operator Operation Associates: Figure D.1

Java operators are evaluated based on their precedence level, with lower precedence operators evaluated first. Operators at the same precedence level are evaluated left to right or right to left depending on their associativity. The type of operands determines if an operator performs arithmetic, string, bitwise, or boolean operations. Parentheses can be used to override the normal precedence evaluation order.

Uploaded by

Mouhamad Bazzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Java operators are evaluated according to the precedence hierarchy shown in Fig.

D.1. Operators at low precedence levels are evaluated before operators at higher
levels. Operators within the same precedence level are evaluated according to the
specified association, either right to left (R to L) or left to right (L to R).
D
Operators in the same precedence level are not listed in any particular order.

java operators
Precedence
Operator Operation Associates
Level

1 [] array indexing L to R
. object member reference
(parameters) parameter evaluation and method invocation
++ postfix increment
-- postfix decrement
2 ++ prefix increment R to L
-- prefix decrement
+ unary plus
- unary minus
~ bitwise NOT
! logical NOT

3 new object instantiation R to L


(type) cast

4 * multiplication L to R
/ division
% remainder
5 + addition L to R
+ string concatenation
- subtraction
6 << left shift
>> right shift with sign
>>> right shift with zero

7 < less than L to R


<= less than or equal
> greater than
>= greater than or equal
instanceof type comparison
8 == equal L to R
!= not equal

figure D.1 Java operator precedence


694 APPENDIX D java operators

Precedence
Operator Operation Associates
Level

9 & bitwise AND L to R


& boolean AND
10 ˆ bitwise XOR L to R
ˆ boolean XOR
11 | bitwise OR L to R
| boolean OR
12 && logical AND L to R

13 || logical OR L to R

14 ?: conditional operator R to L
15 = assignment R to L
+= addition, then assignment
+= string concatenation, then assignment
-= subtraction, then assignment
*= multiplication, then assignment
/= division, then assignment
%= remainder, then assignment
<<= left shift, then assignment
>>= right shift (sign), then assignment
>>>= right shift (zero), then assignment
&= bitwise AND, then assignment
&= boolean AND, then assignment
ˆ= bitwise XOR, then assignment
ˆ= boolean XOR, then assignment
|= bitwise OR, the assignment
|= boolean OR, the assignment

figure D.1 Java operator precedence, continued

The order of operator evaluation can always be forced by the use of parenthe-
ses. It is often a good idea to use parentheses even when they are not required to
make it explicitly clear to a human reader how an expression is evaluated.
For some operators, the operand types determine which operation is carried
out. For instance, if the + operator is used on two strings, string concatenation is
performed, but if it is applied to two numeric types, they are added in the arith-
metic sense. If only one of the operands is a string, the other is converted to a
string, and string concatenation is performed. Similarly, the operators &, ^, and |
perform bitwise operations on numeric operands but boolean operations on
boolean operands. Appendix E describes the bitwise and boolean operators in
more detail.
APPENDIX D java operators 695

The boolean operators & and | differ from the logical operators && and || in
a subtle way. The logical operators are “short-circuited” in that if the result of an
expression can be determined by evaluating only the left operand, the right
operand is not evaluated. The boolean versions always evaluate both sides of the
expression. There is no logical operator that performs an exclusive OR (XOR)
operation.

You might also like