isn't supposed that the first output be 'true' since they are both the same object? They might be the same object type, but the reference variables points to different objects. try this:
What's the result? [ July 29, 2003: Message edited by: Andres Gonzalez ]
I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Joined: Jul 21, 2003
They might be the same object type, but the reference variables points to different objects.
if this is the case then System.out.println("s1.equals(s2)? " + s1.equals(s2)) should output false (which isn't the case) please also note that the purpose of my question is to understand the different behaviour of the test and String objects [ July 29, 2003: Message edited by: Vicken Karaoghlanian ]
Joined: Nov 27, 2001
Ohh.. i see.. sorry The problem here is with the equals() method. The String class overrides the equals() method from Object, so when you do this:
Test is not overriding equals, so
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).
Hi Vicken As Andres pointed out the output is so because the Object's class equals() method compares for object equality by checking if the references to both the objects point to the same thing or not. But in the String class the String class overrides equals() method to provide the equals method with functionality that two strings would be considered equal if their contents are equal. Code for the Object's equals() method :
Code for String's equals() method :
Joined: Apr 13, 2003
One more thing the StringBuffer class doesn't overrides the equals() method.