Explain the differences between equals and = in java

Source: Internet
Author: User

First look at a piece of code:

String test1 = "add";String test2 = "add";String test3 = new String("add");String test4 = new String("add");System.out.println(test1 == test2);System.out.println(test1.equals(test2));System.out.println(test3 == test4);System.out.println(test3.equals(test4));System.out.println(test1 == test3);System.out.println(test1.equals(test4));


Running result:

truetruefalsetruefalsetrue


Analysis:

In combination with the preceding statement, I will briefly describe the value assignment process of the string. When the computer executes Stringtest1 = "add", it first checks whether there is an "add" address space in the memory. If yes, it directly points the variable test1 to the address space. If it does not exist, create an address space and store "add". Then, point the variable test1 to the address space. Therefore, test1 and test2 actually point to the same address space. When the computer executes Stringtest3 = new String ("add"), it directly opens an address space in the memory to store "add", and then points the variable test3 to the address space. The final memory distribution is shown in:

Conclusion:

From the running result and view of the program, we can conclude that "=" compares the storage addresses of two variables. As long as the addresses are different, they are not equal. The "equals" requirement is not so harsh. If the value is equal, it is true.

 

Further dive:

Let's take a look at the source code of the equals method of the object String:

public boolean equals(Object anObject) { if (this == anObject) {     return true; } if (anObject instanceof String) {     String anotherString = (String)anObject;     int n = count;     if (n == anotherString.count) {  char v1[] = value;  char v2[] = anotherString.value;  int i = offset;  int j = anotherString.offset;  while (n-- != 0) {    if (v1[i++] != v2[j++])      return false;  }     return true;  } } return false;}

From the source code, we can see that the String class overrides the equals method of the Object class. It also uses "=" to compare the memory address, but the String class extends the comparison, the result is different from "=.

 

Extension:

"=" Is the equal implementation of the base class Object. Different classes rewrite it differently, resulting in different comparisons. Therefore, you can create a new class, and then rewrite the equals method of the base class to compare it in your own way. Of course, the equals method is rarely rewritten. We may be more likely to use the toString method, but their principles are the same.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.