Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object References and equality between Objects

 
pagano monello
Ranch Hand
Posts: 38
1
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have read a sentence on "OCA JavaSE7 Certification Guide" by Mala Gupta at pag. 214, that lets me really puzzled. I reproduce below the sentence that I am referring to:

Two objects are equal if their object references (the variables that store them) point to the same object


What I think is that, if two object references point to the same object, then it is the same object, and not to equal objects.

Does anyone has a diffrent interpretation of this sentence?

Thanks in advance.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pagano monello wrote:
Two objects are equal if their object references (the variables that store them) point to the same object


What I think is that, if two object references point to the same object, then it is the same object, and not to equal objects.

Does anyone has a diffrent interpretation of this sentence?

If that statement is about the equals method of class Object, the study guide is spot-on! For any non-null reference values x and y, the equals method of class Object returns true if and only if x and y refer to the same object (x == y has the value true).

Hope it helps!
Kind regards,
Roel
 
pagano monello
Ranch Hand
Posts: 38
1
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is about the equals method.

Thank you then Roel.
 
pagano monello
Ranch Hand
Posts: 38
1
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry, really, but I have to come back to this issue. I have read that, by executing the following code


I create two different objects; for what I have understood, indeed, these two object aren't stored in the String pool. If now I compare these two different objects by using equals method, I get true; so I cannot understand really well what Roel wrote.

I am sorry again, but I am a ittle bit confused
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pagano monello wrote:
I create two different objects; for what I have understood, indeed, these two object aren't stored in the String pool. If now I compare these two different objects by using equals method, I get true; so I cannot understand really well what Roel wrote.

What I have explained in my previous post, is about the equals method of the Object class. Not the equals method of the String class (which overrides the equals method of the Object class and returns true if and only if both are non-null String objects representing the same sequence of characters).

The 1st line uses the == operator to check if both reference variables refer to the same object (which uses the same code as the equals method of the Object class) and therefore (as expected) prints false

Hope it helps!
Kind regards,
Roel
 
pagano monello
Ranch Hand
Posts: 38
1
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, now it's perfectly clear to me.

Thank you really Roel.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic