THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

The output is


17 17 18


The expression ++i preincrements the value of i to 17 and evaluates to that value (17); the expression i++
evaluates to the current value of i (17) and postincrements i to have the value 18; finally the expression i is
the value of i after the postincrement from the middle term. Modifying a variable more than once in an
expression makes code hard to understand, and should be avoided.


The increment and decrement operators ++ and -- can also be applied to char variables to get to the next or
previous Unicode character.


9.2.2. Relational and Equality Operators


The language provides a standard set of relational and equality operators, all of which yield boolean values:


> greater than

>= greater than or equal to

< less than

<= less than or equal to

== equal to

!= not equal to

Both the relational and equality operators can be applied to the primitive numeric types, with the usual
mathematical interpretation applying.


Floating-point values follow normal ordering (1.0 is less than 0.0 is less than positive infinity) except that
NaN is an anomaly. All relational and equality operators that test a number against NaN return false, except
!=, which always returns true. This is true even if both values are NaN. For example,


Double.NaN == Double.NaN


is always false. To test whether a value is NaN, use the type-specific NaN testers: the static methods
Float.isNaN(float) and Double.isNaN(double).[2]


[2] There is one other way to test for NaN: If x has a NaN value, then x != x is true. This is
interesting but very obscure, so you should use the methods instead.

Only the equality operators == and != are allowed to operate on boolean values.


These operators can be combined to create a "logical XOR" test. The following code invokes sameSign only
if both x and y have the same sign (or zero); otherwise, it invokes differentSign:


if ((x < 0) == (y < 0))
sameSign();
else

Free download pdf