Java Reference
In-Depth Information
Not equals ( != )
The != operator is exactly the opposite of the == operator. It evaluates to true if
its two primitive operands have different values or if its two reference operands
refer to different objects or arrays. Otherwise, it evaluates to false .
The relational operators can be used with numbers and characters, but not with
boolean values, objects, or arrays because those types are not ordered. Java pro‐
vides the following relational operators:
a x
Less than ( < )
Evaluates to true if the first operand is less than the second.
Less than or equal ( <= )
Evaluates to true if the first operand is less than or equal to the second.
Greater than ( > )
Evaluates to true if the first operand is greater than the second.
Greater than or equal ( >= )
Evaluates to true if the first operand is greater than or equal to the second.
Boolean Operators
As we've just seen, the comparison operators compare their operands and yield a
boolean result, which is often used in branching and looping statements. In order
to make branching and looping decisions based on conditions more interesting
than a single comparison, you can use the Boolean (or logical) operators to combine
multiple comparison expressions into a single, more complex expression. The
Boolean operators require their operands to be boolean values and they evaluate to
boolean values. The operators are:
Conditional AND ( && )
This operator performs a Boolean AND operation on its operands. It evaluates
to true if and only if both its operands are true . If either or both operands are
false , it evaluates to false . For example:
if ( x < 10 && y > 3 ) ... // If both comparisons are true
This operator (and all the Boolean operators except the unary ! operator) have
a lower precedence than the comparison operators. Thus, it is perfectly legal to
write a line of code like the one just shown. However, some programmers pre‐
fer to use parentheses to make the order of evaluation explicit:
if (( x < 10 ) && ( y > 3 )) ...
You should use whichever style you find easier to read.
This operator is called a conditional AND because it conditionally evaluates its
second operand. If the first operand evaluates to false , the value of the expres‐
sion is false , regardless of the value of the second operand. Therefore, to
Search WWH ::




Custom Search