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);
Result:
true true false false
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.