Go deep into Java -- string

Source: Internet
Author: User

The Java string was originally a very inconspicuous thing. It is generally known that it is not the basic data type of Java, but also exists as a class. At the same time,Java strings can only be compared by str1.equal (str2). (It turns out that = can be used for comparison,ProgramIs not supported). Many of you must have the same knowledge as me. As for why I suddenly became interested in string, I did not mention the source, and I just learned something about it. (You have not found any books to read, so you can only browse other gardens and then extract them .)

 

I. Common sense

1. the source code of the string class is defined as public final class string and cannot be inherited.

2. in Java, all strings are constants and the value of an object cannot be changed after being generated. Assign a value to the string variable, but modify the reference. The string class contains substring () methods that can return substrings. However, in fact, these methods do not modify the content of the original string class. They only create another string class, and return it as the result.

3. The essence of the string class is the character array, public final char value [].

String STR = "ABC"; equivalent to Char data [] = {"A", "B", "C"}; string STR = new string (data );

4. Java has reloaded the string type.+Operator, which can be used directly+Connect two strings

5. Call the intern () method of the string class at runtime to dynamically add objects to the string pool. When the intern method is called, if the pool already contains a string equal to this string object (the object is determined by the equals (object) method), the string in the pool is returned. Otherwise, this string object is added to the pool and a reference to this string object is returned.

6. When the program runs, JVM maintains two different memories to maintain string data. The first is the string constant pool, the string constant pool, and the second is the stack.

7. There are three ways to create a string:

 
String str1 = "ABC"; string str2 = new string ("ABC"); string str3 = str1 + "CDE ";

 

2. go deep into creating strings ("ABC" is created during compilation, and new string ("ABC") is determined during runtime)

Principle 1: when using any method to create a String object S, Java Runtime (running JVM) will find whether a String object with the same content exists in the string pool. If it does not exist, creates a string s in the pool. Otherwise, it is not added in the pool. Principle 2: in Java, if the new keyword is used to create an object, a new object will be created in the stack area. Principle 3: Create a String object by specifying (string STR = "ABC") Directly or concatenating a string (string STR = "AB" + "cd, only the string in the string pool will be checked and maintained. If there are objects with the same content in the pool, it will not be created in the pool; otherwise, a New String object will be created in the pool. This method does not create an object in the stack, whether or not it exists. Principle 4: Use an expression containing variables (string STR = "AB" + str1) to create a String object. This will not only check and maintain the string pool, in addition, a string object is created in the stack area, regardless of whether the same content exists in the stack area ).

 

Three, string splicing (reproduced from the http://blog.csdn.net/izard999/article/details/6708433)

First, take a lookCode

 
String STR = NULL; STR = STR + "ABC"; system. Out. pprinltn (STR); // "nullabc"

Surprised, right. Let's take a look at the explanation:

Str1 + str2:

RuntimeThe concatenation of two strings str1 and str2 will first call String. valueof (OBJ). This obj is str1, while the implementation of string. valueof (OBJ) is return OBJ = NULL? "Null": obj. tostring (), then generate stringbuilder, call the stringbuilder (str1) constrmethod, initialize stringbuilder, the length is str1.length () + 16, and call append (str1 )!
Next, call stringbuilder. append (str2), concatenate the second string, and then call stringbuilder. tostring to return the result!

Therefore, the answer to that question comes from stringbuilder. append ("null"). append ("ABC"). tostring ();

Naturally, the following results are not surprising:

 
String STR = nullstr = STR + NULL; // "nullnull"

Iv. null values and null values of strings

String STR = new string (); // initialize a newly createdStringObject To indicate an empty character sequence. STR = "";

 
String A = new string (); A = a + NULL; // "null"

 

5. Comparison of strings

= Compare the string's bucket address, or reference

Equal () compares the value of the string content

 

6. Some programs

 
Public static void main (string [] ARGs) {// create the object "ABC" in the variable pool and heap respectively ", s1 points to the heap object string S1 = new string ("ABC"); // creates the object "ABC" in the heap ", s2 points to the heap object string S2 = new string ("ABC"); // S3 directly points to the heap object string S3 = "ABC "; // create an object "A" in the pool, and the S4 points to the object string S4 = "A"; // create the object "BC" in the pool and create the object "ABC" in the heap ", and S5 points to the heap object string S5 = S4 + "BC"; // creates the object "AB" and "C" in the pool ", s6 points to the object "ABC" string S6 = "AB" + "C"; system. out. println (S1 = S2); // falsesystem. out. println (S1 = S3); // falsesystem. out. println (s2 = S3); // falsesystem. out. println (s1.intern () = S3); // truesystem. out. println (S3 = S5); // falsesystem. out. println (S3 = s6); // true}

There are also a few Frequently Asked Questions:

1 .
string S1 =
New string ( " S1 " );
string S2 =
New string ( " S1 " );
several string objects are created above ?
answer: Three. Create one in the constant pool during compilation and two in heap during runtime.

2.
String S1=
"S1";
String S2=S1;
S2=
"S2";
What is the string in the object to which S1 points??
Answer:"S1"

 

 

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.