Reputation: 391
I've got question about java's equals(Object o) and hashCode() methods. What are the technical constraints of implementation this both methods? Is there something that I can't do during implement this methods?
Upvotes: 0
Views: 483
Reputation: 831
Is there something that I can't do during implement this methods?
Well, as a rule of thumb (and as already @RHT mentioned above and @Antti Sykäri explains here) do your future self a favor and always use EqualsBuilder and HashCodeBuilder from the Apache Commons Lang library. Because, quite frankly, at least in my case I never get to remember all the nitty-gritty details that a correct implementation would require. ;-)
Upvotes: 0
Reputation: 31182
All you need to remember, is:
Make sure you understand it every time you override any of those methods.
Upvotes: 0
Reputation: 82579
You can technically anything inside them you can do in any other methods.
Instead what you concern yourself with are the practical and contractual obligations of the methods.
Good rules of thumb:
Upvotes: 1
Reputation: 1
a given object must consistently report the same hash value two objects which equals() says are equal must report the same hash value - so no timestamps in the hashcode :).
Two unequal objects can also have the same hashcode, though it is better to make the hashcode difficult to repoduce.
Upvotes: 0
Reputation: 2421
None. It's just two methods in Object class. You could even change an object's state within this methods and this will freak out every developer and system but it's still valid from technical point of view.
Upvotes: 1