aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how to solve this hashcode() example.? 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 "how to solve this hashcode() example.?" Watch "how to solve this hashcode() example.?" New topic
Author

how to solve this hashcode() example.?

Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
public class X
{
private int a;
private int b;
public void setA(int i){ this.a = i; }
public int getA(){ return this.a; }
public void setB(int i){ this.b = i; }
public int getB(int b){ return b; }
public boolean equals(Object obj)
{
return ( obj instanceof X && this.a == ((X) obj).a );
}
public int hashCode()
{
//1
}
}

Which of the following options would be valid at //1?

a. return 0;
b. return a;
c. return a+b;
d. return a*a;
e. return a/2;

How does this work?
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Originally posted by nik arora:
public class X
{
private int a;
private int b;
public void setA(int i){ this.a = i; }
public int getA(){ return this.a; }
public void setB(int i){ this.b = i; }
public int getB(int b){ return b; }
public boolean equals(Object obj)
{
return ( obj instanceof X && this.a == ((X) obj).a );
}
public int hashCode()
{
//1
}
}

Which of the following options would be valid at //1?

a. return 0;
b. return a;
c. return a+b;
d. return a*a;
e. return a/2;

How does this work?



I would choose a,b,d,e.
These four match to the equals() contract.
"b" is not being used in the equals() method, so it wont be fine to include
that in hashCode() generation.

equals() can be more specific than hashCode() but vice versa is not true

Can anybody please comment on this bold line!


cmbhatt
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi chandra,
Can you explain me how those 4 answers what you mentioned satisfies
equals() contract?
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Originally posted by nik arora:
Hi chandra,
Can you explain me how those 4 answers what you mentioned satisfies
equals() contract?


Ok!

a. return 0;
b. return a;
c. return a+b;
d. return a*a;
e. return a/2;

Let us take options one by one:

a-
Your equals method says, two objects are equal when their a value matches.
Now if equals(...) method returns true for two objects their hashCode() must
be equal. If your hashCode() returns 0, it is ok although it is not efficient because it returns 0 for all regardless they are equal or not.

b- It is correct because hashCode() will the value of a. It is also fine.
Two objects with the same a value are equal.

c- a+b is incorrect because, you are not using b in your equals method.
It will return wrong hashCode for two objects with same a value.
Suppose
for object1 a=5, b=10;
for object2 a=5, b=12;
your equals() method will return true because it is comparing on behalf of value of "a", but hashCode() of these two objects will be different (15 for
object1 and 17 for object2) that is against the equals() hashCode() contract.

d- and e- can easily be understood if you get the above description well.
a*a and a/2 will return same for two objects with the same "a" value.
In the option e you are dividing by constant so it will always be same for a value "a" if "a" does not change.
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Thanks chandra that was a cool explanation.
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi Chandra,
I understood what you explained. I get confused when i see these type of questions my doubt is how to determine the correct hashcode()
implementation for the equals() method. Can you explain me with one more example?
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Originally posted by nik arora:
Hi Chandra,
I understood what you explained. I get confused when i see these type of questions my doubt is how to determine the correct hashcode()
implementation for the equals() method. Can you explain me with one more example?


We may encounter such a question in the mocks:







It is legal and appropriate to return the hashCode of the String (in our case name) while we are using name in the equals() method. Two employee objects are equal when their names are equal.
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Explain this code i didnot understand how it exactly works:

public int hashcode()
{
return name.hashcode();
}
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Originally posted by nik arora:
Explain this code i didnot understand how it exactly works:

public int hashcode()
{
return name.hashcode();
}


Nothing special there, hashCode() method of the employee class returns the the default hashcode that String name has inherited from the Object class.

Actually hashCode() and equals(...) methods are Object class methods, we override them according to our need. If you dont override the equals() and hashCode() method of the Object class for your class, the default implementation of the equals() and hashCode() will be used.

hashCode() method of the Object class returns the integer value computed from the memory location of the object.

so name.hashCode() means something computed from the memory address of the
object name refers to.

Object class implementation of the equals() method


If two reference variables are referring to same object on the heap, they are equal.
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hey Chandra thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to solve this hashcode() example.?