Full understanding of the string data type in Java

Source: Internet
Author: User

From http://bbs.tech.ccidnet.com/htm_data/2/0612/237396.html

Full understanding of the string data type in Java


1. First, string does not belong to eight basic data types. String is an object.

Because the default value of an object is null, the default value of string is also null, but it is a special object and has some features that other objects do not have.

2. Both new string () and new string ("") declare a new null string, which is an empty string or not null;

3. String STR = "kvill ";
String STR = new string ("kvill"); difference:

Here, we will not talk about heap or stack, but simply introduce the simple concept of constant pool.

The constant pool refers to the data that is identified during the compilation period and saved in the compiled. Class file. It includes constants in classes, methods, interfaces, and other fields, as well as string constants.

Example 1:

String S0 = "kvill ";
String S1 = "kvill ";
String S2 = "KV" + "ill ";
System. Out. println (S0 = S1 );
System. Out. println (S0 = S2 );
Result:

True
True
First, we need to know that Java will ensure that a String constant has only one copy.

Because "kvill" in S0 and S1 in the example are string constants, they are determined during the compilation period, so S0 = S1 is true; "KV" and "ill" are both string constants. When a string is connected by multiple string constants, it must be a String constant, so S2 is also parsed as a String constant during compilation, so S2 is also a reference of "kvill" in the constant pool.

So we get S0 = S1 = S2;

The string created with new string () is not a constant and cannot be determined during the compilation period. Therefore, the strings created with new string () are not placed in the constant pool and they have their own address space.

Example 2:

String S0 = "kvill ";
String S1 = new string ("kvill ");
String S2 = "KV" + new string ("ill ");
System. Out. println (S0 = S1 );
System. Out. println (S0 = S2 );
System. Out. println (S1 = S2 );
Result:

False
False
False
In Example 2, S0 is still a "kvill" application in the constant pool. S1 is a reference of the new object "kvill" created at runtime because it cannot be determined during the compilation period, s2 is also an application that creates the "kvill" object because it has a new string ("ill") in the second half and cannot be determined during the compilation period; if you understand this, you will know why this result is obtained.

4. String. Intern ():

Another note: the constant pool exists in the. Class file and is loaded by JVM at runtime and can be expanded. The intern () method of string is a method to expand the constant pool. When a string instance STR calls the intern () method, Java checks whether the constant pool has the same UNICODE String constant, if yes, its reference is returned. If no, a string with Unicode equal to STR is added to the constant pool and Its Reference is returned. See example 3.

Example 3:

String S0 = "kvill ";
String S1 = new string ("kvill ");
String S2 = new string ("kvill ");
System. Out. println (S0 = S1 );
System. Out. println ("***********");
S1.intern ();
S2 = s2.intern (); // assign the reference of "kvill" in the constant pool to S2
System. Out. println (S0 = S1 );
System. Out. println (S0 = s1.intern ());
System. Out. println (S0 = S2 );
Result:

False
**********
False // although s1.intern () is executed, its return value is not assigned to S1
True // indicates that s1.intern () returns a reference to "kvill" in the constant pool.
True
Finally, let me get rid of another misunderstanding:

Someone said, "Use string. the intern () method can save a string class to a global string table. If a unicode string with the same value is already in this table, this method returns the address of an existing string in the table, if there is no string with the same value in the table, register the address in the table. "If I understand the global string table he said as a constant pool, his last sentence is, "If there is no string with the same value in the table, register your address in the table" is incorrect:

Example 4:

String S1 = new string ("kvill ");
String S2 = s1.intern ();
System. Out. println (S1 = s1.intern ());
System. Out. println (S1 + "" + S2 );
System. Out. println (s2 = s1.intern ());
Result:

False
Kvill
True
In this class, we do not have a "kvill" constant, so there is no "kvill" in the constant pool at the beginning. When we call s1.intern () then, a new "kvill" constant is added to the constant pool. The original "kvill" that is not in the constant Pool still exists, so it is not "registering your own address to the constant pool.

If S1 = s1.intern () is false, the original "kvill" still exists;

S2 is now the address of "kvill" in the constant pool, so S2 = s1.intern () is true.

5. About equals () and =:

In simple terms, this is to compare whether the unicode sequence of the two strings is equivalent. If the two strings are equal, true is returned, and = is to compare whether the addresses of the two strings are the same, that is, whether it is a reference to the same string.

6. the string is immutable.

This is a lot more important, as long as you know that the string instance will not change once it is generated, for example: String STR = "KV" + "ill" + "" + "Ans ";
There are four string constants. First, "KV" and "ill" generate "kvill" to exist in the memory, and then "kvill" and "generate" kvill "to exist in the memory, finally, "kvill ans" is generated, and the address of this string is assigned to STR, because the string's "immutable" produces many temporary variables, this is why stringbuffer is recommended, because stringbuffer can be changed.
 

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.