Small apprenticeship advanced series-JVM's String processing

Source: Internet
Author: User

For the String type, the documents on the java official website are described as follows:

The String class represents a String. All character strings (such as "abc") in java programs are implemented as such instances.

Strings are constants and their values cannot be changed after creation. Because the String object is unchangeable, it can be shared.

So how does jvm share these strings?

To save memory and improve resource reuse, jvm introduces the concept of a constant pool, which is part of the method area. One of its functions is to store various literal quantities and symbol references in production. Learn more about the JVM-memory Zone

From the concepts mentioned in the above section, we can know that ,. Why should we emphasize that it is generally the case? Because the String class provides an intern () method, it can help us add java strings that do not exist in the cache pool to the cache pool and return references to the String objects in the cache pool.

For details about the intern () method, let's give a brief description of the code later. Now we will focus on, under what circumstances can we determine the string variable value during compilation and add it to the buffer?

If the string connection expression of the program does not use variables or call methods, the value of the string variable can be determined during compilation and cached in the buffer, at the same time, let the variable point to this string; otherwise, the buffer will not be available, because the value of the string variable after the variable and the method are called can only determine the value of the connector during running, therefore, the value of the string variable cannot be determined during compilation, so that the string variable cannot be added to the buffer and used.

Let's take a look at how to verify the above conclusions through the Code and its compilation process.

Code 1 (no variables or call methods are used ):

                     String param1 = "abc"         String param2 = "abc" + "def"         String param3 = "abcdef"  }

First, open cmd.exe, compile the Java file, and run the following command to view the ByteCode after java Compilation ,:

Let's first explain what it means :. From this, we can see that in row 0th, row 3rd, and row 6th, the program extracts "abc" and "abcdef" from the constant pool and pushes them into the stack, and, the string references in rows 3rd and 6th are the same. This shows that,

 

                    String param = "abc"         String param1 = "3abc"         String param2 = param.length() + "abc"  }

Obviously, the output result is false.

 

 String param2 = param.length() + "abc" String param1 = "3abc";

Will it be cached and used directly by param1?

 

                    String param = "abc"         String newStr =  String("cde"         String param2 = "cde"           }

We can see that when creating a newStr String object, we first extract the String "cde" from the stack, and then call the String constructor to create an object by using the keyword "new, assign a new reference to newStr. Therefore, newStr does not point to the string "cde" in the buffer. Therefore, the overhead of string variables created using this method is usually relatively large.

Next, let's explain the intern () method. The official website describes this method as follows:

When the intern method is called, if the pool already contains a String equal to this String Object (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.

The following is an example of how to use the intern () method.

                     String param = "abc"         String newStr =  String("abc"         String param2 =  String("abc"          param2 = param2.intern();             System.out.println(param == newStr);             System.out.println(param == param2);      }

The expression ability of the document is limited and may be generally written. I don't know if you will have any other questions after reading this article. I hope you will come up with them and learn and make progress together. Thank you.

 

 

 

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.