==The difference between and equals

Keywords: Java Operation & Maintenance server

We use string comparison to demonstrate the difference between = = and equals:
First, clarify three points:

1. = = compares whether two references point to the same object.
2. The equals method of String class in Java has been rewritten. It compares whether the contents of two strings are equal.
The 3.equals method is as like as two peas in the absence of rewriting.

Look at the first code:

public class equalsTest {
    public static void main(String[] args) {
        String s1 = "guQi";
        String s2 = "guQi";
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"use==Judgment:"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s1 <span class="token operator">==</span> s2<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"use equals Judgment:"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s1<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

}

Operation results:
Use = = to judge:
true
Judge with equals:
true
public class equalsTest {
public static void main(String[] args) {
String s1 = "guQi";
String s2 = "guQi";

    System.out.println("use==Judgment:");
    System.out.println(s1 == s2);

    System.out.println("use equals Judgment:");
    System.out.println(s1.equals(s2));
}

}

Operation results:
Use = = to judge:
true
Judge with equals:
true

The execution logic of this code is as follows:

1. When executing String s1 = "guQi"; When, the JVM creates the string constant "guQi" in the constant pool, and s1 this reference points to the string constant.
2. When executing String s2 = "guQi"; When, the JVM first looks for the string constant "guQi" in the constant pool. If so, let s2 directly point to the constant without creating a new string constant. Obviously, this time it's found and there's no need to create it.


Therefore, s1 and s2 in this code point to the same string constant (the objects pointed to by the reference are the same, and = = returns true), and the string constants are also equal (equals returns true).

Different codes:

public class equalsTest {
    public static void main(String[] args) {
        String s1 = "guQi";
        String s2 = new String("guQi");
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"use==Judgment:"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s1 <span class="token operator">==</span> s2<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"use equals Judgment:"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s1<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

}

Operation results:
Use = = to judge:
false
Judge with equals:
true
public class equalsTest {
public static void main(String[] args) {
String s1 = "guQi";
String s2 = new String("guQi");

    System.out.println("use==Judgment:");
    System.out.println(s1 == s2);

    System.out.println("use equals Judgment:");
    System.out.println(s1.equals(s2));
}

}

Operation results:
Use = = to judge:
false
Judge with equals:
true

The logic of this code is as follows:

1. When executing String s1 = "guQi"; When, the JVM creates the string constant "guQi" in the constant pool, and s1 this reference points to the string constant. (same as the first code)
2. When executing String s2 = new String("guQi"); When, the JVM creates a new object in the heap. The content of the object is "guQi", and s2 this reference points to the new object.


Therefore, s1 and s2 in this code do not point to the same object (= = returns false), but the content is the string "guQi"(equals returns true).

Posted by sgalatas on Wed, 17 Nov 2021 16:19:53 -0800