String的学习之itern()方法

什么都先不说,先看下面这个引入的例子:

String str1 = new String("张")+ new String("鲜生");    
System.out.println(str1.intern() == str1); 
System.out.println(str1 == "张鲜生");

本人JDK版本1.8,输出结果为:

true
true

再将上面的例子加上一行代码:

String str2 = "张鲜生";//新加的一行代码,其余不变
String str1 = new String("张")+ new String("鲜生");    
System.out.println(str1.intern() == str1); 
System.out.println(str1 == "张鲜生");

再运行,结果为:

false
false

是不是感觉莫名其妙,新定义的str2好像和str1没有半毛钱的关系,怎么会影响到有关str1的输出结果呢?其实这都是intern()方法搞的鬼!看完这篇文章,你就会明白。o(∩_∩)o

1.为什么要介绍intern()方法

intern()方法设计的初衷,就是重用String对象,以节省内存消耗。这么说可能有点抽象,那么就用例子来证明。

static final int MAX = 100000;
static final String[] arr = new String[MAX];
 
public static void main(String[] args) throws Exception {
	//为长度为10的Integer数组随机赋值
	Integer[] sample = new Integer[10];
	Random random = new Random(1000);
	for (int i = 0; i < sample.length; i++) {
	    sample[i] = random.nextInt();
	}
	//记录程序开始时间
	long t = System.currentTimeMillis();
	//使用/不使用intern方法为10万个String赋值,值来自于Integer数组的10个数
	    for (int i = 0; i < MAX; i++) {
	        arr[i] = new String(String.valueOf(sample[i % sample.length]));
	        //arr[i] = new String(String.valueOf(sample[i % sample.length])).intern();
	    }
	    System.out.println((System.currentTimeMillis() - t) + "ms");
	    System.gc();
}

这个例子就是为了证明使用intern()比不使用intern()消耗的内存更少。

先定义一个长度为10的Integer数组,并随机为其赋值,在通过for循环为长度为10万的String对象依次赋值,这些值都来自于Integer数组。两种情况分别运行,

这段代码是粘过来的,原文内博主写的是用eclipse来查看dump信息,这里由于我是idea,故说一下idea内查看dump的方法。

  • 首先在idea内设置Edit Configurations

image
image

  • VM options 输入如下-agentlib:hprof=heap=dump,format=b,file=E:temp/test1.hprof
    运行main线程后会在E:temp/test1.hprof路径下找到test1.hprof,这个文件可以通过jdk的jvisualvm运行打开,这里我们只要看一下创建了多少对象实例就可以了。在JAVA_HOME/bin目录下找到jvisualvm.exe

image
image

将文件打开后可两次结果如下:
在这里插入图片描述
image

从运行结果来看,不使用intern()的情况下,程序生成了213442个实例对象,而使用了intern()方法时,程序仅生成了13462个实例对象。自然也证明了intern()节省内存的结论。

细心的同学会发现使用了intern()方法后程序运行时间有所增加。这是因为程序中每次都是用了new String后又进行intern()操作的耗时时间,但是不使用intern()占用内存空间导致GC的时间是要远远大于这点时间的。

2.深入认识intern()方法

JDK1.7后,常量池被放入到堆空间中,这导致intern()函数的功能不同,具体怎么个不同法,且看看下面代码,这个例子是网上流传较广的一个例子,分析图也是直接粘贴过来的,这里我会用自己的理解去解释这个例子:

String s = new String("1");
s.intern();
String s2 = "1";
System.out.println(s == s2);
 
String s3 = new String("1") + new String("1");
s3.intern();
String s4 = "11";
System.out.println(s3 == s4);

输出结果为:

JDK1.6以及以下:false false
JDK1.7以及以上:false true

再分别调整上面代码2.3行、7.8行的顺序:

String s = new String("1");
String s2 = "1";
s.intern();
System.out.println(s == s2);
 
String s3 = new String("1") + new String("1");
String s4 = "11";
s3.intern();
System.out.println(s3 == s4);

输出结果为:

JDK1.6以及以下:false false
JDK1.7以及以上:false false
2.1 JDK1.6

image

在JDK1.6中所有的输出结果都是 false,因为JDK1.6以及以前版本中,常量池是放在 Perm 区(属于方法区)中的,熟悉JVM的话应该知道这是和堆区完全分开的。

使用引号声明的字符串都是会直接在字符串常量池中生成的,而 new 出来的 String 对象是放在堆空间中的。所以两者的内存地址肯定是不相同的,即使调用了intern()方法也是不影响的。

intern()方法在JDK1.6中的作用是:比如String s = new String(“张鲜生”),再调用s.intern(),此时返回值还是字符串"张鲜生",表面上看起来好像这个方法没什么用处。但实际上,在JDK1.6中它做了个小动作:检查字符串池里是否存在"张鲜生"这么一个字符串,如果存在,就返回池里的字符串;如果不存在,该方法会把"张鲜生"添加到字符串池中,然后再返回它的引用。然而在JDK1.7中却不是这样的,后面会讨论。

2.2 JDK1.7

针对JDK1.7以及以上的版本,我们将上面两段代码分开讨论。先看第一段代码的情况:

image

再把第一段代码贴一下便于查看:

String s = new String("1");
s.intern();
String s2 = "1";
System.out.println(s == s2);
 
String s3 = new String("1") + new String("1");
s3.intern();
String s4 = "11";
System.out.println(s3 == s4);

String s = newString(“1”),生成了常量池中的“1” 和堆空间中的字符串对象

s.intern(),这一行的作用是s对象去常量池中寻找后发现"1"已经存在于常量池中了

String s2 = “1”,这行代码是生成一个s2的引用指向常量池中的“1”对象

结果就是 s 和 s2 的引用地址明显不同。因此返回了false。

String s3 = new String(“1”) + newString(“1”),这行代码在字符串常量池中生成“1” ,并在堆空间中生成s3引用指向的对象(内容为"11")。注意此时常量池中是没有 “11”对象的

s3.intern(),这一行代码,是将 s3中的“11”字符串放入 String 常量池中,此时常量池中不存在“11”字符串,JDK1.6的做法是直接在常量池中生成一个 "11" 的对象

但是在JDK1.7中,常量池中不需要再存储一份对象了,可以直接存储堆中的引用。这份引用直接指向 s3 引用的对象,也就是说s3.intern() ==s3会返回true。

String s4 = “11”,这一行代码会直接去常量池中创建,但是发现已经有这个对象了,此时也就是指向 s3 引用对象的一个引用。因此s3 == s4返回了true。

下面继续分析第二段代码:
image

再把第二段代码贴一下便于查看:

String s = new String("1");
String s2 = "1";
s.intern();
System.out.println(s == s2);
 
String s3 = new String("1") + new String("1");
String s4 = "11";
s3.intern();
System.out.println(s3 == s4);

String s = newString(“1”),生成了常量池中的“1” 和堆空间中的字符串对象

String s2 = “1”,这行代码是生成一个s2的引用指向常量池中的“1”对象,但是发现已经存在了,那么就直接指向了它

s.intern(),这一行在这里就没什么实际作用了。因为"1"已经存在了。

结果就是s和s2的引用地址明显不同。因此返回了false。

String s3 = new String(“1”) + newString(“1”),这行代码在字符串常量池中生成“1” ,并在堆空间中生成s3引用指向的对象(内容为"11")。注意此时常量池中是没有“11”对象的。

String s4 = “11”,这一行代码会直接去生成常量池中的"11"

s3.intern(),这一行在这里就没什么实际作用了。因为"11"已经存在了。

结果就是s3和s4的引用地址明显不同。因此返回了false。

3 总结

终于要做Ending了。现在再来看一下开篇给的引入例子,是不是就很清晰了呢。

String str1 = new String("张鲜生")+ new String("鲜生");    
System.out.println(str1.intern() == str1); 
System.out.println(str1 == "张鲜生");

str1.intern() == str1就是上面例子中的情况,str1.intern()发现常量池中不存在“SEUCalvin”,因此指向了str1。 "SEUCalvin"在常量池中创建时,也就直接指向了str1了。两个都返回true就理所当然啦。

那么第二段代码呢:

String str2 = "张鲜生";//新加的一行代码,其余不变
String str1 = new String("张鲜生")+ new String("鲜生");    
System.out.println(str1.intern() == str1); 
System.out.println(str1 == "张鲜生");

image

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值