博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 字符串池_什么是Java字符串池?
阅读量:2532 次
发布时间:2019-05-11

本文共 2787 字,大约阅读时间需要 9 分钟。

java 字符串池

As the name suggests, String Pool in java is a pool of Strings stored in . 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中的字符串池是存储在的字符串池。 我们知道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 and its implementation of concept. String pool is also example of .

字符串池之所以可能,是因为及其实现概念中 。 字符串池也是示例。

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 :trues1 == s3 :false
Recommended Read:
推荐阅读

在字符串池中创建了多少个字符串? (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 字符串池

转载地址:http://mmozd.baihongyu.com/

你可能感兴趣的文章
My安卓知识6--关于把项目从androidstudio工程转成eclipse工程并导成jar包
查看>>
旧的起点(开园说明)
查看>>
生产订单“生产线别”带入生产入库单
查看>>
crontab导致磁盘空间满问题的解决
查看>>
java基础 第十一章(多态、抽象类、接口、包装类、String)
查看>>
Hadoop 服务器配置的副本数量 管不了客户端
查看>>
欧建新之死
查看>>
自定义滚动条
查看>>
APP开发手记01(app与web的困惑)
查看>>
笛卡尔遗传规划Cartesian Genetic Programming (CGP)简单理解(1)
查看>>
mysql 日期时间运算函数(转)
查看>>
初识前端作业1
查看>>
ffmpeg格式转换命令
查看>>
万方数据知识平台 TFHpple +Xpath解析
查看>>
Hive实现oracle的Minus函数
查看>>
秒杀多线程第四篇 一个经典的多线程同步问题
查看>>
RocketMQ配置
查看>>
vs code调试console程序报错--preLaunchTask“build”
查看>>
蚂蚁金服井贤栋:用技术联手金融机构,形成服务小微的生态合力
查看>>
端口号大全
查看>>