This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equals()" Watch "equals()" New topic
Author

equals()

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477


when we don't override eqauls() and hashCode() methods,Object.equlas() gives false and hashcode() may be true or false value returning.
But i don't get the part why do we need to override those two method what is the wrong happening here?

please guide me on those.
[ April 14, 2007: Message edited by: Shiva Mohan ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18499
    
  40

But i don't get the part why do we need to override those two method what is the wrong happening here?


Well, you don't have to override those two methods.

The default equals() method (from Object class) will return the value of true, if two references are referring to the same instance. The default hashCode() method will use the identity hash as the hashcode. This hashcode will very likely be different for different instances, but may not be as there can only be so many different values for hashcode.

If this behavior is fine for your purposes, then there is no need to override those two methods.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Thanks for the reply Henry.

if the behavior is not fine for purposes, (Actually i have no idea when we really need to override those two methods regarding hashtable)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18499
    
  40

Originally posted by Shiva Mohan:
if the behavior is not fine for purposes, (Actually i have no idea when we really need to override those two methods regarding hashtable)


Hate to point out the obvious but it's instances of your class, designed for your program. It's your decision whether two instances of your class are equal or not. There is no simple answer as it is a design issue -- you have to decide how instances of your class should be treated, and whether the default methods is what you want.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals()
 
Similar Threads
HashMap
hashmap and equals doubts
Sets (Universal, Subsets, Union)
hashCode & HashMap doubts
.equals()-Regarding collections