• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

hashcode() and equals()

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
I stumbled on this question-

Given that the objects denoted by the parameters override the equals() and the hashCode() methods appropriately, which return values are possible from the following method?

String func(Object x, Object y) {
return (x == y) + " " + x.equals(y) + " " + (x.hashCode() == y.hashCode());
}

Select the two correct answers.

a "false false true"

b "false true false"

c "false true true"

d "true false false"

e "true false true"
The answer is a and c .How can it be?
In option a if x.equals(y) is false then how can be (x.hashCode() == y.hashCode() true?
regs Rajesh
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"It is generally necessary to override the hashCode method whenever equals method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes "

If the equals () method and hashcode methods are overridden according to the ideal definition, the correct answers will be

1.true true true
2.false false false
3.false true true--(c)

But a." false false true " is possible though it is not correct.But other options is impossible.

[ December 12, 2005: Message edited by: Prathiba Kalirengan ]
[ December 12, 2005: Message edited by: Prathiba Kalirengan ]
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
" false false true " is possible and Correct !

Part of the contract of hashcode() :

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.


It is sometimes impossible to produce different hashcodes for two different objects (regarding equals method).
Look at the Long object : Long L1 and Long L2 are equals if L1.longValue() == L2.longValue();
But there are 2^64 different possible values for a long, and hashcode() should produce an int...
 
Prathiba Kalirengan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case of "false false true" ,the implementation of hashCode method is inappropriate.In the question they mention that the equals and hashcode methods are overridden appropriately.
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I disagree...


will print false, false, true, and I dont't think that Sun's implementation of hashcode() for the Long class is inappropriate...
 
Prathiba Kalirengan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with u.I think it holds true for strings too
 
Rajesh Chandra
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Sebe but as Prathiba pointed out it may work with strings too.Well I tried and it doesnt.In what all cases can we expect such situations? How to be sure to be precise in scjp exam??
regs Rajesh
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Seb pointed out, there is a pigeon hole principle at work here: since
hasCode returns an int, there are 2^32 possible hash codes and many classes
have more than that many possible distinct (non-equal) values.
What I walk away with is that equal hasCode values don't imply equal (or ==)
objects, but a well-written hasCode should make this likely. For
example, an implementation of hashCode for String that only used the first
few letters wouldn't be a very good one!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rajesh Chandra:
Thank you Sebe but as Prathiba pointed out it may work with strings too.Well I tried and it doesnt.In what all cases can we expect such situations? How to be sure to be precise in scjp exam??
regs Rajesh



You tried all possible pairs of unequal Strings and didn't find any pairs that had the same hashCode? Wow, that must have taken a very long time!

Seriously, such pairs do exist. Finding them may be tricky, but that doesn't mean they're not there. Such pairs exist whenever there can be more than 2^32 distinct objects of a class, so they're not rare at all. They're the rule rather than the exception.

How to be precise for the SCJP? Well, here at JavaRanch we believe the best thing to do is to actually understand what's going on. If you truly understand the discussion here, then the SCJP presents no problems. If you're just looking for a quick study, well, then just memorize "a and c", and hope for the best!
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How did I find "bC"? I read the API for String.hasCode:

Returns a hash code for this string. The hash code for a String object is computed as

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

using int arithmetic, where s[i] is the ith character of the string, n is
the length of the string, and ^ indicates exponentiation. (The hash value
of the empty string is zero.)
 
Sheriff
Posts: 17734
302
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

Originally posted by Rajesh Chandra:
In what all cases can we expect such situations? How to be sure to be precise in scjp exam?



Understand the full contract for hashCode:


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.



  • Look at the third bullet point.
    [ December 12, 2005: Message edited by: Junilu Lacar ]
     
    reply
      Bookmark Topic Watch Topic
    • New Topic