深入理解java虚拟机(三):String.intern()-字符串常量池

深入理解java虚拟机(一):java内存区域(内存结构划分)
深入理解java虚拟机(二):java内存溢出实战
 
深入理解java虚拟机(三):String.intern()-字符串常量池
深入理解java虚拟机(四):对象存活判定算法和垃圾收集算法
深入理解java虚拟机(五):hotspot垃圾收集算法实现 
深入理解java虚拟机(六):java垃圾收集分析实战(内存分配与回收策略)
深入理解java虚拟机(七):java垃圾收集分析总结 

深入理解java虚拟机(八):java内存分析工具-MAT和OQL

 

看源码:

 public native String intern();
 
 Returns a canonical representation for the string object.

A pool of strings, initially empty, is maintained privately by the class String.

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

All literal strings and string-valued constant expressions are interned. String literals are defined in §3.10.5 of the Java Language Specification

Returns:
a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.

 

意思是说当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(该对象由 equals(Object) 方法确定),则返回池中的字符串。否则,将此 String 对象添加到池中,并且返回此 String 对象的引用

看下面这段代码:

public class StringInternTest {

	public static void main(String[] args) {

		String str1 = new StringBuilder("chaofan").append("wei").toString();
		System.out.println(str1.intern() == str1);

		String str2 = new StringBuilder("ja").append("va").toString();
		System.out.println(str2.intern() == str2);
	}
}


有图可知,当在jdk1.6中运行时,会得到两个false,而在jdk1.7中运行,会得到一个true和一个false。
产生的差异在于在jdk1.6中 intern 方法会把首次遇到的字符串实例复制到永久待(常量池)中,并返回此引用;但在jdk1.7中,只是会把首次遇到的字符串实例的引用添加到常量池中(没有复制),并返回此引用
所以在jdk1.7中执行上面代码,str1返回true是引用他们指向的都是str1对象(堆中)(池中不存在,返回原引用),而str2返回false是因为池中已经存在"java"了(关键词),所以返回的池的对象,因此不相等。

 

看下面实例:

@Test
	public void test3(){
		
		String str1 = "a";
		String str2 = "b";
		String str3 = "ab";
		String str4 = str1 + str2;
		String str5 = new String("ab");
		 
		System.out.println(str5.equals(str3));//true
		System.out.println(str5 == str3);//false
		System.out.println(str5.intern() == str3);//true
		System.out.println(str5.intern() == str4);//false
		
		
	}
	@Test
	public void test4(){
		
		String a = new String("ab");
		String b = new String("ab");
		String c = "ab";
		String d = "a" + "b";
		String e = "b";
		String f = "a" + e;

		System.out.println(b.intern() == a);//fasle
		System.out.println(b.intern() == c);//true
		System.out.println(b.intern() == d);//true  编译期d已确定(修改、赋值)为ab
		System.out.println(b.intern() == f);//false
		System.out.println(b.intern() == a.intern());//true
		
		
	}
	@Test
	public void test5(){
		// 编译期已确定
		String a = "abc";
		String b = "abc";
		String c = "a" + "b" + "c";
		String d = "a" + "bc";
		String e = "ab" + "c";
		        
		System.out.println(a == b);//true 
		System.out.println(a == c);//true
		System.out.println(a == d);//true
		System.out.println(a == e);//true
		System.out.println(c == d);//true
		System.out.println(c == e);//true
		
		
	}


 

  • 9
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值