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

hashcode

 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
source: Whizlabs

What will be the most appropriate implementation of hashcode() method assuming equals implemented correctly.

Ans is


I thought that answer would be


Please explain why the given answer is correct. Thanks.
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the difinition of the equals method not given in the question??
 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NO it just says that equals method has been implemented correctly but definition is not given.
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
frankly, very much close to real exam question.

you need not include area to find the hashcode because area is itself calculated using length and breadth.

hope that clears your doubt
 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but area is declared final. That means,
if an object has length = 4 and breadth = 2
then area is calculated as 8.
Later on when this object is changed, lets say, length is changed to 3 , area will still remain 8 because it is final.

That does not mean this object with values (3,2,8) equal to object with values (3,2,6).
Please clarify where i am wrong.
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But how will length and breadth change. They are both private and there is no setter (mutator) to change their values...
 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just in case there are getters and setters then what will be the answer. I just want to know. Thanks
 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Someone please clarify
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I don't think you should seek for an answer after changing a question. Each question is designed with a strategy. If there were getter and setters for length and width, then they might have provided definition of the equals method or might have removed final from area. So don't go in too much depth for a question. I think this question is too ambiguous. The real exam is not ambiguous at all. So don't worry
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't we have risk of loss of precision here in this line??



I think the reason for not choosing this may be that there is still chance of loss of precision of the hash code being calculated as you are using long in the expression and also casting the value back to int.

HTH
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic