wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Hash Code from Dan's Mock Exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Hash Code from Dan Watch "Hash Code from Dan New topic
Author

Hash Code from Dan's Mock Exam

Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi,
I came across the below sentences in one of Dan's explanations for the questions, which i couldn't understand. Can any of you explain the same with the examples,
1) Whenever hashCode() is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified.
Please explain the significance of " provided no information used in equals comparisons on the object is modified."
2) It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results.
If the two objects are unequal according to the equals method, how the hashCodes will be same.


Narasimha
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
If you take a look at the SPI Spec for teh Object class, you can see some great details about the HashCode method. In particular, the logical rules for the HashCode method are as follows:

The general contract of hashCode is:
- Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
- If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
- It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

Funny - sounds a lot like Dan's explanation.
Anyway, let me clarify a bit. For any object, a hashcode can be generated by invoking it's hashCode method. That method generates a value based on the contents of the object. The rules above say that if two objects are equal (via the .equals method), they must return the same hashCode value. Let's look at an example of that:

As you can see, the two objects are considered equivalent via the equals method and they also produce the same hashcode. This only makes sense because the hashcode value is based on the contents of the object. If the contents of two objects are the same, the hashcode generated should be the same.
If we were to change the contents of one of the two classes (we can't in my example because Strings are immutable), you'd find that the hash codes might not be the same anymore.
That seems odd, doesn't it? :roll: It seems unintuitive that we could have two different objects with different contents that produce the same hashcode.
However, let's go back to my example of two strings. How many different strings can you create? I'm no mathematician, but it's a lot. Now, notice that the hashCode() method returns an int. How many different ints can you have? It's a lot, but it's not even close to the number of different strings you could create. Therefore, some strings, even though they're different, will have the same hashcode. That's why it's not required that two objects with different contents return different hashcode values.
I hope that helps,
Corey


SCJP Tipline, etc.
Lawrence Chettiar
Ranch Hand

Joined: Apr 10, 2003
Posts: 62
Hi,
Below is a pretty long code but will answer your both questions
public class HashCodeTest
{
public static void main(String[] args)
{
Integer i = new Integer(4);
Long l = new Long(4);
System.out.println(i.hashCode());
System.out.println(l.hashCode());
System.out.println(i.equals(l));
myHashCode h1=new myHashCode();
h1.a=1;
h1.b=2;
myHashCode h2=new myHashCode();
h2.a=1;
h2.b=2;
System.out.println(h1.hashCode());
System.out.println(h2.hashCode());
System.out.println(h1.equals(h2));
h2.a=2;
h2.b=1;
System.out.println(h1.hashCode());
System.out.println(h2.hashCode());
System.out.println(h1.equals(h2));
}
}
class myHashCode
{
int a,b;
public int hashCode()
{
return a+b;
}
public boolean equals(Object o)
{
if(o instanceof myHashCode)
{
myHashCode other=(myHashCode)o;
if((other.a == this.a) && (other.b==this.b))
return true;
}
return false;
}
};
If you see the hashcode returns the same value even if the myhashcode objects are not equal, but will never return different value if both are equal.
(This hashcode implementation is very simple in the actual system a more complex calculation should be written.)


SCJP 1.4 (86%)<br />SCMAD Beta (77%)<br />SCEA (part I:89%,partII:93%)<br />MCAD (cleared 70-315 & 70-320 [Score 980])
Lawrence Chettiar
Ranch Hand

Joined: Apr 10, 2003
Posts: 62
oops
Corey had already explained by the time I could compose the reply.
Any way extra info is never harmfull
 
Don't get me started about those stupid light bulbs.
 
subject: Hash Code from Dan's Mock Exam