java面试题之----String的intern

时间:2024-07-05 14:03:14

When---什么时候需要了解String的intern方法:

面试的时候(蜜汁尴尬)!虽然不想承认,不过面试的时候经常碰到这种高逼格的问题来考察我们是否真正理解了String的不可变性、String常量池的设计以及String.intern方法所做的事情。但其实,我们在实际的编程中也可能碰到可以利用String.intern方法来提高程序效率或者减少内存占用的情况,这个我们等下会细说。

What---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 section 3.10.5 of the The Java? Language Specification.

上面是jdk源码中对intern方法的详细解释。简单来说就是intern用来返回常量池中的某字符串,如果常量池中已经存在该字符串,则直接返回常量池中该对象的引用。否则,在常量池中加入该对象,然后 返回引用。下面的一个例子详细的解释了intern的作用过程:

Now lets understand how Java handles these strings. When you create two string literals:

String name1 = "Ram"; 

String name2 = "Ram";

In this case, JVM searches String constant pool for value "Ram", and if it does not find it there then it allocates a new memory space and store value "Ram" and return its reference to name1. Similarly, for name2 it checks String constant pool for value "Ram" but this time it find "Ram" there so it does nothing simply return the reference to name2 variable. The way how java handles only one copy of distinct string is called String interning.

How---String.intern方法在jdk1.7之前和之后的区别:

简单的说其实就一个:在jdk1.7之前,字符串常量存储在方法区的PermGen Space。在jdk1.7之后,字符串常量重新被移到了堆中。

Back---重回String设计的初衷:

Java中的String被设计成不可变的,出于以下几点考虑:

1. 字符串常量池的需要。字符串常量池的诞生是为了提升效率和减少内存分配。可以说我们编程有百分之八十的时间在处理字符串,而处理的字符串中有很大概率会出现重复的情况。正因为String的不可变性,常量池很容易被管理和优化。

2. 安全性考虑。正因为使用字符串的场景如此之多,所以设计成不可变可以有效的防止字符串被有意或者无意的篡改。从java源码中String的设计中我们不难发现,该类被final修饰,同时所有的属性都被final修饰,在源码中也未暴露任何成员变量的修改方法。(当然如果我们想,通过反射或者Unsafe直接操作内存的手段也可以实现对所谓不可变String的修改)。

3. 作为HashMap、HashTable等hash型数据key的必要。因为不可变的设计,jvm底层很容易在缓存String对象的时候缓存其hashcode,这样在执行效率上会大大提升。

Deeper---直接来看例子:

首先来试试下面程序的运行结果是否与预想的一致:

java面试题之----String的intern
 String s1 = new String("aaa");
String s2 = "aaa";
System.out.println(s1 == s2); // false s1 = new String("bbb").intern();
s2 = "bbb";
System.out.println(s1 == s2); // true s1 = "ccc";
s2 = "ccc";
System.out.println(s1 == s2); // true s1 = new String("ddd").intern();
s2 = new String("ddd").intern();
System.out.println(s1 == s2); // true s1 = "ab" + "cd";
s2 = "abcd";
System.out.println(s1 == s2); // true String temp = "hh";
s1 = "a" + temp;
// 如果调用s1.intern 则最终返回true
s2 = "ahh";
System.out.println(s1 == s2); // false temp = "hh".intern();
s1 = "a" + temp;
s2 = "ahh";
System.out.println(s1 == s2); // false temp = "hh".intern();
s1 = ("a" + temp).intern();
s2 = "ahh";
System.out.println(s1 == s2); // true s1 = new String("1"); // 同时会生成堆中的对象 以及常量池中1的对象,但是此时s1是指向堆中的对象的
s1.intern(); // 常量池中的已经存在
s2 = "1";
System.out.println(s1 == s2); // false String s3 = new String("1") + new String("1"); // 此时生成了四个对象 常量池中的"1" + 2个堆中的"1" + s3指向的堆中的对象(注此时常量池不会生成"11")
s3.intern(); // jdk1.7之后,常量池不仅仅可以存储对象,还可以存储对象的引用,会直接将s3的地址存储在常量池
String s4 = "11"; // jdk1.7之后,常量池中的地址其实就是s3的地址
System.out.println(s3 == s4); // jdk1.7之前false, jdk1.7之后true s3 = new String("2") + new String("2");
s4 = "22"; // 常量池中不存在22,所以会新开辟一个存储22对象的常量池地址
s3.intern(); // 常量池22的地址和s3的地址不同
System.out.println(s3 == s4); // false // 对于什么时候会在常量池存储字符串对象,我想我们可以基本得出结论: 1. 显示调用String的intern方法的时候; 2. 直接声明字符串字面常量的时候,例如: String a = "aaa";
// 3. 字符串直接常量相加的时候,例如: String c = "aa" + "bb"; 其中的aa/bb只要有任何一个不是字符串字面常量形式,都不会在常量池生成"aabb". 且此时jvm做了优化,不// 会同时生成"aa"和"bb"在字符串常量池中
java面试题之----String的intern

如果有出入的话,再来看看具体的字节码分析:

