String in Java infers equality between equals and = = and equals of StringBuilder

Source: Internet
Author: User

The string type in Java has a method of equals that can be used to infer whether two strings are equal, but such equality is different from the "equality" inferred by the operator = =, followed by the analysis, and the result is validated by the program

The Equals function of string can return true only if the two string "looks" equal, meaning that when the contents of two string objects are the same, the memory address that does not need to be stored is the same, but = = Inference then returns true only if the memory address used by the inferred two variables is the same. For example, there are two identical twins, A, B, if you use a==b to infer that it will return false. The use of A.equals (B) returns True.

We can see that the source code for the Equals function in object is

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

We know that all objects in Java are inherited from the object class by default. So when we do not rewrite the Equals method, if equals is used to infer whether the two objects are equal. Only when these two objects point to the same memory address. To return true, otherwise even if the content is exactly the same but in memory it is two different memory addresses are also returned false, at this time if the twins A, a and a control. Both A==b and A.equals (B) return false.

In that case, why would the equals and = = of the string be different, here we should look at the source code that overrides equals in the string:

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;    }
You can see that the Equals function in string first infers whether its memory address is the same:

if (this = = AnObject) {            return true;        }

Then infer whether the content is the same:

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

A new string object is constructed when we create a string in a way that uses a string connection--the connection is typically + or concat ("substring"). That is to open a new address in memory to store, so this time even if the content is the same. If you infer it with = =, it also returns false when we use the equals sign, if there is a string in memory. Then the variable points to this memory address two is not created again, so this time with = = will return true, we look at the routine:

public class StringTest01 {public static void main (string[] args) {//TODO auto-generated method stubstring hello= "Hello"; String Hel1=hello; String hel2= "Hel"; String hel3=hel2+ "Lo"; String hel4=hel2.concat ("Lo"); System.out.println (hello); System.out.println (HEL1); System.out.println (HEL3); System.out.println (hel4);//== equals test System.out.println (HELLO==HEL1); System.out.println (HELLO==HEL3); System.out.println (HELLO==HEL4); System.out.println (hel3==hel4);//equals function Test System.out.println (hello.equals (Hel1)); System.out.println (Hello.equals (HEL3)); System.out.println (Hello.equals (Hel4)); System.out.println (Hel3.equals (Hel4));//stringbuilder Test StringBuilder hellobuilder = new StringBuilder ("Hel"); System.out.println (Hellobuilder.equals (Hel2));}}

The output is:


The last StringBuilder test we found is that although we use equals to infer, the return is false. What is this for?

First of all. When we use StringBuilder to create objects. It will certainly open up a new, dedicated address in memory to hold the object content. However, even if the content stored in the StringBuilder is the same as the contents of other strings, using equals to infer also returns false, because StringBuilder does not override the Equals function. The Equals of StringBuilder is:

public boolean equals (Object obj) {        return (this = = obj);    }
So it returns false.



String in Java infers equality between equals and = = and equals of StringBuilder

Related Article

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.