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 ClassJava字符串类

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

java 字符串池_什么是Java字符串池?相关推荐

  1. java 字符串乱码_这份Java面试题含答案解析竟然真的让你不用在面试上“如履薄冰”...

    面试题集共分为以下十部分: 一.Core Java: 1 - 95 题1 - 24 页 基础及语法: 1 - 61 题1 - 13 页 异常: 62 - 69 题13 - 15 页 集合: 70 - ...

  2. java集合转字符串拼接_关于集合和字符串的互转实现方法

    今天在写项目的时候遇到一个问题,就是要把得到的一个集合转换成字符串,发现 import org.apache.commons.lang.stringutils; 有这么一个简单的方法:string s ...

  3. java字符串拼接_这样写Java,同事直呼666

    作者:涛姐涛哥 来源:cnblogs.com/taojietaoge/p/11575376.html 一.MyBatis 不要写 1=1 当遇到多个查询条件,使用where 1=1 可以很方便的解决我 ...

  4. java 手编线程池_死磕 java线程系列之自己动手写一个线程池

    欢迎关注我的公众号"彤哥读源码",查看更多源码系列文章, 与彤哥一起畅游源码的海洋. (手机横屏看源码更方便) 问题 (1)自己动手写一个线程池需要考虑哪些因素? (2)自己动手写 ...

  5. java 对象锁定_少锁定Java对象池

    java 对象锁定 自从我写任何东西以来已经有一段时间了,我一直在忙于我的新工作,其中涉及在性能调优方面做一些有趣的工作. 挑战之一是减少应用程序关键部分的对象创建. 尽管Java随着时间的推移已改进 ...

  6. 自定义java线程池_我的Java自定义线程池执行器

    自定义java线程池 ThreadPoolExecutor是Java并发api添加的一项功能,可以有效地维护和重用线程,因此我们的程序不必担心创建和销毁线程,也不必关注核心功能. 我创建了一个自定义线 ...

  7. java设计高并发内存池_高并发服务器-连接池的设计

    高并发服务器-连接池的设计 高并发服务器需要有一些池的设计,如内存池,连接池,数据库连接池. 池(pool)的设计主要考虑到一些资源的频繁申请和释放,尤其是在高并发的服务器中,几万甚至几十万并发每秒, ...

  8. java stringbuffer原理_深入理解Java:String

    在讲解String之前,我们先了解一下Java的内存结构. 一.Java内存模型 按照官方的说法:Java 虚拟机具有一个堆,堆是运行时数据区域,所有类实例和数组的内存均从此处分配. JVM主要管理两 ...

  9. sas java 虚拟机异常_深入理解JAVA虚拟机之异常诊断

    常见的JAVA虚拟机HotSpot虚拟机运行时数据库由5部分构成:方法区,堆,虚拟机栈,本地方法栈,程序计数器.下面列举各个部分可能出现的异常及其出现原因. 1.方法区存放的已被虚拟机加载的类型信息, ...

最新文章

  1. 戏说 .NET GDI+系列学习教程(三、Graphics类的应用_验证码)
  2. 在vmware的Solaris虚拟机中安装vmtool
  3. 智点财务软件记账凭证的录入
  4. 内存分析工具MAT的使用
  5. 框架生成的HTML修改,Django框架form表单验证 修改html标签的样式
  6. 分析单点登录cas的解决方式
  7. tabindex, taborder和notab属性的区别
  8. C++之map插入数据相同的key不能覆盖value解决办法
  9. Java实现单例模式之饿汉式、懒汉式、枚举式,带测试。
  10. java马克思手稿_java 循环嵌套解决一元,二元,三元方程(增长率,鸡兔同笼,马克思手稿)...
  11. 驱动中EXPORT_SYMBOL()的作用
  12. [大学回忆录-思想]为博乎?为专乎?
  13. Javaweb 网上订餐系统
  14. SQL 2008下载安装及问题解决
  15. 海康视频的4G接入-实时浏览
  16. 2021年起重机司机(限桥式起重机)考试题及起重机司机(限桥式起重机)报名考试
  17. 无线上网认证之Portal认证——企业WiFi管家
  18. Android听筒模式和免提模式的切换
  19. 【优化】近端梯度下降(Proximal Gradient Descent)求解Lasso线性回归问题
  20. Vue CLI 脚手架

热门文章

  1. Centos如何通过yum安装php7
  2. Laravel 5.x 启动过程分析 [转]
  3. ZC_汇编指令_cmp
  4. Spring MVC自动为对象注入枚举数据
  5. html5各种页面切换效果和模态对话框
  6. [转载] 七龙珠第一部——第005话 邪恶沙漠的雅木茶
  7. 转:给.net 程序员的一些建设
  8. [转载] json字符串转list_Python入门进阶教程JSON操作
  9. [转载] Python利用pandas处理Excel数据的应用
  10. [转载] python数学计算模块之math常用函数学习使用