[Java Interview Series No.1] The difference between equals and ==

Beep

Recently, I want to use interview questions to consolidate my basic knowledge. In the summary of many interview questions, the frequency of "the difference between equals and ==" is not generally high. Although in my own programming, I sometimes use it differently Both, but since it is a deep study, let's take a look at the difference between the two. (See the conclusion and go directly to the last section [Summary])

look at the essence

First of all, the functions of both are for comparison, but in essence and use, there are still big differences between the two:

  • In essence: ==it is an operator of Java, equals()but a method for comparison of the Object class, which is fundamentally different.

  • In use: Although in daily development, ==and equals()both often appear in the case of comparing whether the two are equal, the applicable scenarios of the two are quite different.

First look at "=="

First of all, when "==" is used for comparison, there are several different situations:

  • Both sides are reference types (encapsulation types)
  • Basic type on both sides
  • One side is a reference type and the other side is a primitive type

Basic type on both sides

When ==both sides are basic types, ==the function is to compare whether the values ​​of the two are equal, such as the following example:

int a = 1222;
int b = 1222;
System.out.println(a == b);

输出:

true

For the two variables a and b of java basic type int, the value is both 1222, so the output is true. The same applies to all other primitive types in Java.

Java fundamental types include:

  • Number type: integer byte, short, int, long, floating point number: double, float
  • Character type: char
  • Boolean type: boolean

Both sides are reference (encapsulation) types

When ==both sides are reference (encapsulation) types, the comparison is whether the references are the same, that is, in addition to comparing whether the values ​​are the same, it is also necessary to compare whether the memory addresses where the values ​​are located are the same.

For most reference types, as long as the keyword is used newto create a new object, the reference address of the object will also be a new address, and the result obtained by using it is ==always false.

And when the first object uses new and the first object is directly copied to the second object, the first object and the second object point to the same object, and the use of waiting is always true ==.

In addition to the above two general situations, it is necessary to consider some different situations when different types are defined. Here we focus on several commonly used encapsulation types:

  1. String type

For String, you need to introduce the concept of a string constant pool. You can search and understand the specific content by yourself. I will make up related articles in the future. First set the flag, and then come back and post the address . Here we need to understand some things:

In order to improve performance and reduce memory overhead, JVM performs a series of optimization operations when instantiating string constants:

  1. Provides a string constant pool for strings at the JVM level, which can be understood as a cache area;
  2. When creating a string constant, the JVM will check whether the string exists in the string constant pool;
  3. If the string exists in the string constant pool, the reference instance is returned directly; if it does not exist, the string is instantiated first, and the string is put into the string constant pool so that it can be used directly next time Access to achieve the effect of fast use of the cache.

For example the following code:

String str1 = "Hello";
String str2 = "Hello";
System.out.println(str1 == str2);

输出:

true

This situation applies to the above-mentioned string constant pool statement. First, when defining str1, it will first check whether there is a string "Hello" in the constant pool. If not, create a new object "Hello" and save it in memory. , and then when defining str2, it is detected that the string "Hello" already exists in the constant pool, and str2 will also point to this string, and it will appear at this time, the values ​​and references of str1 and str2 are consistent, so using == will compare The case of true appears.

  1. The corresponding package type of the basic type

The corresponding encapsulation type table of the basic type is as follows:

basic type Corresponding package type
char Character
boolean Boolean
float Float
byte Byte
double Double
int Integer
short Short
long Long

For the encapsulation types of these basic types, in the Java language specification, I found ==this sentence about:

insert image description here

The general meaning is:

  • When ==both sides are of Integer type, if the values ​​are equal within the range of -128~127, it returns true;
  • When ==both sides are of Character type, if the values ​​are equal within the range of \u0000~\u007f, it returns true;
  • When ==both sides are of Boolean type, return true if the values ​​are equal.

The values ​​in the above range are directly cached in memory. In the case of direct assignment, they all point to the cached object. Therefore, when the values ​​in the above range are equal, it will return true ==.

Integer:

Integer i1 = 122;
Integer i2 = 122;
System.out.println(i1 == i2);

Integer i3 = 1222;
Integer i4 = 1222;
System.out.println(i3 == i4);

输出:

true
false

Character:

Character c1 = '\u0002';
Character c2 = '\u0002';
System.out.println(c1 == c2);

Character c3 = '\u00ff';
Character c4 = '\u00ff';
System.out.println(c3 == c4);

输出:

true
false

Boolean:

Boolean b1 = true;
Boolean b2 = true;

System.out.println(b1 == b2);

输出:

true

Look at equals() again

equals()Usually used for comparison of reference types, a relatively easy-to-understand statement: equals()it is a general method of the Object class in Java, written as follows:

public boolean equals(Object obj) {
    
    
    return (this == obj);
}

It can be seen that, by default, the function of equals()and ==is consistent, because equals()the operation in is still used ==. For the use of two reference types equals(), the reference is still compared by default. At this time, only ==part of the explanation is needed. Consider it.

However , most of the reference types will be equals()rewritten, such as some Java built-in reference types such as Integer and String introduced in the previous section, or some reference types defined by ourselves, usually will equals()rewrite for comparing values. For example, the following is equals()the definition of the String type:

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;
}

The corresponding reference types of other similar basic types equals()also have their own definitions, so I won’t go into details here, just check it yourself.

in conclusion

For ==:

  • Basic type: the comparison is whether the values ​​are the same;
  • Reference type: the comparison is whether the references are the same;

For equals(): It
is essentially ==the same as the effect, but in most cases it will be rewritten as a value comparison, so equals()it is usually used for value comparison.

Guess you like

Origin blog.csdn.net/u012751272/article/details/127220692