Java Reference
In-Depth Information
This can be expressed using the conditional operator as follows:
max = (n1 > n2) ? n1 : n2;
The expression on the right-hand side of the assignment statement is the condi-
tional operator expression:
(n1 > n2) ? n1 : n2
The ? and : together form a ternary operator known as the conditional operator. A
conditional operator expression starts with a Boolean expression followed by a ? and
then followed by two expressions separated with a colon. If the Boolean expression is
true , then the value of the first of the two expressions is returned as the value of the
entire expression; otherwise, the value of the second of the two expressions is returned
as the value of the entire expression.
3.2
Boolean Expressions
“Contrariwise,” continued Tweedledee, “if it was so, it might be; and if it
were so, it would be; but as it isn't, it ain't. That's logic.”
LEWIS CARROLL, Through the Looking-Glass
Now that we have motivated Boolean expressions by using them in if-else state-
ments, we will discuss them and the type boolean in more detail. A Boolean expres-
sion is simply an expression that is either true or false . The name Boolean is derived
from George Boole, a 19th-century English logician and mathematician whose work
was related to these kinds of expressions.
Boolean
expression
Simple Boolean Expressions
We have already been using simple Boolean expressions in if-else statements. The
simplest Boolean expressions are comparisons of two expressions, such as
time < limit
and
balance <= 0
A Boolean expression does not need to be enclosed in parentheses to qualify as a Bool-
ean expression, although it does need to be enclosed in parentheses when it is used in
an if-else statement.
Display 3.3 shows the various Java comparison operators you can use to compare
two expressions.
Search WWH ::




Custom Search