JVM pool -- string, integer

Source: Internet
Author: User

---- String
1. for the number of characters in a Java program, JVM uses a string pool (stirng pool) to save them: When a string is directly used for the first time, JVM will put it into the string pool for caching, which is a bit like
The application of the metadata-sharing mode. In general, the string objects in the string pool are not recycled by the garbage collector. When the program needs to use this string again, you do not need to create a new string, but directly direct the reference variable
The existing string in the string pool.

2. String STR = new string ("Hello, wrold! "); How many objects are created?
The answer is: 2. String pool. Heap memory.

3. As mentioned above, in addition to directly creating string objects using strings, you can also create string objects using string connection expressions. Therefore, you can also assign a string connection expression to a string variable.
If the value of this string connection expression can be determined during compilation, JVM calculates the value of this string variable during compilation and points it to the corresponding string in the string pool. For example:
String A = "Hello, wrold! ";
String B = "hello" + ", wrold" + "! ";
System. Out. println (A = B );
The value of B in the above program is a string connection expression, but the value of this string connection expression can be determined at compilation, so JVM will calculate the value of B at compilation, and point B to the corresponding string in the string pool.
Therefore, when the above program judges a = B, it will output true.
It should be noted that the above string connection expression contains both the string direct quantity and integer direct quantity. No variables are involved and there is no way to call them. Therefore, the JVM can determine the value of the string connection expression during compilation.
This string variable points to the corresponding string in the string. However, if the program uses a variable or calls a method, the value of the string connection expression can only be determined at runtime, and the string cannot be determined during compilation.
The value of the variable. Therefore, the JVM string Pool cannot be used. Example:
Int Len = 12;
String A = "Hello, wrold! Length: 12 ";
String B = "hello" + ", wrold" + "! "+" Length: "+ 12;
String c = "hello" + ", wrold" + "! "+" Length: "+" Hello, wrold! ". Length ();
String d = "hello" + ", wrold" + "! "+" Length: "+ Len;
System. Out. println ("A:" + );
System. Out. println ("B:" + B );
System. Out. println ("C:" + C );
System. Out. println ("D:" + D );
System. Out. println (A = B); // true: the value of B can be determined during compilation.
System. Out. println (A = C); // false: Because C contains a method call, it cannot be determined during compilation.
System. Out. println (A = D); // false: Because C contains variables, it cannot be determined during compilation.

Of course, there is one exception: if all the variables in the String concatenation operation are modified using final, then the JVM can determine the value of the String concatenation expression at compilation, similarly, it directs the string to the corresponding one in the JVM string pool.

String.
Example:
Final int Len = 12; // use final
Final string S = "Length:"; // use final
String A = "Hello, wrold! Length: 12 ";
String B = "hello" + ", wrold" + "! "+" Length: "+ 12;
String c = "hello" + ", wrold" + "! "+" Length: "+ Len;
String d = "hello" + ", wrold" + "! "+ S + Len;

System. Out. println ("A:" + );
System. Out. println ("B:" + B );
System. Out. println ("C:" + C );
System. Out. println ("D:" + D );
System. Out. println (A = B); // true: the value of B can be determined during compilation.
System. Out. println (A = C); // true
System. Out. println (A = D); // false

4. String STR = "A" + "B" + "C"; how many string objects are created?
Some people think it is four, and some people think it is five. However, only one string object is actually created, because the STR value can be determined during compilation, JVM will calculate the STR value as "ABC" at compilation, and then
Directly put it in the string pool and point STR to this constant. Therefore, there are no string objects such as "a", "B", and "C.

5. String is unchangeable. Therefore, if the program requires a string sequence that will change. Use stringbuffer or stringbuilder.
In fact, stringbuilder should be preferred. The only difference between stringbuilder and stringbuffer is that stringbuffer is thread-safe, that is, most of the methods in stringbuffer are
Added the synchronized modifier. Adding the synchronized modifier to the method can ensure the thread safety of the method, but it will reduce the execution efficiency of the method. In a multi-threaded environment, the stringbuilder class should be used for tables first.

String.

---- Integer
1. first look at the example.
Integer a1 = 200;
Integer a2 = 200;
System. Out. println (a1 = a2); // false
Integer a3 = 100;
Integer A4 = 100;
// Integer ranges from-128 to 127 (flyweight (metadata mode), saving memory overhead)
System. Out. println (A3 = A4); // true
The following is an individual's guess based on the string pool mechanism (no specific information is found for the time being, as if I had seen similar information before ).
JVM also performs special processing on integer. It can be described as an integer whose nominal value is within the range of-128-127. If an integer instance is created in the form of a value assignment, it is directed to the corresponding integer instance in the constant pool.
In other words, the JVM opened up a space when it was started, and put all the integers with a nominal value range of-128-127 there. If an integer instance within this range is created in the future, you do not need to create an object in heap. Direct the object to the created object in the pool. Why? Of course, it is for cache and sharing.
In fact, this is another classic application of the metadata mode. How is this implemented? Generally, it is done by combining the factory and the multi-instance mode. In fact, you can change the singleton mode to the singleton mode. The above models are not described here.
Note: If the above Code is:
Integer a3 = new INTEGER (100 );
Integer A4 = new INTEGER (100 );
System. Out. println (A3 = A4) // false
Because the new instance creation method is specified, no matter what is shared, the object is directly created in the heap. Smart, you should understand the mechanism of the string class at once.

PS: The above can be said to be some special processing mechanisms of the JVM class. In most mainstream programming languages, compilers optimize their common classes and syntaxes. For example, for syntax such as for... each. Therefore, the modern JVM can be said to be faster and faster.

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.