Java Reference
In-Depth Information
int x, y, z; // x, y, and z are all declared
x = 42; // x is given the value of 42
y = x++; // y is given x's value (42) before it is incremented
// and x is then incremented to 43
z = ++x; // x is incremented to 44, and z is given x's value
CAUTION
As with shorthand operators, increment and decrement operators
in extremely complex expressions can produce results you might
not have expected.
The concept of “assigning x to y before x is incremented” isn't
precisely right because Java evaluates everything on the right side
of an expression before assigning its value to the left side.
Java stores some values before handling an expression to make
postfix work the way it has been described in this section.
When you're not getting the results you expect from a complex
expression that includes prefix and postfix operators, try to break
the expression into multiple statements to simplify it.
2
Comparisons
Java has several operators for making comparisons among variables, variables and liter-
als, or other types of information in a program.
These operators are used in expressions that return Boolean values of true or false ,
depending on whether the comparison being made is true or not. Table 2.5 shows the
comparison operators.
TABLE 2.5
Comparison Operators
Operator
Meaning
Example
Equal
==
x == 3
Not equal
!=
x != 3
Less than
<
x < 3
Greater than
>
x > 3
Less than or equal to
<=
x <= 3
Greater than or equal to
>=
x >= 3
 
Search WWH ::




Custom Search