Java Reference
In-Depth Information
Therelationaltype-checkingoperatorisusedtodeterminewhetheranobjectbelongs
to a specific type. This topic is discussed in Chapter 2 .
Shift Operators
Theshiftoperatorsconsistofleftshift( << ),signedrightshift( >> ),andunsignedright
shift( >>> ).Leftshiftshiftsthebinaryrepresentationofitsleftoperandleftwardbythe
numberofpositionsspecifiedbyitsrightoperand.Eachshiftisequivalenttomultiply-
ingby2.Forexample, 2 << 3 shifts2'sbinaryrepresentationleftby3positions;the
result is equivalent to multiplying 2 by 8.
Eachofsignedandunsignedrightshiftshiftsthebinaryrepresentationofitsleftop-
erandrightwardbythenumberofpositionsspecifiedbyitsrightoperand.Eachshiftis
equivalent to dividing by 2. For example, 16 >> 3 shifts 16's binary representation
right by 3 positions; the result is equivalent to dividing 16 by 8.
The difference between signed and unsigned right shift is what happens to the sign
bitduringtheshift.Signedrightshiftincludesthesignbitintheshift,whereasunsigned
right shift ignores the sign bit. As a result, signed right shift preserved negative num-
bers,butunsignedrightshiftdoesnot.Forexample, -4 >> 1 (theequivalentof -4/2 )
evaluates to -2, whereas -4 >>> 1 evaluates to 2147483646.
Tip The shift operators are faster than multiplying or dividing by powers of 2.
Unary Minus/Plus Operators
Unary minus ( - )and unary plus ( + )are the simplest ofall operators. Unary minus re-
turns the negative of its operand (such as -5 returns -5 and --5 returns 5 ), whereas
unaryplusreturnsitsoperandverbatim(suchas +5 returns 5 and +-5 returns -5 ).Un-
ary plus is not commonly used, but is present for completeness.
Precedence and Associativity
Whenevaluatingacompoundexpression,Javatakeseachoperator's precedence (level
ofimportance)intoaccounttoensurethattheexpressionevaluatesasexpected.Forex-
ample, when presented with the expression 60+3*6 , we expect multiplication to be