This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

hashCode() Question?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. This question if from a test engine that I downloaded a while ago and does not give the answeres to the questions. Unfortunatly I don't remember where exactly I got it. Anyways, here is the question that is killing me...
Given the following class, which are correct implementations of the hashCode() method?
class ValuePair {
public int a, b;
public boolean equals(Object other) {
try {
ValuePair o = (ValuePair) other;
return (a == o.a && b == o.b)
|| (a == o.b && b == o.a);
} catch (ClassCastException cce) {
return false;
}
}
public int hashCode() {
// Provide implementation here.
}
}
Select the three correct answers:
(1) return 0;
(2) return a;
(3) return a + b;
(4) return a - b;
(5) return a ^ b;
(6) return (a 16) | b;

2, 3, and 5 made the most sense to me, but that isn't the correct.
Thanks for any help!
- Brian
 
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

  • If you analyze the equals method, it returns true if both objects have the same two values, regardless of whether they are assigned to a or b. Once you figure that out, you'll realize that you need a commutative operation (same result regardless of order of operands) in order to adhere to the second clause of the hashCode contract. Of the choices given, #3 & #5 are commutative. #1 is not an ideal hashcode but still satisfies the contract.
    [ January 24, 2003: Message edited by: Junilu Lacar ]
     
    Ranch Hand
    Posts: 203
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Junilu's explanation is excellent. (2) can not be one of the answers. Let us say we have two objects X, Y. X has (10,20) as it's a and b and Y has (20,10). Now both the objects are equal.
    Answer can not be (2) as the hash codes are not equal.
    The
     
    reply
      Bookmark Topic Watch Topic
    • New Topic