程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Similarities and differences between determining whether objects are equal in Java and Python

編輯:Python
The similarities and differences of judging whether objects are equal in Java and PythonJavaFor basic types, equals and == only judge whether the values ​​are the same;Integer type: byte, short, int, long;Float: float, doubleBoolean: booleanCharacter type: charThere are two cases for object types:a. String type (Because the String class overrides the equals method; at this time, equals compares whether the values ​​are equal, == indicates whether the addresses are equal ):b. Other types (provided that the equals method is not overridden, at this time: == and the equals method both compare whether the addresses of the two are the same)PythonPython determines whether objects are equal (== and is)Objects in Python contain three basic elements, namely:id: used to uniquely identify an object, which can be understood as a memory address;type: identifies the type of the object;value: the value of the object;== : Compare whether the contents of two objects are equal, that is, whether the values ​​of the two objects are equal, regardless of whether idEqual, the default will call the object's __eq__() methodis: The comparison is whether the two objects are exactly the same, that is, their ids are equal.id() can get the memory address of the objectThat is, if a is b is True, then a == b is also TrueIn other words, in Python is is the comparison id(memory address) equal, == is the comparison value is equal( is to determine whether two objects point to the same memory area).

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved