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

== operator and equals method

 
Vipul Chauhan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.
== compares reference variable

2.
default implementation of equals method (
object o = new object();
o.equals(o2);
)
compares whether 2 obj variables refers to the same object.

question.

does that mean default equals is same as == ?
please explain with reason



thanks
 
E Armitage
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vipul Chauhan wrote:
does that mean default equals is same as == ?
please explain with reason
thanks

What do you think is the answer and what is your reason?
 
Vipul Chauhan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think both are same
for example

class B{}
which doesnt override equals method

B b1 = new B();
B b2= new B();


b1==b2 and b1.equals(b2); will have same answer.
 
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
And is the answer true, false or something else? Once you have made your decision, you can always verify your answer by writing (and running) a little program.
 
Vipul Chauhan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if we consider output then both are same.

as both are different objects == will give false
i dont know how equals method gives false

please correct me if i am wrong anyware.
 
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
Vipul Chauhan wrote:as both are different objects == will give false



Vipul Chauhan wrote:i dont know how equals method gives false

Given class B (from the post above) how does its equals-method look like?
 
Vipul Chauhan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it wil use equals method defined in class Object
 
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
That's correct! And how does the implementation in Object's class look like?
 
K. Tsang
Bartender
Posts: 3583
16
Android Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know I know.

But I let the OP answer
 
meghana chintanippu
Ranch Hand
Posts: 42
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as far as i know default equals() has same implemetation as that of == operator.
but as it a method it can be overriden and can have the overriden impleamentation of the class and as == is operator it always refer to refernce variables.
 
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
meghana chintanippu wrote:as far as i know default equals() has same implemetation as that of == operator.

That's correct too! So do you know now why equals method gives false?
 
meghana chintanippu
Ranch Hand
Posts: 42
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here the equals() is not overridden so it will show its default implementation that it checks the address of reference variables and finds it different so prints false
 
Vipul Chauhan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meghana chintanippu wrote:here the equals() is not overridden so it will show its default implementation
is the answer i guess
 
Henry Wong
author
Marshal
Pie
Posts: 22085
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As already hinted at, but a few ranchers ... here is the equals() method implementation of the Object class, which can be obtained by looking at the source code...



Henry
 
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
Vipul Chauhan wrote:
meghana chintanippu wrote:here the equals() is not overridden so it will show its default implementation
is the answer i guess

But for what reason do you guess? All the pieces are here, you just have to put them in the correct order.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic