• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

True Hash Code Statements in Java  RSS feed

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the following statements are true about hashCode in java?
1. if two objects are declared equal using the equals() method then they must have the same hash codes.
2. if two objects are declared equal using the equals() method then they may or may not have the same has codes.
3. during the execution of the java application if the hashCode() method is called on the same object multiple times then the method must return the same integer value.
4. the object class has a hashCode() method that returns only positive integers.

Option 1 - 1 and 3
Option 2 - 2 and 4
Option 3 - 2, 3 and 4
Option 4 - 1, 3 and 4

Which option is true. I am confused in Option 2 and Option 3.
 
Marshal
Posts: 63454
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did the question really say that two objects are “declared” equal? Please tell us what you think the correct answer is; we don't give out complete solutions.
 
Campbell Ritchie
Marshal
Posts: 63454
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll give you a link to help you: here. It is old, but it has only changed slightly.

[edit]Insert “only”.
 
bablu singh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The link is really helpful and to your question it is just like the second bullet point in that link:
"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."

Does this mean that Option 1 is the correct answer?
 
Campbell Ritchie
Marshal
Posts: 63454
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You tell me. What you found means that No 1 is correct (same hash code), and since No 2 (hash codes can differ) is the opposite of No 1, No 2 must be incorrect. But you haven't answered Nos 3 and 4 yet.
 
bablu singh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No 3 is also correct. I am not sure about no 4 because as far as I know it should return positive integers but when I googled I found that we get a negative integer after the overflow. Negative hashcode is perfectly valid! It is perfectly legal to have negative hash codes.  Can you please share your thoughts.
 
Campbell Ritchie
Marshal
Posts: 63454
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

bablu singh wrote:No 3 is also correct. . . .

You think it is correct, and whoever wrote the question thinks it is correct, but whoever wrote the question got the question wrong. It is only necessary for a hash code to remain the same if no information changes. They expect you to mark that question as correct, though it is in fact incorrect.

I am not sure about no 4 . . . .

Nowhere does it say anything about positive or negative numbers. Try this:-Enter lines of text repeatedly until you get a priintout ≥ 0x80000000. Remember that negative numbers start 8, 9, a, b, c, d, e, or f. That should answer No 4.
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!