Home > Java > JavaBase > body text

The difference between equals and '==' in java

王林
Release: 2019-11-19 17:03:35
Original
2055 people have browsed it

The difference between equals and '==' in java

Difference:

== What is compared is the (heap) memory address of the object stored in the variable (stack) memory, used to determine Whether the addresses of the two objects are the same, that is, whether they refer to the same object. What is compared is the real pointer operation.

equals is used to compare whether the contents of two objects are equal. Since all classes inherit from the java.lang.Object class, it is applicable to all objects, if this method is not overridden. , the method still being called is the method in the Object class, but the equals method in Object returns the judgment of ==.

Data types in java can be divided into two categories:

Basic data types

byte, short, char, int, long, float, double, boolean

Comparisons between basic data types require the double equal sign (==), because they compare values.

Reference data type

Interface, class, array and other non-basic data types

String in Java belongs to Reference data type because String is a class.

When they use (==) to compare, they compare their storage addresses in memory. Therefore, unless they are the same new object, the result of their comparison is true, otherwise The result of the comparison is false. Because if there is no new, a new heap memory space will be re-opened

Entity entity = new Entity();
Entity entity1 = new Entity();
Entity entity2 = entity;
        
System.out.println(entity==entity2);
System.out.println(entity.equals(entity2));

System.out.println(entity.equals(entity1));
System.out.println(entity==entity1);
Copy after login

Result:

true
true
false
false
Copy after login

Summary:

For composite data Compare equals between types. In the absence of overriding the equals method, the comparison between them is still the address value of the storage location in the memory, which is the same as the result of the double equal sign (==); if it is overwritten, the result will be the same as the overriding. The request comes.

Function of ==:

Basic type: What is compared is whether the values ​​are the same;

Reference type: What is compared is whether the address values ​​are the same.

The role of equals:

Reference type: By default, the address value is compared. After overriding this method, compare whether the member variable values ​​of the object are the same.

Recommended tutorial: java introductory tutorial

The above is the detailed content of The difference between equals and '==' in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template