java面试题之----String的intern
 /**
* 字节码为:
* 0: ldc #16; //String 11 --- 从常量池加载字符串常量11
2: astore_1 --- 将11的引用存到本地变量1,其实就是将s指向常量池中11的位置
*/
String s = "11"; /**
* 0: new #16; //class java/lang/String --- 新开辟了一个地址,存储new出来的对象
3: dup --- 将new出来的对象复制了一份到栈顶(也就是s1最终指向的是堆中的另一个存储字符串11的地址)
4: ldc #18; //String 11          
6: invokespecial #20; //Method java/lang/String."<init>":(Ljava/lang/String;)V
9: astore_1
*/
String s1 = new String("11"); /**
* 0: new #16; //class java/lang/StringBuilder --- 可以看到jdk对字符串拼接做了优化,先是建了一个StringBuilder对象
3: dup
4: new #18; //class java/lang/String --- 创建String对象
7: dup
8: ldc #20; //String 1 --- 从常量池加载了1(此时常量池和堆中都会存字符串对象)
10: invokespecial #22; //Method java/lang/String."<init>":(Ljava/lang/String;)V --- 初始化String("1")对象
13: invokestatic #25; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
16: invokespecial #29; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V --- 初始化StringBuilder对象
19: new #18; //class java/lang/String
22: dup
23: ldc #20; //String 1
25: invokespecial #22; //Method java/lang/String."<init>":(Ljava/lang/String;)V
28: invokevirtual #30; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
31: invokevirtual #34; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
34: astore_1 ---从上可以看到实际上常量池目前只存了1
36: invokevirtual #38; //Method java/lang/String.intern:()Ljava/lang/String; --- 调用String.intern中,jdk1.7以后,常量池也是堆中的一部分且常量池可以存引用,这里直接存的是s2的引用
39: pop --- 这里直接返回的是栈顶的元素
*/
String s2 = new String("1") + new String("1");
s2.intern(); /**
* 0: ldc #16; //String abc --- 可以看到此时常量池直接存储的是:abc, 而不会a、b、c各存一份
2: astore_1
*/
String s3 = "a" + "b" + "c"; /**
0: new #16; //class java/lang/StringBuilder
3: dup
4: ldc #18; //String why --- 常量池的why
6: invokespecial #20; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
9: ldc #23; //String true --- 常量池的true
11: invokevirtual #25; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
14: invokevirtual #29; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
17: astore_1
*/
String s1 = new StringBuilder("why").append("true").toString();
System.out.println(s1 == s1.intern()); // jdk1.7之前为false,之后为true
java面试题之----String的intern

下面我们延伸一下来讲讲字符串拼接的优化问题:

java面试题之----String的intern
 1 String a = "1"; 
2 for (int i=0; i<10; i++) {
3   a += i;
4 }
0: ldc #16; //String 1
 2:   astore_1
3: iconst_0
4: istore_2                    --- 循环开始
5: goto 30
8: new #18; //class java/lang/StringBuilder --- 每个循环都建了一个StringBuilder对象,对性能有损耗
11: dup
12: aload_1
13: invokestatic #20; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
16: invokespecial #26; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
19: iload_2
20: invokevirtual #29; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
23: invokevirtual #33; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
26: astore_1
27: iinc 2, 1 ---- 计数加1
30: iload_2
31: bipush 10
33: if_icmplt 8 String a = "1";
for (int i=0; i<10; i++) {
a += "1";
}
的字节码为:
0: ldc #16; //String 1
2: astore_1
3: iconst_0
4: istore_2
5: goto 31
8: new #18; //class java/lang/StringBuilder ---还是会每次建立一个StringBuilder对象
11: dup
12: aload_1
13: invokestatic #20; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
16: invokespecial #26; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
19: ldc #16; //String 1 ---和上一个循环的区别也仅仅在于这里是从常量池加载1,
21: invokevirtual #29; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
24: invokevirtual #33; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
27: astore_1
28: iinc 2, 1
31: iload_2
32: bipush 10
34: if_icmplt 8

可知,真正的性能瓶颈在于每次循环都建了一个StringBuilder对象
所以我们优化一下 :
StringBuilder sb = new StringBuilder("1");
for (int i=0; i<10; i++) {
sb.append("1");
}
对应的字节码为:
0: new #16; //class java/lang/StringBuilder -- 在循环直接初始化了StringBuilder对象
3: dup
4: ldc #18; //String 1
6: invokespecial #20; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
9: astore_1
10: iconst_0
11: istore_2
12: goto 25
15: aload_1
16: ldc #18; //String 1
18: invokevirtual #23; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
21: pop
22: iinc 2, 1
25: iload_2
26: bipush 10
28: if_icmplt 15
java面试题之----String的intern

Where---String.intern的使用:

我们直接看一个例子来结束String.intern之旅吧:

java面试题之----String的intern
 Integer[] DB_DATA = new Integer[10];
Random random = new Random(10 * 10000);
for (int i = 0; i < DB_DATA.length; i++) {
DB_DATA[i] = random.nextInt();
}
long t = System.currentTimeMillis();
for (int i = 0; i < MAX; i++) {
arr[i] = new String(String.valueOf(DB_DATA[i % DB_DATA.length])); // --- 每次都要new一个对象
// arr[i] = new String(String.valueOf(DB_DATA[i % DB_DATA.length])).intern(); --- 其实虽然这么多字符串,但是类型最多为10个,大部分重复的字符串,大大减少内存
} System.out.println((System.currentTimeMillis() - t) + "ms");
System.gc();
java面试题之----String的intern

参考链接:

http://www.360doc.com/content/14/0721/16/1073512_396062351.shtml

https://www.cnblogs.com/SaraMoring/p/5713732.html