The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is the out put of this ? 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 "what is the out put of this ? " Watch "what is the out put of this ? " New topic

what is the out put of this ?

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 481
class A {
int i1, i2;
public void setI1(int i) {i1 = i;}
public int getI1() {return i1;}
public void setI2(int i) {i2 = i;}
public int getI2() {return i2;}
public A(int ii1, int ii2) {i1 = ii1; i2 = ii2;}
public boolean equals(Object obj) {
if (obj instanceof A) {
return (i1 == ((A)obj).getI1());
return false;
public int hashCode() {
// Insert statement here.

Which of the following statements could be inserted at the specified location without violating the hash code contract?
a. return 31;
b. return getI1();
c. return getI2();
d. return 31 * getI1() + getI2();

The answer given is
a. return 31;
b. return getI1();

why is the (a) is the right answer and how ?

please explian this any one
marc weber

Joined: Aug 31, 2004
Posts: 11343

Do you know what the "hash code contract" is? (HINT: See the API for Object.)

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Santana Iyer
Ranch Hand

Joined: Jun 13, 2005
Posts: 335
concept is if two objects are equal than their hashCode must be same.
however if two objects are not equal than their hashCode may be equal or may not be equal.

you are giving same hashCode for every object (return 31

so that satisfies first statement. if two objects are equal than hashCode is equal. though it is inefficient it is legal.
[ September 07, 2005: Message edited by: Santana Iyer ]
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 481
please explain me more clear

i am understand where do i get the value 31 is it default value for the hashcode.
Joshua Smith
Ranch Hand

Joined: Aug 22, 2005
Posts: 193
Santana is right. Answer A is technically correct as it technically honors the contract, but it would not normally be used in the real world. Answer B is a better answer since it uniquely identifies the object. But for the sake of certification, you have to answer A and B.

From the API for Object:

Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.
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.

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

The "it is not required..." paragraph is why answer A is technically true. The "As much as is reasonably practical..." paragraph is the reason why answer B is something that you would want to do in the real world.


Rational Pi Blog - Java, SCJP, Dev Bits-
I agree. Here's the link:
subject: what is the out put of this ?
It's not a secret anymore!