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 is the relationship between = = and equals in python

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Editor to share with you what is the relationship between python = = and equals, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Its function is to determine whether the addresses of two objects are equal. That is, to determine whether two objects are the same object (the basic data type = = compares the value, and the reference data type = = compares the memory address).

Equals (): its function is also to determine whether two objects are equal. But it is generally used in two ways:

Case 1: the class does not override the equals () method. Then comparing two objects of this class through equals () is equivalent to comparing the two objects by "= =".

Case 2: the class overrides the equals () method. In general, we override the equals () method to compare whether the contents of two objects are equal; if their contents are equal, we return true (that is, the two objects are considered equal).

For example:

Public class test1 {public static void main (String [] args) {String a = new String ("ab"); / an is a reference String b = new String ("ab"); / / b is another reference, and the content of the object is the same as String aa = "ab"; / / put String bb = "ab" in the constant pool / / find if (aa==bb) / / true System.out.println ("aa==bb") from the constant pool; if (astatb) / / false, which is not the same object System.out.println ("astatb"); if (a.equals (b)) / / true System.out.println ("aEQb") If (42 = = 42) {/ / true System.out.println ("true");}

Description:

The equals method in String is overridden because the equals method of object is the memory address of the object being compared, while the equals method of String compares the value of the object.

When creating an object of type String, the virtual machine looks in the constant pool for an object that already exists with the same value as the one you want to create, and if so, assigns it to the current reference. If not, recreate a String object in the constant pool.

The above is all the content of the article "what is the relationship between python 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: 202

*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

Internet Technology

Wechat

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

12
Report