Programming and Problem Solving with Java

(やまだぃちぅ) #1

(^158) | Selection and Encapsulation
We can use relational operators to compare not only variables or constants, but also the
values of arithmetic expressions. In the following table, we compare the results of adding 3
to xand multiplying yby 10 for different values of xand y:
Value of x Value of y Expression Result
12 2 x + 3 <= y 10 true
20 2 x + 3 <= y
10 false
71 x + 3 != y 10 false
17 2 x + 3 == y
10 true
100 5 x + 3 > y * 10 true
Caution:It’s easy to confuse the assignment operator (=) and the relational operator (==).
These two operators have very different effects in your code, however. Some people pro-
nounce the relational operator as “equals-equals” to remind themselves of the difference.


Comparing Strings You cannot compare strings using the relational operators. Syntactically, Java


lets you write the comparisons for equality (==) and inequality (!=) between values of class
String, but such a comparison is not what you typically want. Recall from Chapter 2 that
Stringis a reference type. That is, the content of aStringvariable is the memory address for
the beginning of the string object. When you assign one string to another, Java simply copies
this address. Similarly, when you compare two strings, Java checks whether they have the
same address. It doesnotcheck whether they contain the same sequence of characters.
Forgetting that Java makes comparisons involving strings in this manner, and mistak-
enly using the ==or !=operator, is a source of some insidious errors. Sometimes, the com-
parison seems to work; at other times it fails. The reason is that most Java compilers are quite
clever about how they store Stringliterals. If you type the same literal in two different places
in your code, the compiler recognizes their equality and stores the character sequence just
once; it then uses the same address in the Bytecode. Thus, comparing a Stringliteral to a vari-
able that has been assigned an identical literal elsewhere in the program is likely to indicate
that they are equal (if the Java compiler is well designed).
On the other hand, if you get a string from aBufferedReaderobject and compare it to a
Stringliteral, the two must always compare as unequal, even when they contain the exact
same sequence of characters. The string from theBufferedReaderand theStringliteral are
stored in different places in memory, which means that their addresses compare as unequal.
Rather than using the relational operators, we compare strings with a set of value-
returning instance methods that Java supplies as part of the Stringclass. Because they are
instance methods, the method name is written following a Stringobject, separated by a dot.
The string to which the method name is appended is one of the strings in the comparison,
and the string in the argument list is the other string to be compared. Because we sometimes
want to compare strings ignoring capitalization, the Stringclass provides methods called
toLowerCaseand toUpperCasethat convert all the characters of a string to lowercase or up-
Free download pdf