First Blood
先看下面的代码:
String s = new String("1");
String s1 = s.intern();
System.out.println(s == s1);
打印结果为:
false
字符串常量池中。
如下图:
s == s1结果为false。
Double kill
在上面的基础上我们再定义一个s2如下:
String s = new String("1");
String s1 = s.intern();
String s2 = "1";
System.out.println(s == s1);
System.out.println(s1 == s2); // true
s1 == s2为true,表示变量s2是直接指向的字符串常量,如下图:
Triple kill
在上面的基础上我们再定义一个t如下:
String s = new String("1");
String t = new String("1");
String s1 = s.intern();
String s2 = "1";
System.out.println(s == s1);
System.out.println(s1 == s2);
System.out.println(s == t); // false
System.out.println(s.intern() == t.intern()); // true
s == t为false,这个很明显,变量s和变量t指向的是不同的两个String类型的对象。
Ultra kill
在上面的基础上我们再定义一个x和s3如下:
String s = new String("1");
String t = new String("1");
String x = new String("1") + new String("1");
String s1 = s.intern();
String s2 = "1";
String s3 = "11";
System.out.println(s == s1);
System.out.println(s1 == s2);
System.out.println(s == t);
System.out.println(s.intern() == t.intern());
System.out.println(x == s3); // fasle
System.out.println(x.intern() == s3.intern()); // true
x.intern() == s3.intern()为true。
Rampage
将上面的代码简化并添加几个变量如下:
String x = new String("1") + new String("1");
String x1 = new String("1") + "1";
String x2 = "1" + "1";
String s3 = "11";
System.out.println(x == s3); // false
System.out.println(x1 == s3); // false
System.out.println(x2 == s3); // true
x == s3为false表示x指向String类型对象,s3指向字符串常量;
x1 == s3为false表示x1指向String类型对象,s3指向字符串常量;
x2 == s3为true表示x2指向字符串常量,s3指向字符串常量;
返回的String类型的对象。
总结
现在我们知道intern方法就是将字符串保存到常量池中,在保存字符串到常量池的过程中会先查看常量池中是否已经存在相等的字符串,如果存在则直接使用该字符串。
更节省内存。我们也可以使用String s = 一个String类型的对象.intern();方法来间接的使用字符串常量,这种做法通常用在你接收到一个String类型的对象而又想节省内存的情况下,当然你完全可以String s = 一个String类型的对象;但是这么用可能会因为变量s的引用而影响String类型对象的垃圾回收。所以我们可以使用intern方法进行优化,但是需要注意的是intern能节省内存,但是会影响运行速度,因为该方法需要去常量池中查询是否存在某字符串。
https://tech.meituan.com/2014/03/06/in-depth-understanding-string-intern.html