天天看点

java 字符串池_什么是Java字符串池?

java 字符串池

As the name suggests, String Pool in java is a pool of Strings stored in Java Heap Memory. We know that String is a special class in java and we can create String objects using a new operator as well as providing values in double-quotes.

顾名思义, java中的字符串池是存储在Java堆内存中的字符串池。 我们知道String是Java中的一个特殊类,我们可以使用新的运算符以及以双引号提供值的方式创建String对象。

Java中的字符串池 (String Pool in Java)

Here is a diagram that clearly explains how String Pool is maintained in java heap space and what happens when we use different ways to create Strings.

这是一个清楚地说明Java堆空间中如何维护字符串池的图,以及当我们使用不同的方式创建字符串时会发生什么。

String Pool is possible only because String is immutable in Java and its implementation of String interning concept. String pool is also example of Flyweight design pattern.

字符串池之所以可能,是因为字符串在Java及其实现字符串内部化概念中是不可变的 。 字符串池也是Flyweight设计模式的示例。

String pool helps in saving a lot of space for Java Runtime although it takes more time to create the String.

字符串池有助于节省Java运行时空间,尽管创建字符串需要花费更多时间。

When we use double quotes to create a String, it first looks for String with the same value in the String pool, if found it just returns the reference else it creates a new String in the pool and then returns the reference.

当我们使用双引号创建一个String时,它将首先在String池中查找具有相同值的String,如果发现它只是返回引用,否则它将在池中创建一个新String,然后返回引用。

However using new operator, we force String class to create a new String object in heap space. We can use

intern()

method to put it into the pool or refer to another String object from the string pool having the same value.

但是,使用new运算符,我们强制String类在堆空间中创建一个新的String对象。 我们可以使用

intern()

方法将其放入池中,或从字符串池中引用另一个具有相同值的String对象。

Here is the java program for the String Pool image:

这是字符串池映像的Java程序:

package com.journaldev.util;

public class StringPool {

    /**
     * Java String Pool example
     * @param args
     */
    public static void main(String[] args) {
        String s1 = "Cat";
        String s2 = "Cat";
        String s3 = new String("Cat");
        
        System.out.println("s1 == s2 :"+(s1==s2));
        System.out.println("s1 == s3 :"+(s1==s3));
    }

}
           

Output of the above program is:

上面程序的输出是:

s1 == s2 :true
s1 == s3 :false
           

Recommended Read: 推荐阅读 : Java String Class Java字符串类

在字符串池中创建了多少个字符串? (How many Strings are getting Created in the String Pool?)

Sometimes in java interview, you will be asked a question around String pool. For example, how many strings are getting created in the below statement;

有时在Java面试中,系统会询问您有关字符串池的问题。 例如,在下面的语句中创建了多少个字符串;

String str = new String("Cat");
           

In the above statement, either 1 or 2 string will be created. If there is already a string literal “Cat” in the pool, then only one string “str” will be created in the pool. If there is no string literal “Cat” in the pool, then it will be first created in the pool and then in the heap space, so a total of 2 string objects will be created.

在上面的语句中,将创建1或2个字符串。 如果池中已经有字符串文字“ Cat”,则在池中将仅创建一个字符串“ str”。 如果池中没有字符串文字“ Cat”,则将首先在池中然后在堆空间中创建它,因此将总共创建2个字符串对象。

Read: Java String Interview Questions

阅读 : Java字符串面试问题

翻译自: https://www.journaldev.com/797/what-is-java-string-pool

java 字符串池