Difference between equals and == in Java?

Confusion about equals and ==

The value type is a stack stored in memory (hereafter referred to as the stack), and the variable of the reference type only stores the address of the variable of the reference type in the stack, and itself is stored in the heap.

== in Java

1. The == operation compares whether the values ​​of two variables are equal. For reference variables, it means whether the addresses stored in the heap of the two variables are the same, that is, whether the contents of the stack are the same.

2.== compares the addresses of two objects, while equals compares the contents of the two objects.

equals in Java

1. Whether the two variables represented by the equals operation are references to the same object, that is, whether the contents in the heap are the same.

Summarize

== is not necessarily true when equals is true

equals and == in String type

Let's start with a piece of code:

String s1 = "ARCHer";
String s2 = new String("ARCHer");
String s3 = "ARCHer";
String s4 = new String("ARCHer");
String s5 = "Hello ARCHer";

if(s1 == s2) System.out.println("s1 == s2");
else System.out.println("s1 != s2");
if(s1.equals((s2))) System.out.println("s1 equals s2");
else System.out.println("s1 not equals s2");

if(s1 == s3) System.out.println("s1 == s3");
else System.out.println("s1 != s3");
if(s1.equals((s3))) System.out.println("s1 equals s3");
else System.out.println("s1 not equals s3");

if(s2 == s4) System.out.println("s2 == s4");
else System.out.println("s2 != s4");
if(s2.equals((s4))) System.out.println("s2 equals s4");
else System.out.println("s2 not equals s4");

if(s1 == s5) System.out.println("s1 == s5");
else System.out.println("s1 != s5");
if(s1.equals((s5))) System.out.println("s1 equals s5");
else System.out.println("s1 not equals s5");

Show the result:

s1! = s2
s1 equals s2
s1 == s3
s1 equals s3
s2 != s4
s2 equals s4
s1 != s5
s1 not equals s5

When the program runs, it creates a string buffer pool to use. That is to say, after I define s1 = "ARCHer", this string is written to the buffer pool. When I define s3, it will look in the buffer pool and find the same string, so s1 == s3 .

When defining s2, I used new, which is to open up a new space to hold this string, so when == s1 != s3. And equals is smarter, it doesn't care about 3721, I just compare whether the two values ​​are the same, and find that ARCHer and ARCHer are the same, therefore, s1 equals s3.

It can be understood like this: == compares the addresses of two variables, and equals compares the values ​​of two variables. (It is mentioned in the C language that a function is an evaluation operation, that is, the incoming parameter expression is evaluated first and then the operation is performed. I think it should be the same as here. And == is not a function, it should be called an operation , compares the addresses of two variables.)

If you are not satisfied, make a small addition. On the basis of the above, add a piece of code:

String s6 = new String("ARCHer").intern();
if(s1 == s6) System.out.println("s1 == s6");
else System.out.println("s1 != s6");
if(s1.equals((s6))) System.out.println("s1 equals s6");
else System.out.println("s1 not equals s6");

Guess the result? That's right:

s1 == s6
s1 equals s6
The return value of .intern() is still the original string, but there is a small action behind it, I believe everyone can see it - check whether there is a string such as "ARCHer" in the string pool, if it exists, return The string in the pool; if it doesn't exist, the method will add "ARCHer" to the string pool before returning a reference to it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324718301&siteId=291194637