• 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

Question on hashCode implementation

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have started preparing for the SCJP2. I have a simple question on the hashcode implementation. I dont have the answer to this question. There are 3 correct answers.

public class Test {
public int a, b;
public boolean equals (Object other) {


try {
Test o = (Test) other;
return (a==o.a && b==o.b) || (a==o.b && b==o.a);
} catch (ClassCastException cce){
return false;
}
}

public int hashCode(){
// insert code here
}
}

Choose 3 options

1- return 0;

2- return a;

3- return a+b;

4- return a-b;

5- return a^b;

6- return (a 16)|b;


My answers are 1,3 and 5. I am not sure about the option 6( I am guessing its a typo)

Can someone help me out? Thanks
[ January 25, 2006: Message edited by: Abid Sami ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with 1 and 3, but not 5. Take another look at that, and see if you can see why.

(I don't know what 6 is trying to say either.)
 
Abid Sami
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marc. There are 3 correct options. Why not 5? I am not sure what the third one could be
[ January 25, 2006: Message edited by: Abid Sami ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Abid Sami:
... Why not 5? ...


Per the hashCode contract defined in Object, "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."

In this case, instances are equal if they are Test objects were either...
  • Values of a and b are respectively equal among instances (a==o.a && b==o.b), OR...
  • Values of a and b are "switched" among instances (a==o.b && b==o.a).

  • So for example, we might have a=1, b=2 in one instance; and a=2, b=1 in another instance. These are equal per the equals method. So a hashCode of a^b would not be acceptable, because 1^2 is not the same as 2^1.
     
    Ranch Hand
    Posts: 39
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I think Abid Sami answers 1, 3 and 5 are correct,

    XOR - ^
    ---------

    System.out.println(1^2);
    System.out.println(2^1);

    both are same, 3 is the value

    Truth Table for XOR says

    X Y ^
    -----------
    0 1 1
    1 0 1
    0 0 1
    1 1 1


    for more information on hashcode implementation, my suggestion is to refer Effective java book by Joshua Bloch
     
    ven kaar
    Ranch Hand
    Posts: 39
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    corrected truth table

    X Y ^
    -----------
    0 1 1
    1 0 1
    0 0 0
    1 1 0
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by ven_kaar:
    I think Abid Sami answers 1, 3 and 5 are correct,

    XOR - ^...


    Of course, you're correct! I misread the ^ as "to the power of." (Doh!)
     
    Abid Sami
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by marc weber:

    Of course, you're correct! I misread the ^ as "to the power of." (Doh!)




    Thanks Ven and Marc for the answers. I always misread ^ as "to the power of." as well.
    [ January 26, 2006: Message edited by: Abid Sami ]
    reply
      Bookmark Topic Watch Topic
    • New Topic