File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes compare objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "compare objects" Watch "compare objects" New topic
Author

compare objects

Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522

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.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
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
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522

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

Joined: Nov 27, 2001
Posts: 1561
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

Joined: Apr 13, 2003
Posts: 1088
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

Joined: Apr 13, 2003
Posts: 1088
One more thing the StringBuffer class doesn't overrides the equals() method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compare objects
 
Similar Threads
equals()
Regardind null....
Differene between == and equals() method
Strings
one more with Strings ( == and equals)