• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to solve this hashcode() example.?

 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi chandra,
Can you explain me how those 4 answers what you mentioned satisfies
equals() contract?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks chandra that was a cool explanation.
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Explain this code i didnot understand how it exactly works:

public int hashcode()
{
return name.hashcode();
}
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Chandra thanks
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic