004 Several features of Java strings

Source: Internet
Author: User

In the previous article in this series (003 Java string), some basic situations and the whole architecture of the string class in the Java language are explained and analyzed, I believe that we have mastered it well. This tutorial mainly complements some of the important features of the string class to help you avoid some of the pitfalls of using the string process.

First, add a concept that is used very frequently in the JDK: immutable classes. The so-called immutable class refers to the object of this class will not be changed after the generation, about the advantages of immutable classes, shortcomings, especially in Java concurrency programming advantages, here temporarily skip. So how do you define an immutable class? If you look closely at the source code of the string class, you will surely find that all of the methods of the string class that are likely to modify the strings object have created a new string object instead of modifying it on the original string object. So the string class is a very standard immutable class.

If you have ever done a Java face test, then you will not be unfamiliar with the following topics:

String a = "test";

String b = "TE" + "St";

System.out.println (A = = B);

System.out.println (A.equals (b));

We strongly recommend that you first think about it, and then come to the answer and then look down. The correct answer is true and true, congratulations. Although the answer is simple, the mechanism behind it is not easy, the main reason for this result is three: one is the string constant defined by double quotation marks, the method area (constant area) stored in the JVM memory model, and the second is that the string is an immutable class, and the JVM has special handling of the strings constants: Cache processing , which means that a string constant exists only in one copy of the entire JVM, and that the Java compiler optimizes the string constants that can be determined at compile time, that is, merging operations automatically. I drew a diagram of the layout of a string in the simplified version of the JVM memory model:

As you can see from this diagram, for string constants, the JVM stores it in the method area.

? ? Next, take a look at the following code:

String a = "test";

String b = new String (a);

String c = new String (a);

System.out.println (A = = B);

System.out.println (b = = c);

It is also recommended that you first think about the answer. If the answer is not false and false, then you need to look at the next content and think. We found here that using the new algorithm to generate two objects, they no longer conform to the above mentioned string constants, and they are laid out in the JVM's simplified memory model as shown in:

It can be concluded from this graph that if you use the new operator to generate a string object, the string object must be stored in the heap space as a newly generated object.

? ? For the memory layout of a string object, let's look at the intern function in the String class:

Public native String intern ();

If your English is not bad, it is recommended to take a closer look at the comment of the method, from which you can get most of the properties of the method. After reading and thinking, you can use the following code to test whether it is fully understood, the code is as follows:

String a = "test";

String b = new String (a);

System.out.println (A = = A.intern ());

System.out.println (b = = B.intern ());

System.out.println (A = = B);

System.out.println (a.intern () = = B.intern ());

If the answer you give is true, False, false, and true, then congratulations, the next written interview question about the memory layout of string is you. If the answer does not match exactly, take a closer look at the diagram of the JVM memory layout model and think about it:

Intern This local method, for a string object stored in the heap memory and in the method area, is the method that obtains the actual location where the string is stored in the JVM. All string values used in a Java application are actually stored only in the constant area of the JVM's method area, and the string objects in the heap memory do not actually store string values. If you understand this, the answer to the above code is obvious.

? ? Finally, we need to pay attention to the trim () method in the String class, the source code is as follows:

Public String trim () {

int len = value.length;

int st = 0;

Char[] val = value; /* Avoid GetField opcode */

while ((St < Len) && (Val[st] <= ") {

st++;

}

while ((St < Len) && (val[len-1] <= ") {

len--;

}

Return ((St > 0) | | (Len < value.length)) ? SUBSTRING (ST, Len): this;

}

I believe that the first programming language that most Java programmers learn is c\c++, and it is natural to assume that the trim () method of the string class is also used to remove the front and back whitespace characters of a string. When you look closely at the source code of the trim () method, you may find that the Java version trim () method removes more than just whitespace characters (spaces, line breaks, tab characters), but all Unicode codes that are less than or equal to 32 (a space Unicode code).

? ? Through this and the previous tutorial on the string class, I believe you have a more in-depth understanding of the structure and basic characteristics of the string class in JDK1.7. To sum up, the two most critical difficulties in the string class are character encoding and memory layout, which I believe are well mastered by all of you. If you have any questions or suggestions, please leave me a message.

This series of documents will be published in my public number, welcome to sweep code attention.

                

004 Several features of Java strings

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.