Operators
lists the operators of the Java language, along with their precedence,
operand types, and associativity.
Table 13.3: Java Operators
Prec. 
Operator 
Operand Type(s) 
Assoc. 
Operation Performed 
1 
++ 
arithmetic 
R 
preorpost increment (unary) 

 
arithmetic 
R 
preorpost decrement (unary) 

+,  
arithmetic 
R 
unary plus, unary minus 

~ 
integral 
R 
bitwise complement (unary) 

! 
boolean 
R 
logical complement (unary) 

(type) 
any 
R 
cast 
2 
*, /, % 
arithmetic 
L 
multiplication, division, remainder 
3 
+,  
arithmetic 
L 
addition, subtraction 

+ 
string 
L 
string concatenation 
4 
<< 
integral 
L 
left shift 

>> 
integral 
L 
right shift with sign extension 

>>> 
integral 
L 
right shift with zero extension 
5 
<, <= 
arithmetic 
L 
less than, less than or equal 

>, >= 
arithmetic 
L 
greater than, greater than or equal 

instanceof 
object, type 
L 
type comparison 
6 
== 
primitive 
L 
equal (have identical values) 

!= 
primitive 
L 
not equal (have different values) 

== 
object 
L 
equal (refer to same object) 

!= 
object 
L 
not equal (refer to different objects) 
7 
& 
integral 
L 
bitwise AND 

& 
boolean 
L 
boolean AND 
8 
^ 
integral 
L 
bitwise XOR 

^ 
boolean 
L 
boolean XOR 
9 
 
integral 
L 
bitwise OR 

 
boolean 
L 
boolean OR 
10 
&& 
boolean 
L 
conditional AND 
11 
 
boolean 
L 
conditional OR 
12 
?: 
boolean, any, any 
R 
conditional (ternary) operator 
13 
= 
variable, any 
R 
assignment 

*=, /=, %=, +=, =, <<=, >>=, >>>=, &=, ^=, = 
variable, any 
R 
assignment with operation 
Operator precedence controls the order in which operations are
performed. Consider the following example:
The multiplication operator * has a higher
precedence than the addition operator +, so the
multiplication is performed before the addition.
Furthermore, the assignment operator = has the
lowest precedence of any operator, so the assignment is
done after all the operations on the righthand side are
performed. Operators with the same precedence (like
addition and subtraction) are performed in order according
to their associativity
(usually lefttoright). Operator precedence
can be overridden with the explicit use of parentheses. For example:
The associativity of an operator specifies the order that
operations of the same precedence are performed in. In
Table 13.3
a value of L specifies lefttoright associativity, and a
value of R specifies righttoleft associativity.
Lefttoright associativity means that operations are
performed lefttoright. For example:
is the same as:
because the addition operator has lefttoright
associativity. On the other hand, the following expressions:
x = ~~y;
q = a?b:c?d:e?f:g;
are equivalent to:
x = ~((~y));
q = a?b:(c?d:(e?f:g));
because the unary operators and the ternary conditional
?: operator have righttoleft associativity.
Java operators are basically identical to C operators,
except for these differences:
 The + operator applied to String values
concatenates them. If only one operand of + is a
String, the other one is converted to a string.
The conversion is done automatically for primitive types
and by calling the toString method of nonprimitive
types.
 Java does not have the comma operator like C does. It does,
however, simulate this operator in the limited context of the
for loop initialization and increment expressions.
 Since all Java integral types are signed, the >>
operator always does a signed right shift, filling in high
bits with the sign bit of the operand. The new >>>
operator performs an unsigned right shift, filling in high
bits of the shifted value with zero bits.
 The & and  operators perform bitwise AND
and OR operations on integral operands, and perform logical
AND and OR operators on boolean operands.
&& and  also perform logical AND and OR on
boolean operands, but do not evaluate the righthand
operand, if the result of the operation is fully determined
by the lefthand operand.
 The instanceof operator returns true if the
object on the lefthand side is an instance of the class or
implements the interface on the righthand side. Otherwise
it returns false. If the lefthand side is
null, it returns false.

