• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Equals method.

 
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.
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • 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 ]
 
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 VI Editor Tomcat Server
  • 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.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • 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
 
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 VI Editor Tomcat Server
  • 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 ]
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic