Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mock question: hashcode

 
adam Lui
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following code:
1. class Car {
2. String make;
3. String model;
4. //Insert code here.
5. public boolean equals(Object o) {
6. Car c = (Car) o;
7. return model.equals(c.model);
8. }
9. }
Which of the following inserted independently at line 4 will honor the hashCode contract?

A. public int hashCode () {return (int) (Math.random()*101);}
B. public int hashCode () {return 420;}
C. public int hashCode () {return make.hashCode();}
D. public int hashCode () {return model.hashCode();}
E. None of the above

















----------------
answers: B and D


I dont understand at all, why B and D??
if we return 420 for all objects, how does it practically honor the hashcode contract?

and model.hashCode(), is this even valid and wats the logic here?

pls help!
 
Mehmet Gursul
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If two objects are equal then their hashCode() should also be equal.

So, although returning constant value (420 or any other value), is not a good thing, but it is legal. B and D are correct answers.


Mehmet Gursul
SCJP 5.0 (81%)
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by adam lui:
and model.hashCode(), is this even valid and wats the logic here?

Note that hashCode() is a method in class Object. So you can call hashCode() on any object. 'model' is a String, so calling hashCode() on it is legal.

The hashCode() contract says: If two objects are equal (which means: equals() returns true when comparing the two objects), then the hash codes of the two objects should also be equal.

Note that this does not mean that the hash codes must be different if the two objects are not equal!
 
adam Lui
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper Young:

Note that hashCode() is a method in class Object. So you can call hashCode() on any object. 'model' is a String, so calling hashCode() on it is legal.

The hashCode() contract says: If two objects are equal (which means: equals() returns true when comparing the two objects), then the hash codes of the two objects should also be equal.

Note that this does not mean that the hash codes must be different if the two objects are not equal!


so for answer C, var "make" is also String, why is it incorrect?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by adam lui:
so for answer C, var "make" is also String, why is it incorrect?

It's not only about if it compiles correctly or not.

Look closely again at the equals() method in class Car. It compares two Car objects only by looking at 'model'. So two Car objects are considered equal if their 'model' member variables are equal.

Now remember the hashCode() contract: if two objects are equal, their hash codes must be equal. With answer C, that is not always the case, so answer C is wrong. You can have two Car objects with the same value in 'model' but different values in 'make'. The hashCode() contract would be violated in case of answer C.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic