Java String Pool Depth resolution

Source: Internet
Author: User

Copyright NOTICE: This article for Bo Master original article, reproduced please indicate the source, Welcome to exchange Study!

At work, the string class is an object type that we use very frequently. In order to improve performance and reduce memory overhead, the JVM avoids duplicate string creation and maintains a special memory space, which is the core of our discussion today, the string pool. String pooling is maintained by the string class private.

We know that there are two ways to create a string object in Java: 1) Assign a value in literal form 2) Create a new string object with the New keyword. There are differences in performance and memory consumption between the two approaches.

Method One: Assign values in literal form, for example:

When a string is created in the form of literals, the JVM first looks in the string pool for the existence of an "AAA" object, creates an "AAA" object in the string pool if it does not exist, and then returns the reference address of the "AAA" object in the pool to the string constant str. STR will then point to the "AAA" string object in the pool, and if so, no object is created, directly returning the address of the "AAA" object in the pool and assigning it to the string constant.

In this example, the execution: str = = STR2, the following results are obtained:

This is because, when creating the string object str2, there is already a "AAA" object in the string pool, which directly returns the reference address of the object "AAA" to str2, so that str2 points to the "AAA" object in the pool, which means that Str and str2 point to the same object. So the statement System.out.println (str = = str2) output: true.

Method Two: Use the new keyword to create a new string object, for example:

When you create a new string object with the New keyword, the JVM first looks for the string object in the string pool that has no "AAA", and if so, does not create an "AAA" object in the pool, creating an "AAA" string object directly in the heap, and then the "AAA" in the heap The address of the object is assigned to the reference STR3, so that str3 points to the "AAA" string object created in the heap, and if not, first create an "AAA" string object in the string pool, then create an "AAA" string object in the heap, and then put the "AAA" in the heap The address of the string object is returned to the STR3 reference, so that str3 points to the "AAA" string object created in the heap.

In this example, execute: STR3 = = STR4, get the following result:

Because, when you create an object with the New keyword, each time new comes out is a fresh object, which means that the reference STR3 and STR4 point to two different objects, so the statement System.out.println (STR3 = = STR4) Outputs: false.

The implementation of a string pool has a precondition: the string object is immutable. This guarantees that multiple references can point to the same object in the string pool as a colleague. If the string is mutable, then a reference operation alters the value of the object and affects other references, which is obviously unreasonable.

Advantages and Disadvantages of string pooling: The advantage of a string pool is that it avoids the creation of strings of the same content, saves memory, eliminates the time to create the same string, and improves performance; On the other hand, the disadvantage of a string pool is the time it takes for the JVM to traverse objects in a constant pool. But its time cost is relatively low.

The intern method uses: an initially empty string pool, which is maintained by the class string alone. When the Intern method is called, if the pool already contains a string equal to this string object (as determined by the Equals (Oject) 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. For any two strings s and T, S.instan () = = T.instan is true only if and only if S.equals (t) is true. All literal string and string assignment constant expressions are manipulated using the Intern method.

GC Reclamation: A shared string object is maintained in a string pool, and these strings are not reclaimed by the garbage collector.

Summary: Strings are constants, each string object in a string pool has a unique copy, can be pointed to by multiple references, avoids duplicate creation of identical strings, string objects created with literal values are stored in the string pool, and string objects from the keyword new are stored in the heap.

Java string Pool depth parsing

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.