File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Equals and == with Objects Problem" Watch "Equals and == with Objects Problem" New topic

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)

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

Joined: Nov 18, 2008
Posts: 433
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: 446

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%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155

I agree. Here's the link:
subject: Equals and == with Objects Problem
It's not a secret anymore!