Home > Java > Javagetting Started > body text

Explain in detail the difference between equals and ==

王林
Release: 2020-09-28 16:17:46
forward
2212 people have browsed it

Explain in detail the difference between equals and ==

First of all, let’s introduce the memory allocation knowledge in JVM:

(Recommended tutorial: java course)

In In JVM, memory is divided into heap memory and stack memory. The difference between them is: when we create an object (new Object), the constructor of the object will be called to open up space, store the object data in the heap memory, and at the same time generate the corresponding reference in the stack memory. When we call it in subsequent code, we use references in the stack memory. Another thing to note is that basic data types are stored in stack memory.

Initial understanding of the difference between equals and ==:

== is to determine whether two variables or instances point to the same memory space, and equals is to determine whether two variables or instances point to the same memory space. Are the values ​​of the memory spaces pointed to the same? == refers to comparing memory addresses, and equals() compares the contents of strings. == refers to whether the references are the same, and equals() refers to whether the values ​​are the same.

Use a picture to briefly express the relationship between them:

Explain in detail the difference between equals and ==

Test:

Explain in detail the difference between equals and ==

# Detailed explanation of the difference between ##equals and ==:


== compares the (heap) memory address of the object stored in the variable (stack) memory, and is 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 call will still be Object. method in the class, but the equals method in Object returns a == judgment.

String s="abcd" is a very special form, which is essentially different from new. It is the only way in java that can generate objects without new. Assignment in the form of String s="abcd"; is called a direct variable in Java. It is in the constant pool rather than in the compressed heap like new.

A string of this form will be detained inside the JVM. That is, after declaring such a string, the JVM will first search for an object with a value of "abcd" in the constant pool. , if there is, it will be assigned to the current reference. That is, the original reference and the current reference point to the same object. If not, a new "abcd" will be created in the constant pool. Next time if there is String s1 = " abcd"; will point s1 to the object "abcd", that is, a string declared in this form. As long as the values ​​are equal, any multiple references point to the same object.

String s = new String("abcd"); is the same as any other object. An object is generated every time it is called, as long as they are called. It can also be understood this way: String str = "hello"; First, check whether there is an object "hello" in the memory. If so, let str point to that "hello".

If there is no "hello" in the memory, create a new object to save "hello". String str=new String ("hello") means that regardless of whether there is already the object "hello" in the memory, Create a new object to save "hello".

Test:

Explain in detail the difference between equals and ==

Related recommendations:

Getting started with java

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

Related labels:
source:csdn.net
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