As the book says, the default behavior of the equals() method is to compare references. v1 and v2 are referencing different objects.
equals is inherited from Object and if you want to tell the method to compare values, then you have to override (write your own method of the same name with the same arguments) with something like:
[ May 19, 2004: Message edited by: Tim McGuire ]
John Smith
Ranch Hand
Joined: Oct 08, 2001
Posts: 2937
posted
0
public boolean equals(Value v){
A small but important correction: the signature of the equals() method is public boolean equals(Object o). In the form quoted above, the equals() method is overloaded, not overriden. As a result, you will see some strange effects. For example:
So, getting equals() right and overriding the hashCode() method could look like this?:
John Smith
Ranch Hand
Joined: Oct 08, 2001
Posts: 2937
posted
0
Tim -- yes, that looks better. The hashCode() method could be simplified, though:
Also, a better test would be:
There is much more to equals() and hashCode(). The best reference on this subject (and many others) is Effective Java [ May 21, 2004: Message edited by: Eugene Kononov ]