Java Reference
In-Depth Information
23
.
To use the method
compareTo
, you use the expression:
str1.compareTo(str2)
where
str1
and
str2
are
String
variables. Moreover,
str2
can also be a
String
constant (literal). The expression
str1.compareTo(str2)
evalu-
ates as follows:
(
an integer value less than
0
if string
str1
is less than string
str2
0
if string
str1
is equal to string
str2
an integer value greater than
0
if string
str1
is greater than string
str2
str1.compareTo(str2)
=
EXERCISES
1.
Mark the following statements as true or false.
a.
The result of a logical expression cannot be assigned to an
int
variable.
b.
In a one-way selection, if a semicolon is placed after the expression in an
if
statement, the expression in the
if
statement is always
true
.
c.
Every
if
statement must have a corresponding
else
.
d.
The expression:
(ch >= 'A' && ch <= 'Z')
evaluates to
false
if either
ch < 'A'
or
ch >= 'Z'
.
Suppose the input is
5
. (Assume that all variables are properly declared.)
The output of the code:
e.
num = console.nextInt();
if
(num > 5)
System.out.println(num);
num = 0;
else
System.out.println("Num is zero");
is:
Num is zero.
f.
The expression in a
switch
statement should evaluate to a value of any
primitive data type.
g.
The expression
!(x > 0)
is true only if
x
is a negative number.
h.
In Java, both
!
and
!=
are logical operators.
i.
The order in which statements execute in a program is called the flow
of control.
Search WWH ::
Custom Search