What is the need of overriding hashCode method when overriding equals method?
-
what will happen if we override the hashCode method? I do know that hashCode will be the same for two objects, that means there is only one object in the memory ??? what is the need for XOR operation done on variables of my class during returning hashCode method? PS: If you want any further clarification regarding my question i will add more details.
-
Answer:
If you want people to do your homework for you, you could at least try to provide more detailed information. For example, the programming language you are concerned with.
praveen at Yahoo! Answers Visit the source
Other answers
If you want to override equals method in your class, you should also override hashCode method as well. public int hashCode() { final int PRIME = 31; int result = 1; result = PRIME * result + number; return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final Scrap other = (Scrap) obj; if (number != other.number) return false; return true; }
Related Q & A:
- What is the difference between a static method and class method in Python?Best solution by pythoncentral.io
- What do I need to know about Servers?Best solution by Programmers
- What are the steps for the scientific method?Best solution by Yahoo! Answers
- What other acting techniques are there besides "method?Best solution by en.wikipedia.org
- What do i need when i get a digital tv converter box?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.