wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Equals and == with Objects Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Equals and == with Objects Problem" Watch "Equals and == with Objects Problem" New topic
Author

Equals and == with Objects Problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634



Cannot understand the above code



As I Know,
o2 refers to String with value false.since ,new operator is used there would 2 strings one, stored on heap and other in string literal pool
o4 refers to string false by

so,it would refer to string in literal pool.
Thus,o4 should be equal to o3

Since equals of object class would be used as o2 is of type Object,equals would use == for comparison(Equals of object class would be used)
Then,


if o2.equals(o4) returns true.Then,how is o2==o4 false,???


DOES equals of object called would be used or of Boolean or String ??


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
I am not sure if I understood you completely.

mohitkumar gupta wrote:
Thus o4 should be equal to o3


Of what type is o3? What are you comparing it with?


if o2.equals(o4) returns true.Then,how is o2==o4 false,???


Because, as you explained yourself, you have different String objects that are equal but not identical.

Due to polymorphism the method from the current object gets called. If you have a String reference, then String's equals method would be called.
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

even I am not sure whether I understood your question or not
but I will try to explain the code line by line
here it is


hope this makes sense

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155


OCPJP6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Equals and == with Objects Problem
 
Similar Threads
Equals and ==
Again in Problem with equals()
doubt about references
Sorting the Rows in a JTable Component Based on a Column
String concatenation