aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashcode method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashcode method" Watch "hashcode method" New topic
Author

hashcode method

janki tangeda
Ranch Hand

Joined: Jun 07, 2005
Posts: 54
class B {
private int i1;
public int hashCode() {return 1;}
}
class C {
private int i1;
public int hashCode() {return -1;}
}
class D {
private int i1;
public int hashCode() {return i1;}
}

Why are the first two implementations of hashcode not appropriate though it won't violate the hashcode error and won't give any error?
Anurag Jain
Greenhorn

Joined: Jul 23, 2002
Posts: 8
In the first 2 methods, the return value will be identical for all objects, thus it will be inefficient. In the 3rd case the value will be based on the variable which will be less likely to return same hash code.


--<br />Anurag
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

"Anuraag"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Anurag Jain
Greenhorn

Joined: Jul 23, 2002
Posts: 8
I updated it, thanks for pointing out!
janki tangeda
Ranch Hand

Joined: Jun 07, 2005
Posts: 54
got it..thanks Anurag
shyam ramineni
Ranch Hand

Joined: Jul 08, 2005
Posts: 43
Hi,

I have tried the below code.

I think the third option is also not correct as hashCode() method must consistently return the value for a object.
Am I right or wrong?

Thanks
Shyam Ramineni


<a href="http://hyderabaditeducation.com" target="_blank" rel="nofollow">http://hyderabaditeducation.com</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashcode method
 
Similar Threads
Dan's Collection
Question based on hashcode()
Random question
hashCode Question
HashCode and equals