• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compare objects

 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

output: false
true
isn't supposed that the first output be 'true' since they are both the same object? however the second output is correct as expected.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).

http://java.sun.com/j2se/1.4.1/docs/api/java/lang/Object.html#equals
This is the main reason it returns false.
Now, String class overrides equals, so when you do equals() it returns true.

String and wrapper classes override equals to check for values.

From KnB book.
I hope this helps. let me know
[ July 29, 2003: Message edited by: Andres Gonzalez ]
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing the StringBuffer class doesn't overrides the equals() method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic