wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes The hashCode() method of java.lang.Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "The hashCode() method of java.lang.Object" Watch "The hashCode() method of java.lang.Object" New topic

The hashCode() method of java.lang.Object

Venkat Ramsimha
Ranch Hand

Joined: Dec 28, 2004
Posts: 127
consider the following code:

public class Test
public static void main(String args[])
int i;
public boolean equals(Object o)//line1
Test t = (Test)o;//line2
if (t.i == this.i)//line3
return true;//line4
return false;//line5
public int hashCode() //line6
return i * 31;//line7


On the above program i have some queries:
1.What does it mean to declare (Object o) in line1
2.What does it mean to declare Test t = (Test)o; in line2
3.What does it mean to declare if (t.i == this.i) in line3
4.What does it mean to declare return true; in line4
5.What does it mean to declare return false; in line5
6.what is the need in this program to implement hashCode() method
7.What is the need of hashCode() method to return i*31;why in particular i*31 value is returned

i would be very thankful if i get the queries to be solved.

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Read this tutorial

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: The hashCode() method of java.lang.Object
Similar Threads
how to revert ArrayList!
casting - shift operators [scjp1.4]
Can you explain this ?
hashcode question