• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to know Two Objects are same?  RSS feed

 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any idea to get to know two methods are equal or not??
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piyush Chaudhary wrote:How to know Two Objects are same?

By using == operator.
But you should read this: AvoidTheEqualityOperator.

Piyush Chaudhary wrote:Is there any idea to get to know two methods are equal or not??

I don't know what do you mean.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to assume you mean objects (from the subject) and not methods. Objects are not methods.

So, how do you know if two objects are the same? It depends on what you mean by "same". If you mean "pointing to the same spot in memory" then use "==".


If you mean "having all the same properties" then use the method "equals".

 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I'm going to assume you mean objects (from the subject) and not methods. Objects are not methods.

So, how do you know if two objects are the same? It depends on what you mean by "same". If you mean "pointing to the same spot in memory" then use "==".


If you mean "having all the same properties" then use the method "equals".



OK.. So Will you please told me that why the following code is giving eq1!=eq2 in the second case? Very thankful to you....
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piyush Chaudhary wrote:
OK.. So Will you please told me that why the following code is giving eq1!=eq2 in the second case? Very thankful to you....


Because they are two different objects -- one created at line 12 and one created at line 13. If you want to check for value equality, then you need to implement an equals() method that define what that means.

Henry
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're writing your own class, then you have to override the equals method yourself. In your case it might look like this:



But you also have to override the "hashCode" method:

 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to use an equals method?? I think I used it

Please Tell me if this was wrong!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piyush Chaudhary wrote:Tell me if this was wrong!


It is not wrong. You used it correctly.

However, you never defined an equals() method, which means that it is inherited from the Object class. And this implementation defines it as equal when it passes the "==" (instance equality) test.

Henry
 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really, I had google it as much as I can. But there is nothing going on my brain...
Is (==) operation & .equals() mehod is same meaning???
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. == test for identity, not equality.
 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will you please elaborate. Thanks in advance..
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piyush Chaudhary wrote:Will you please elaborate. Thanks in advance..


Elaborate what? Everything has been explained, including complete code examples (by Knute) on how to (possibly) implement the equals(). What is it are you still confused with?

Henry
 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two people walk into a testing center.

The first Person walks up to the receptionist and says, "Hi. My name is John and my ID is A1."

The receptionist takes a note of this.


First Person goes to take a seat.

The second Person walks up to the receptionist and says, "Hi. My name is John and my ID is A2."

The receptionist makes a note of this too.


"Hmm...." The receptionist, being an exceedingly bright high school dropout, thinks to themself, "Hey! They have the the same name? Are they the exact same person, I wonder?"
"If they are I should call the police, something weird is going on!"
The receptionist quickly runs this evaluation:

It returns false.

"Oh," said the receptionist, slightly dejected at the fact that no weird space-time anomalies were taking place. "Oh, but... they have the same name!"
The receptionist tries this instead:

The code returns true.

Moral of the story:
'==' is a matter of identity.
.equals() is a matter of equality.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Harris wrote:
Moral of the story:
'==' is a matter of identity.
.equals() is a matter of equality.


As a reminder, which seems to add to the confusion ... the OP has an interpretation of what is equals, and this interpretation is different from the equals() methods that is inherited from the Object class. The OP needs to override to get the two interpretations (of what is equality) the same.

Henry
 
Eric Matthew
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice analogy Tim
 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim...
But something in my mind is still going ..
1- Two objects can be equals or not?
2- If yes then How to show it?

Lets see Via this code:

In above code, two objects eq1 & eq2 have all the properties(String & size) is same. Is those objects are equal??
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you added an equals method to the equalss class like the code I posted, then


would be true, but


would still be false.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piyush Chaudhary wrote:Thanks Tim...
But something in my mind is still going ..
1- Two objects can be equals or not?

Yes, ofcourse. Did you understand what Tim and the others are explaining?
Piyush Chaudhary wrote:
2- If yes then How to show it?

As has been explained above multiple times already, you will need to override the equals() method in your class named equalss. Knute even posted a complete example of how to do this. Did you try putting Knute's code in your program? Did you read the code and understand what it does?
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
== only defines equality for primitive types only.

For Objects, == actually compare the reference value (same as pointer). Thus for Objects, == tests if the 2 reference values point the the same area in memory.

Two objects can occupy different memory space and still contain the same values. In this case you have to define the equals method and compare them manually.

By default, for Object, the equals method just does obj1 == obj2 ;-))

So you have to override it and define it in your own class.

Note that several classes already override the equals() method, namely; String, Integer, etc.

a = new Object(); // memory space allocated
b = a // make b point to same memory space as a
a == b // true

a = new Object(); // memory space allocated
b = new Object(); // different memory space allocated
a == b // false

 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Did you understand what Tim and the others are explaining?

Yes, Jesper, Now Its clear. That, we should override the equals method before applying it because by default it does (obj1==obj2)
Jesper de Jong wrote:
As has been explained above multiple times already, you will need to override the equals() method in your class named equalss. Knute even posted a complete example of how to do this. Did you try putting Knute's code in your program? Did you read the code and understand what it does?

Now I got to know that its compulsory to override the method equals.. Yes I read the code & tried to understand but its not working in my mind...
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piyush Chaudhary wrote:Yes I read the code & tried to understand but its not working in my mind...


Do you have a specific question?

equals() is just a method like any other. It's a little confusing because you have to override the method in Object, and it's not always clear to a beginner that all classes inherit from Object.

Here's the code again with comments:

 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A very very thanks to all of you..
Some basics about equal is get cleared and I think all the other stuff will be clear as I read it more & going deeper in Head First..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!