What is a Java string constant pool? Why do you have this constant pool?

Source: Internet
Author: User

Simple Introduction

The string Chang in Java is a pool of strings stored in the Java heap memory . We know that string is a special class in Java, and we can create a string object using the new operator, or you can create a string object with double quotation marks ("").

string constant pool in Java

, which clearly explains how to maintain a string constant pool in Java heap memory and how to store it in heap memory when we create strings in different ways.

There is a string constant pool because string is immutable (immutable) in Java and is an implementation of the string interning concept. The string constant pool is also an instance of Hengyuan mode (Flyweight).

string constant pooling helps save a lot of space for the Java runtime, although it takes more time to create strings.

When we create a string using double quotation marks, we first look for a string with the same value in the string constant pool, and if it finds a reference, it creates a new string in the pool and returns a reference to the new string.

If you create a string using the new operator, the string class is forced to create a new string object in the heap space. We can use the Intern () method to place it into a string constant pool or to find a string object with the same value from a string constant pool and return its reference

The following is a program implementation of the string constant pool in the preceding figure:

 PackageCom.journaldev.util; Public classStringpool {/*** Java String Pool Example *@paramargs*/     Public Static voidMain (string[] args) {String S1= "Cat"; String S2= "Cat"; String S3=NewString ("Cat"); System.out.println ("S1 = = S2:" + (s1==S2)); System.out.println ("S1 = = S3:" + (s1==S3)); }}

The output of the above program is

Sometimes inJava interview, you will be asked questions about the string constant pool. For example, how many string objects are created in the following statement:
New String ("Cat");  

In the preceding statement, you might create 1 or 2 string objects. If there is already a string "Cat"in the pool, only one string "str"will be created in the pool. If there is no string literal "Cat"in the pool, it will first be created in the pool and then created in the heap space, so a total of 2 string objects will be created.

string constant pool easy to understand

The allocation of strings, like other object assignments, is costly in time and space. In order to improve performance and reduce memory overhead, the JVM performs some optimizations when instantiating string constants. To reduce the number of strings created in the JVM, the string class maintains a string pool, and the JVM first checks the string constant pool whenever the code creates a string constant. If the string already exists in the pool, the instance reference in the pool is returned. If the string is not in the pool, a string is instantiated and placed in the pool. Java can be optimized because the strings are immutable and can be shared without worrying about data conflicts.

What is a Java string constant pool? Why do you have this constant pool?

1. string pool stored in Java heap memory

2, in order to let the data do not conflict sharing, etc.

What is a Java string constant pool? Why do you have this constant pool?

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.