Page 545 - Introduction to Programming with Java: A Problem Solving Approach
P. 545
meaning associated with the == operator when it is employed to test the equality of two reference variables. That operator also returns true if and only if both references refer to exactly the same object.
Suppose you have a Car class with three instance variables, make, year, and color, and you have a constructor that initializes these instance variables with corresponding argument values. Suppose this Car class does not define an equals method itself, and the only equals method it inherits is the one it inher- its automatically from the Object class. The following code illustrates that the equals method inherited from the Object class does exactly the same thing as the == operator does.
Car car1 = new Car("Honda", 2008, "red");
Car car2 = car1;
Car car3 = new Car("Honda", 2008, "red");
System.out.println(car2 == car1); ⎫ ⎬ System.out.println(car2.equals(car1)); ⎭
System.out.println(car3 == car1); ⎫ ⎬
System.out.println(car3.equals(car1)); ⎭ Output:
true
true
false
false
a
m
it
me
t
h
h
s
re
en
eo
s
a
nt
ob
am
m
e
13.3 The equals Method 511
tn
bj
na
je
a
m
ec
c
t
me
t
es
sf
f
o
or
r
s
sa
d
d
i
if
f
f
fe
er
re
e
n
nt
t
o
ob
b
j
j
e
ec
c
t
t
s
s
w
wi
ea
at
tt
tr
ri
i
b
bu
ut
te
e
s
s
This narrow sense of the word “equals” is not always what you want. For example, suppose your spouse
Apago PDF Enhancer
decides to buy a new car and goes to a particular auto dealer and orders a red 2008 Honda as suggested by the above car1 instantiation. When you see the brochures your spouse brings home, you’re impressed and decide you would like a new car for yourself too. You’d like it to be just like your spouse’s car except for the color, which you want to be blue. So you go to the same dealer and say “I want the same car my spouse just ordered, but I want the color to be blue.” A month later the dealer calls both you and your spouse at your separate places of work and says to each of you separately, “Your car is ready. Please come in to pick it up at 5:30 pm this afternoon.” You both show up as requested, and the dealer takes you outside and proudly exclaims, “Here it is. How do you like it?” You say “Great, it’s just like I wanted!” Then your spouse says, “But where is my car?” And the dealer replies, “But I thought you were to be joint owners of the same car, and your spouse told me to change the color of that car to blue.” Oops, somebody made a mistake. . . .
The mistake occurred in the communication between you and the dealer when you said, “the same car.” You meant the second meaning above: objectA and objectB are two separate objects which have the same attributes. But the dealer heard the first meaning above: objectA is just another name for objectB, and both objectA and objectB refer to exactly the same object.
Defining Your Own equals Method
Now let’s see how you can implement the second meaning. To do so, include in your class an explicit version of an equals method that tests for equal attributes. Then, when your program runs, and an instance of your class calls the equals method, your equals method takes precedence over Object’s equals method, and the JVM utilizes the equals method you defined. The equals method in Figure 13.1’s Car class tests for equal attributes by comparing the values of all three instance variables, that is, the object’s attributes. It returns true only if all three instance variables have the same values, and it returns false otherwise. Notice that this equals method includes two subordinate equals method calls—one made by the make
d
d
i
if
ff
fe
er