Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between hashCode () and equals ()

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you what are the differences between hashCode () and equals (), I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

If the two objects hashCode () are the same, does equals () have to be true?

First of all, the answer is definitely not necessarily. At the same time, the reverse equals is not necessarily the same as true,hashCode.

Both the hashCode and equals methods of the class can be overridden, and the values returned are entirely self-defined.

HashCode () returns the hash code value of the object; equals () returns whether the two objects are equal.

There are some general agreements about the hashCode and equal methods:

1. Comparing two objects with equals () returns true, then the hashCode () method of the two objects must return the same result.

2. Comparing two objects with equals () returns false, which does not require the hashCode () method to return different values, but it is best to return different values to improve the performance of the hash table.

3. Override the equals () method. You must override the hashCode () method to ensure that the two objects hashcode return the same value when the equals method is equal.

The above is all the contents of the article "what are the differences between hashCode () and equals ()". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report