• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Equals method.

 
Mubeen Shaik
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am not understandng thic concept. Why it is giving "False" as result.
This code is from Thinking in Java.

In the same code if i remove static from this code it is giving me error message. Please explain this concept also.

static Test monitor = new Test();





Thanks in advance,
Mubeen Shaik.
 
Mubeen Shaik
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think i have posted this in wrong place. Could you please move this to Java Intemediate forum.


Thanks,
Mubeen Shaik.
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the book says, the default behavior of the equals() method is to compare references. v1 and v2 are referencing different objects.

equals is inherited from Object and if you want to tell the method to compare values, then you have to override (write your own method of the same name with the same arguments) with something like:

[ May 19, 2004: Message edited by: Tim McGuire ]
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public boolean equals(Value v){

A small but important correction: the signature of the equals() method is public boolean equals(Object o). In the form quoted above, the equals() method is overloaded, not overriden. As a result, you will see some strange effects. For example:

 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ooops!
thanks for pointing that out.
 
Mubeen Shaik
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eugene,

I am confused here, then how to Override the method?

Thanks,
Mubeen Shaik.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
overload:

overwrite:
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or simply
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And don't forget to override hashcode method if you override equals.

- Manish
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, getting equals() right and overriding the hashCode() method could look like this?:

 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim -- yes, that looks better. The hashCode() method could be simplified, though:



Also, a better test would be:


There is much more to equals() and hashCode(). The best reference on this subject (and many others) is Effective Java
[ May 21, 2004: Message edited by: Eugene Kononov ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic