当前位置: 当前位置:首页 > 热点 > JavaSE——字符串常量池(StringTable) 正文

JavaSE——字符串常量池(StringTable)

2024-04-29 04:38:45 来源:口口声声网 作者:休闲 点击:680次

JavaSE——字符串常量池(StringTable)

ced485cbb11e458d81a746890b32cf3f.gif

作者:敲代码の流川枫

博客主页:流川枫的字符博客

专栏:和我一起学java

语录:Stay hungry stay foolish

工欲善其事必先利其器,给大家介绍一款超牛的串常斩获大厂offer利器——牛客网

点击免费注册和我一起刷题吧  

文章目录

1. 字符串常量池

2. intern方法

3. 面试题


1. 字符串常量池(StringTable)

为了提升性能和减少内存开销,避免字符串的量池重复创建,JVM维护了一块特殊的字符内存空间,就是串常字符串常量池。字符串常量池由String类私有的量池维护

为了节省存储空间以及程序的运行效率,Java中还引入了:

1. Class文件常量池:每个.Java源文件编译后生成.Class文件中会保存当前类中的字符字面常量以及符号信息

2. 运行时常量池:在.Class文件被加载时,.Class文件中的串常常量池被加载到内存中称为运行时常量池,运行时常量池每个类都有一份

Java中有两种创建字符串对象的量池方式:

1. 直接使用字符串常量进行赋值

public class Test {    public static void main(String[] args) {        String s1 = "hello";        String s2 = "hello";        System.out.println(s1==s2);    }}//运行结果:true

使用String s1 = "hello";创建对象时,JVM先会去字符串池中查找是字符否存在"hello"这个对象,不存在,串常则在字符串池中创建"hello"这个对象,量池然后将池中"hello"这个对象的字符引用地址返回给字符串常量s1,这样s1会指向池中"hello"这个字符串对象;存在,串常则不创建任何对象,量池直接将池中"hello"这个对象的地址返回,赋给字符串常量

 String s2 = "hello";创建对象时,JVM先去字符串常量池找到"hello",并将地址引用给s2,因此如下图看,s1和s2引用指向的value数组是同一个数组

2. 通过new创建String类对象

public class Test {    public static void main(String[] args) {        String s1 = "hello";        String s2 = "hello";        String s3 = new String("hello");        String s4 = new String("hello");        System.out.println(s3==s4);        //运行结果:false    }}

s3和s4创建对象时,首先去字符串常量池中寻找有无"hello",有则不在池中再去创建这个对象了,直接在堆中创建一个"hello"字符串对象,然后将地址返回赋给引用s3或s4,这样就指向了堆中创建的字符串对象;如果没找到,则首先在字符串常量池池中创建一个"hello"字符串对象,然后再在堆中创建一个"hello"字符串对象,然后将地址返回赋给引用,如此就指向了堆中创建的对象

new关键字创建对象时,new出来的是一个新的对象,也即引用s3和s4指向的是两个不同的对象,因此语句输出:false

一道题:

//下面代码将输出什么内容:()public class SystemUtil{	public static boolean isAdmin(String userId){		return userId.toLowerCase()=="admin";	}	public static void main(String[] args){		System.out.println(isAdmin("Admin"));	}}

分析:

首先,==号两边的是引用数据类型,那么比较的是"地址相不相同"

然后isAdmin()方法中的"admin"是存放在字符串常量池中的,userId.toLowerCase()是new出来的,回的是一个新的对象

因此输出:false

2. intern方法

该方法的作用是手动将创建的String对象添加到常量池中

public class Test {    public static void main(String[] args) {        char[] chars = new char[]{'h','e','l','l','o'};        String s1 = new String(chars);        //s1.intern();        String s2 = "hello";        System.out.println(s1==s2);    }    //运行结果:false}

String s1 = new String(chars);时,拷贝一个新的数组赋给s1,即s1指向的是一个新的数组

String s2 = "hello";时,JVM首先在常量池中寻找"hello",发现没有,就创建一个"hello"存放到常量池中,将地址返回给s2

因此输出:false

添加s1.intern();

public class Test {    public static void main(String[] args) {        char[] chars = new char[]{'h','e','l','l','o'};        String s1 = new String(chars);        s1.intern();        String s2 = "hello";        System.out.println(s1==s2);    }    //运行结果:true}

s1.intern();将创建的String对象添加到常量池中, String s2 = "hello";时,JVM首先去常量池中寻找,找到"hello",然后将地址返回给s2

因此输出:true

3. 面试题

请解释String类中两种对象实例化的区别(JDK1.8中)

String str = "hello"

只会开辟一块堆内存空间,保存在字符串常量池中,然后str共享常量池中的String对象

String str = new String("hello")

会开辟两块堆内存空间,字符串"hello"保存在字符串常量池中,然后用常量池中的String对象给新开辟的String对象赋值

String str = new String(new char[]{'h', 'e', 'l', 'l', 'o'})

现在堆上创建一个String对象,然后利用copyof将重新开辟数组空间,将参数字符串数组中内容拷贝到String对象中

“ 本期的分享就到这里了, 记得给博主一个三连哈,你的支持是我创作的最大动力!

ced485cbb11e458d81a746890b32cf3f.gif

作者:综合
------分隔线----------------------------
头条新闻
图片新闻
新闻排行榜