Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals() problem

 
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



I'm not getting,why objects are not equal here
 
Ranch Hand
Posts: 174
Java ME Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mums override equals method and do your comparing stuff there.
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To give a bit more detail: since you don't override the equals method, InstEqual inherits the equals method from the Object class. That just checks to see if they are the same object (obj1 == obj2). If you want equality to be defined in a different way (for instance, according to obj.value in this case), then you override the method to define it.

And if you override equals(), you really ought to override hashCode() as well to be consistent.
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote: if you override equals(), you really ought to override hashCode() as well to be consistent.





But i am not getting why to override hashCode() and what is need here because my program is working well.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without overriding hashCode() you'll fine, until you do something that relies on it. Most likely, that will involve putting your objects in a HashMap or HashSet. Then it won't work quite as you expected. If you look at the documentation for Object.equals() and Object.hashCode() it explains how they are supposed to work.

I've got a couple of comments about your equals method:

1. You should check obj instanceof InstEqual before casting it, to avoid the possible exception. The should return false if the cast would fail.

2. Instead of:
it's easier to read if you just use:


 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:Without overriding hashCode() you'll fine, until you do something that relies on it. Most likely, that will involve putting your objects in a HashMap or HashSet. Then it won't work quite as you expected. If you look at the documentation for Object.equals() and Object.hashCode() it explains how they are supposed to work.

I've got a couple of comments about your equals method:

1. You should check obj instanceof InstEqual before casting it, to avoid the possible exception. The should return false if the cast would fail.

2. Instead of:
it's easier to read if you just use:




thank you matthew for detailed explanation and i really like that shortcut
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1. always override hashcode if you override equals.
2. follow all contracts of equals specified in javadoc.
3. if you are implementing comparable then make sure equals() and compareTo() must be compatible to each other . ie. compareTo() must return zero if two object are equal with equals method.

you can also check my post How to correctly override equals in java
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Javin Paul wrote:
thank you Javin for help

 
How do they get the deer to cross at the signs? Or to read this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic