Java Reference
In-Depth Information
Sample Run: (In this sample run, the user input is shaded.)
Enter the tax filing status: married
Enter filing joint/separately: joint
Filing status: married joint
Standard exemption: $12000.00
The preceding output is self-explanatory. The details are left as an exercise.
In addition to the method
compareTo
,youcanalsousethemethod
equals
of the
class
String
to determine whether two
String
objects contain the same value.
However, the method
equals
returns the value
true
or
false
.Forexample,the
expression:
str1.equals("Hello")
evaluates to
true
, while the expression:
str1.equals(str2)
evaluates to
false
, where
str1
and
str2
are as defined in Example 4-26.
You can apply the relational operators
==
and
!=
to variables of the
String
type, such
as the variables
str1
and
str2
. However, when these operators are applied to these
variables they compare the values of the variables, not the values of the
String
objects
they point to. For example, suppose, as in Figure 4-6:
str1 = "Hello";
str2 = "Hi";
str1
Hello
str2
Hi
FIGURE 4-6
Variables
str1, str2,
and the objects to which they point
The expression (
str1 == str2
) determines whether the values of
str1
and
str2
are the same, that is, if
str1
and
str2
point to the same
String
object.
Similarly, the expression (
str1 != str2
) determines whether the values of
str1
and
str2
are not the same, that is, if
str1
and
str2
do not point to the same
String
object.
Search WWH ::
Custom Search