String in Java

Source: Internet
Author: User

Resources:

Http://www.cnblogs.com/chenssy/p/3695271.html

http://blog.csdn.net/yyaf2013/article/details/12168491


First take a look at the output of the following code:

public void Strtest () {String a = "Huhui"; String B = "Huhui"; String c = new String ("Huhui"); System.out.println ("a = = B?") + (A = = b)); System.out.println ("a = = c?") + (A = = c)); System.out.println ("a.equals (b)?") +a.equals (b)); System.out.println ("A.equals (c)?") +a.equals (c));}
Output

A = = b? Truea = = c? Falsea.equals (b)? Truea.equals (c)? True


One might ask, why is the result of a==b true? Let 's take a look at the difference between "= =" and equals:

The = = operator is specifically used to compare the values of two variables, that is, whether the value stored in the memory used to compare the variables is the same, to compare two basic types of data or two reference variables equal, only with the = = operator.
If the data that a variable points to is an object type, then this time involves two blocks of memory, the object itself occupies a chunk of memory (heap memory), and the variable occupies a chunk of memory, such as objet obj = new Object (); the variable obj is a memory, and new object () is another memory, At this point, the value stored in the memory of the variable obj corresponds to the first address of the memory that the object occupies. For variables that point to the object type, if you want to compare whether the two variables point to the same object, that is, to see if the values in memory for the two variables are equal, then you need to compare them with the = = operator.

The equals method is used to compare whether the contents of two independent objects are the same, and the two objects it compares are independent. For example, for the following code:

String A=new string ("123"); String B=new string ("123");
Two new statements create two objects, and then use a, B, to point to one of the two variables, which is two different objects, their first address is different, that is, the values stored in a and B are not the same, so the expression a==b will return false, and the contents of the two objects are the same. Therefore, the expression a.equals (b) returns True. Remember that the comparison of strings is basically using the Equals method.

If a class does not override the Equals method, it inherits the Equals method of the object class, and the implementation code for the Equals method of the object class is as follows:

Boolean equals (Object o) {return this==o;}
This means that if a class does not define its own Equals method, its default Equals method (inherited from the object class) is using the = = operator, and whether the object pointed to by the two variables is the same object, using equals and using = = will get the same result. If the comparison is two independent objects, the total return is false. If the class you are writing wants to be able to compare the contents of the two instance objects created by the class, then you must override the Equals method,


Now to analyze the execution results of the Strtest () method:

The allocation of strings, like other object assignments, takes time and space, and we use a lot of strings. In order to improve performance and reduce the overhead of memory, the JVM has some optimizations when instantiating strings: using a string constant pool. Whenever we create a string constant, the JVM first checks the string constant pool, and if the string already exists in the constant pool, it returns the instance reference in the constant pool directly. If the string does not exist in a constant pool, the string is instantiated and placed in a constant pool. Because of the immutability of string strings we can be quite certain that there must be no two identical strings in the constant pool (this is critical to understanding).

String a = "Huhui";
String B = "Huhui";

In the above two statements, both A and B are "Huhui" objects that point to the JVM string constant pool, and they point to the same object, so the expression a==b returns True.

String c = new String ("Huhui");

In the above statement, the New keyword produces an object "Huhui" (note that this huhui differs from the above Huhui), because it is the object created by new, so this object is stored in the heap. So the above statement produces two objects: Save the C in the stack and the "Huhui" stored in the heap. However, there are no two identical string objects in Java. Therefore, the "Huhui" in the heap refers to the "Huhui" in the string constant pool, so the relationship of C, Huhui, and pool Huhui should be:c-->huhui--> pool Huhui. The whole relationship is as follows:


Image source: http://www.cnblogs.com/chenssy/p/3695271.html

We can clearly see the relationship between A, B, C and "Huhui" through the above figure. String c = new String ("Huhui"); Although the content of C is created in the heap, his internal value is also the value of Huhui, which points to the JVM constant pool, which is still Huhui string constant when it constructs Huhui.



String in Java

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.