Java string S = "ABC" and string S = new string ("ABC ")

Source: Internet
Author: User

First, let's take a look at the semantics of variables in Java:

Java variables have two types of semantics: the original type variables are value semantics. That is to say, if you assign values to an original type variable, the data value itself is changed. A variable of the object type is a reference semantics. That is to say, assigning a value to a variable of the object type only directs it to another object, but does not change the value of the object that was originally referenced.

Next, let's take a look at the features of string and Java's special processing methods for Sting:

 

Features of string

1. The string class is final and cannot be inherited.
2. The essence of the string class is the character array char [], and its value cannot be changed.
3. A special method for creating a String object is to directly specify a String object, such as string x = "ABC" and "ABC. X is the address of the "ABC" object, which is also called the reference of the "ABC" object.
4. string objects can be connected through "+. A new string is generated after concatenation.
5. A string pool (string pool) will be maintained during Java runtime, and javadoc translation is very fuzzy "string buffer ". The string pool is used to store various types of strings generated during running, and the string content in the pool is not repeated. The general object does not exist in this buffer pool, and the created object only exists in the stack area of the method.

6. There are many ways to create strings. There are three types:
First, use the new keyword to create a string, such as string S1 = new string ("ABC ");
Second, directly specify. For example, string S2 = "ABC ";
Third, generate a new string using concatenation. For example, string S3 = "AB" + "C ";
 
Create a String object
There are also many ways to create a String object. The key is to understand its principles.

Principle 1: when using any method to create a String object S = x, Java Runtime (running JVM) the string object with the same content is located in the string pool with X. If the string object does not exist, a string S is created in the pool. Otherwise, it is not added in the pool.
 
Principle 2: in Java, as long as the new keyword is used to create an object, a new object will be created (in the heap or stack.
 
Principle 3: If you create a String object by specifying it directly or using a String concatenation, the system only checks and maintains the string in the string pool. If no string exists in the pool, it creates one in the pool! However, this string object will never be created in the stack area.
 
Principle 4: Use an expression containing variables to create a String object. This will not only check and maintain the string pool, but also create a String object in the stack area.
 

Immutable class

Java makes special processing on the string type to improve efficiency-providing the string pool for the string type
There are two ways to define a variable of the string type:

String name = "Tom"; (string name = "T" + "O" + "M" has the same effect as here)
String name = new string ("Tom ")

If you use the first method, When you declare a string whose content is "Tom", it uses the original memory in the string pool instead of allocating the memory again, that is to say, string saname = "Tom" will point to the same memory. If the second method is used, no matter whether there is "Tom" in the string pool, it will re-allocate a piece of memory in the heap to define a new object.

In addition, the string type cannot be changed: the string type cannot be changed, that is, when you want to change a String object, for example, if name = "madding" is used, the VM will not change the original object, but generate a new String object, and then let the name point to it, if the original "Tom" does not have any object to reference it, the garbage collection mechanism of the virtual machine will receive it.

 

Finally, the question about defining the String Stack

String S = new string () analyze the heap and stack, whether to define s first or new string () first ()
1. String str1 = "ABC ";
System. Out. println (str1 = "ABC ");

Steps:
1) create a space in the stack to store the reference str1;
2) Open up a space in the string pool to store the String constant "ABC ";
3) Reference str1 to the String constant "ABC" in the pool ";
4) The address referred to by str1 is the address where the constant "ABC" is located. The output value is true;

 

2. String str2 = new string ("ABC ");
System. Out. println (str2 = "ABC ");

Steps:
1) create a space in the stack to store the reference str2;
2) Open up a space in the heap to store a New String object "ABC ";
3) Reference str2 to the New String object "ABC" in the heap ";
4) The object address referred to by str2 is the heap address, the constant "ABC" address is in the pool, and the output is false;

 

3. String str3 = new string ("ABC ");
System. Out. println (str3 = str2 );

Steps:
1) create a space in the stack to store the reference str3;
2) create a new space in the heap to store another string object (different from str2;
3) Reference str3 to point to the New String object;
4) str3 and str2 point to different string objects in the heap. The address is also different and the output value is false;

 

4. String str4 = "A" + "B ";
System. Out. println (str4 = "AB ");

Steps:
1) create a space in the stack to store and reference str4;
2) based on the compiler's optimization function to merge known quantities, the pool opens up a space to store the merged String constant "AB ";
3) Reference str4 to the constant "AB" in the pool ";
4) str4 refers to the constant "AB" in the pool, and the output is true;

 

5. Final string S = "A"; // Note: Here s is modified with final, which is equivalent to a constant.
String str5 = S + "B ";
System. Out. println (str5 = "AB ");

Steps:
Same as 4

 

6. String S1 = "";
String S2 = "B ";
String str6 = S1 + S2;
System. Out. println (str6 = "AB ");

Steps:
1) open an intermediate storage reference S1 in the stack, S1 points to the String constant "A" in the pool ",
2) open an intermediate storage reference S2 in the stack. S2 points to the String constant "B" in the pool ",
3) create an intermediate storage reference str5 in the stack,
4) S1 + S2 restores a New String object "AB" through the tostring () method in the last step of stringbuilder. Therefore, a space is opened in the heap to store this object,
5) Reference str6 to the New String object restored in the heap (S1 + S2,
6) str6 points to the object in the heap, while the constant "AB" is in the pool, and the output is false.

 

7. String str7 = "ABC". substring (0, 2 );
Steps:
1) Open up a space in the stack to store the reference str7,
2) The substring () method restores a New String object "AB" (different from str6). A space is opened in the heap to store this object,
3) Reference str7 to point to the New String object in the heap,

 

8. String str8 = "ABC". touppercase ();
Steps:
1) Open up a space in the stack to store the reference str6,
2) The touppercase () method restores a New String object "ABC", and no new space is opened in the pool to store the String constant "ABC ",
3) Reference str8 to point to the New String object in the heap.

 

9. String S = "ABC ";

String S1 = s;

System. Out. println (S1 = "ABC ");

S = S + "hello ";

System. Out. println (S1 = "ABC ");

System. Out. println (S = "ABC ");

Steps:

1) Open up a space in the stack to store S;

2) Open up a space in the sting pool to store "ABC", and open up a space in the stack to store variable S1;

3) The system outputs true and opens a space in the heap to store "abchello ";

4) reference S to "abchello" in the heap ";

5) The system outputs true, and then outputs false;

 

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.