


Detailed explanation of the difference between == and equals in Java
This article brings you a detailed explanation of the difference between == and equals in Java. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The difference between == and equals in Java, I feel that only a few people can say it completely correctly.
The common wrong answer is: == Basic types compare whether the values are the same, reference types compare whether the references are the same; and equals compares whether the values are the same.
As for why it is wrong, you will know after reading this article’s interpretation of == and equals.
1. == Interpretation
The effects of == are different for basic types and reference types, as follows:
Basic types : The comparison is whether the values are the same; reference type: the comparison is whether the references are the same;
Code example:
String x = "string"; String y = "string"; String z = new String("string"); System.out.println(x==y); // true System.out.println(x==z); // false System.out.println(x.equals(y)); // true System.out.println(x.equals(z)); // true
Code interpretation: Because x and y point to the same reference, so = = is also true, and the new String() method is rewritten to open up memory space, so the result of == is false, and equals always compares values, so the results are all true.
2. Interpretation of equals
equals is essentially ==, except that String and Integer override the equals method and turn it into a value comparison. Just look at the code below to understand.
First let’s look at how equals compares objects with the same value by default. The code is as follows:
class Cat { public Cat(String name) { this.name = name; } private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } Cat c1 = new Cat("王磊"); Cat c2 = new Cat("王磊"); System.out.println(c1.equals(c2)); // false
The output result is beyond our expectation, it is false? What is going on? You will know after reading the source code of equals. The source code is as follows:
public boolean equals(Object obj) { return (this == obj); }
It turns out that equals is essentially ==.
Then the question comes, why do two String objects with the same value return true? The code is as follows:
String s1 = new String("老王"); String s2 = new String("老王"); System.out.println(s1.equals(s2)); // true
Similarly, when we enter the equals method of String, we find the answer. The code is as follows:
public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; }
It turns out that String overrides the equals method of Object and changes the reference comparison. It becomes a value comparison.
3. Summary
Generally speaking, == is a value comparison for basic types, and for reference types, it is a reference; and equals defaults In this case, it is a reference comparison, but many classes have overridden the equals method, such as String, Integer, etc., turning it into a value comparison, so under normal circumstances, equals compares whether the values are equal.
The above is the detailed content of Detailed explanation of the difference between == and equals 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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
