Equals and == detailed explanation (basic data types, reference data types, String)

1. Essential knowledge:

img

The equals method was originally defined in the base class Object of all classes. The source code is

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

1.1 Special String:

String s="abce" is a very special form, which is essentially different from new. It is the only way in java to generate objects without new. Assignment in the form of String s = "abce"; is called direct in java, it is in the constant pool rather than in the heap like new. This form of string, string detention occurs inside the JVM, that is, when such a string is declared, the JVM will first look for an object with a value of "abcd" in the constant pool. If there is, it will Assign it to the current reference. That is, the original reference and the current reference point to the same object. If not, create a new "abcd" in the constant pool. Next time if there is String s1 = "abcd"; s1 points to the object "abcd", that is, the string declared in this form. As long as the values ​​are equal, any multiple references point to the same object.

And String s = new String("abcd"); is the same as any other object. Each call produces an object, as long as they are called.

It can also be understood like this: String str = "hello"; First look for the "hello" object in the memory, if so, let str point to the "hello". If there is no "hello" in the memory, create a new one The object saves "hello". String str=new String ("hello") means that no matter whether there is already an object "hello" in the memory or not, create a new object and save "hello".

img

1. Two objects will inevitably create "ab" objects in the heap space, and also put "ab" in the constant pool

An object is: the new keyword is created in the heap space

Another object is: the object "ab" in the string constant pool. Bytecode instruction: ldc

2、new String(“a”) + new String(“b”)呢?

Object 1: new StringBuilder()

Object 2: new String("a")

Object 3: "a" in the constant pool

Object 4: new String("b")

Object 5: "b" in the constant pool

1. Basic data types

Storage location : All basic data types are stored in the stack, which is fast.

equal and == : The data size is determined, and the memory space size can be allocated. They are stored directly by value, so they can be accessed directly by value. The comparison between them directly choose ==, and the direct comparison here is their value, and equal is the same.

2. Reference data types

  1. == : Compare whether two objects are the same object in the memory, that is, the storage locations in the memory are the same. The values ​​stored in the two String objects are the same, but they may be stored in different places in memory.
  2. == : The comparison is the reference, and the equals method is the content.

The public boolean equals(Object obj) method is provided by the Object object and can be overridden by subclasses. The default implementation returns true only when the object is compared with itself, this time it is equivalent to ==. String, BitSet, Date, and File all rewrite the equals method. For two String objects, equal values ​​mean that they contain the same sequence of characters. For packaging classes of basic types, equal values ​​mean that the values ​​of the corresponding basic types are the same.

Three, string buffer pool

String s1 = "hello"; 
String s2 = "hello"; 
System.out.println(s1 == s2);//true 
System.out.println(s1.equals(s2));//true

It turns out that when the program is running, a string buffer pool (string constant pool) is created. When using the expression s2 = "hello" to create a string, the program will first look for the string constant pool, and s1 will be used first. Put it in the pool, so when s2 is created, the program finds s1 with the same value and s2 refers to the object "hello" referenced by s1;

String s1 = "hello"; 
String s2 = new String("hello");
System.out.println(s1==s2);//false
System.out.println(s1.equals(s2));//true

However, in the second program, the new operator is used, which will inevitably create a "hello" object in the heap space, and also put "hello" in the string constant pool (because it already exists, it is not put). Their values ​​are the same, but their locations are different, so their address values ​​have changed!

Brief summary:

==:

1. The comparison is whether the operands at both ends of the operator are the same object.

2. The operands on both sides must be of the same type (it can be between parent and child classes) in order to compile and pass.

3. The comparison is the address, if it is the comparison of specific Arabic numerals, the value is equal to true, such as:

int a=10 is the same as long b=10 and double c=10.0 (true), because they all point to the heap at address 10.

equals:

Equals is used to compare whether the contents of two objects are equal. Since all classes are inherited from the java.lang.Object class, they are applicable to all objects. If the method is not overridden, the call is still The method in the Object class, but the equals method in the Object returns the judgment of ==.

The difference between equals and ==

The equals method was originally defined in the base class Object of all classes. The source code is

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

It can be seen from the source code of equals that the equals defined here ==are equivalent (the equals in the Object class is the same). The reason for the difference is that some classes (such as String, Integer, etc.) rewrite equals, but there is no Classes that rewrite equals (such as our own classes) can only inherit the equals method from the Object class, and the equals method ==is equivalent to the same, unless we override equals in this class.

There are five points to note about equals again:

1 Reflexivity: For any reference value X, the return value of x.equals(x) must be true;

2 Symmetry: For any reference value x, y, if and only if the return value of y.equals(x) is true, the return value of x.equals(y) must be true;

3 Transitivity: if x.equals(y)=true, y.equals(z)=true, then x.equals(z)=true;

4 Consistency: If there is no change in the comparison object, the result of the comparison should not change;

5 Non-nullability: For any non-null reference value X, the return value of x.equals(null) must be false.

In addition, " ==" runs faster than "equals" because " ==" is only for comparison.

Guess you like

Origin blog.csdn.net/qq_34159161/article/details/108754603