Java Reference
In-Depth Information
15. When the variables are of type int , you test for equality using ==, as follows:
variable1 == variable2
When the variables are of type String , you test for equality using the method
equals , as follows:
variable1.equals(variable2)
In some cases, you might want to use equalsIgnoreCase instead of equals .
16. if (nextWord.compareToIgnoreCase("N") < 0)
System.out.println("First half of the alphabet");
else
System.out.println("Second half of the alphabet");
17. if ( (exam >= 60) && (programsDone >= 10) )
System.out.println("Passed");
else
System.out.println("Failed");
18. if ( (pressure > 100) || (temperature >= 212) )
System.out.println("Emergency");
else
System.out.println("OK");
19. a.
true.
b .
true. Note that expressions a and b mean exactly the same thing. Because
the operators == and < have higher precedence than && , you do not need to
include the parentheses. The parentheses do, however, make it easier to read.
Most people find the expression in option a easier to read than the expression
in option b, even though they mean the same thing.
c .
true.
d .
true.
e .
false. Because the value of the first subexpression, (count == 1) , is false ,
you know that the entire expression is false without bothering to evaluate
the second subexpression. Thus, it does not matter what the values of x and y
are. This is called short-circuit evaluation , which is what Java does.
f .
true. Since the value of the first subexpression, (count < 10) , is true , you
know that the entire expression is true without bothering to evaluate the sec-
ond subexpression. Thus, it does not matter what the values of x and y are.
This is called short-circuit evaluation , which is what Java does.
g .
false. Notice that the expression in g includes the expression in option f as
a subexpression. This subexpression is evaluated using short-circuit evaluation
as we described for option f. The entire expression in g is equivalent to
!( (true || (x < y)) && true )
which in turn is equivalent to !( true && true ) , and that is equivalent to
!(true) , which is equivalent to the final value of false .
Search WWH ::




Custom Search