对于创建String对象的机制,在这一过程中涉及的东西还是值得探究一番的。
首先我们来了解一下intern()方法:
public native String intern()
这是源码给出的解释
/**
* Returns a canonical representation for the string object.
*
* A pool of strings, initially empty, is maintained privately by the
* class {@code String}.
*
* When the intern method is invoked, if the pool already contains a
* string equal to this {@code String} object as determined by
* the {@link #equals(Object)} method, then the string from the pool is
* returned. Otherwise, this {@code String} object is added to the
* pool and a reference to this {@code String} object is returned.
*
* It follows that for any two strings {@code s} and {@code t},
* {@code s.intern() == t.intern()} is {@code true}
* if and only if {@code s.equals(t)} is {@code 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.
*
* @return a string that has the same contents as this string, but is
* guaranteed to be from a pool of unique strings.
*/
第三段:
String类的intern()方法:一个初始为空的字符串池,它由类String独自维护。当调用 intern方法时,如果池已经包含一个等于此String对象的字符串(用equals(oject)方法确定),则返回池中的字符串。否则,将此String对象添加到池中,并返回此String对象的引用。 对于任意两个字符串s和t,当且仅当s.equals(t)为true时,s.intern() == t.intern()才为true。所有字面值字符串和字符串赋值常量表达式都使用 intern方法进行操作。
最后一段:
返回值:与此字符串具有相同内容的字符串,但是保证来自唯一字符串池。
我们可以归纳出以下理解:
在调用”ab”.intern()方法的时候会返回”ab”,但是这个方法会首先检查字符串池中是否有”ab”这个字符串,如果存在则返回这个字符串,否则就将这个字符串添加到字符串池中,然会返回这个字符串的引用。
例子
一、
/**
* 范例1
* @author HailongYao
*
*/
public static void main(String[] args) {
String s1 = "hello";
String s2 = "hello";
String s3 = "he" + "llo";
String s4 = "hel" + new String("lo");
String s5 = new String("hello");
String s6 = s5.intern();
String s7 = "h";
String s8 = "ello";
String s9 = s7 + s8;
System.out.println(s1==s2);//true
System.out.println(s1==s3);//true
System.out.println(s1==s4);//false
System.out.println(s1==s9);//false
System.out.println(s4==s5);//false
System.out.println(s1==s6);//true
}
在jdk1.8下运行的结果为:
true
true
false
false
false
true
String类的final修饰的,以字面量的形式创建String变量时,jvm会在编译期间就把该字面量(“hello”)放到字符串常量池中,由Java程序启动的时候就已经加载到内存中了。这个字符串常量池的特点就是有且只有一份相同的字面量,如果有其它相同的字面量,jvm则返回这个字面量的引用,如果没有相同的字面量,则在字符串常量池创建这个字面量并返回它的引用。由于s2指向的字面量“hello”在常量池中已经存在了(s1先于s2),于是jvm就返回这个字面量绑定的引用,所以s1 == s2。s3中字面量的拼接其实就是“hello”,jvm在编译期间就已经对它进行优化,所以s1和s3也是相等的。s4中的new String(“lo”)生成了两个对象,“lo”,“new String(“lo”)”,"lo"存在字符串常量池,"new String(“lo”)"存在堆中,String s4 = “hel” + new String(“lo”)实质上是两个对象的相加,编译器不会进行优化,相加的结果存在堆中,而s1存在字符串常量池中,当然不相等。s1 == s9的原理一样。s4 == s5两个相加的结果都在堆中,不用说,肯定不相等。s1 == s6中,s5.intern()方法能使一个位于堆中的字符串在运行期间动态地加入到字符串常量池中(字符串常量池的内容是程序启动的时候就已经加载好了),如果字符串常量池中有该对象对应的字面量,则返回该字面量在字符串常量池中的引用,否则,创建复制一份该字面量到字符串常量池并返回它的引用。因此s1==s6输出true。
二、
/**
* 范例2
* @author HailongYao
*
*/
public static void main(String[] args) {
String s1 = new String("hello");
String intern1 = s1.intern();
String s2 = "hello";
System.out.println(s1 == s2);
String s3 = new String("hello") + new String("hello");
String intern3 = s3.intern();
String s4 = "hellohello";
System.out.println(s3 == s4);
}
在jdk1.8下运行的结果为:
false
true
jdk1.8下字符串常量池已经转移到堆中了,是堆中的一部分内容,jvm设计人员对intern()进行了一些修改,当执行s3.intern()时,jvm不再把s3对应的字面量复制一份到字符串常量池中,而是在字符串常量池中存储一份s3的引用,这个引用指向堆中的字面量,当运行到String s4 = "hellohello"时,发现字符串常量池已经存在一个指向堆中该字面量的引用,则返回这个引用,而这个引用就是s3。所以s3 == s4输出true。
三、
/**
* 范例3
* @author HailongYao
*
*/
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = "hello";
String intern1 = s1.intern();
System.out.println(s1 == s2);
String s3 = new String("hello") + new String("hello");
String s4 = "hellohello";
String intern3 = s3.intern();
System.out.println(s3 == s4);
}
在jdk1.8下运行的结果为:
false
false
jdk1.8下输出false,false,s3指向堆内容,接着s4="hellohello"发现字符串常量池中没有该字面量,创建并返回该引用。s3,s4一个在堆,一个在字符串常量池,是两个独立的引用,所以s3==s4输出false。
四、(综合。并结合面试题:创建了几个对象的问题)
/**
* 范例5
* @author HailongYao
*
*/
public class Test {
public static void main(String[] args) {
String a = "a";//在字符串缓冲区中添加a的字面量
String b = "b";
String c = a+"c";
String d = a+b;
String ab = "a"+"b";
System.out.println(ab=="ab");//ab也就是"a"+"b",java已经自动拼接为了"ab",故等号两边引用相同
System.out.println(c=="ac");//c也就是a+"c",是一个对象;而"ac"是另外一个对象。故引用不同
System.out.println(System.identityHashCode(ab)+"\n"+
System.identityHashCode("ab"));//输出ab和"ab"的信息,发现信息相同
System.out.println("==========");
String e = "q"+"w"+"e"+"r";//创建了7个对象。
System.out.println(e=="qwer");
System.out.println("==========");
String s1 = "Monday"; //在字符串缓冲区中添加s1的字面量
String s2 = new String("Monday"); //只在栈中创建了新的对象,字符串缓冲区中并不添加s2的字面量
System.out.println(System.identityHashCode(s1)+"\n"+
System.identityHashCode(s2));//两者在栈中的引用不同
System.out.println(s1==s2); //false
System.out.println("==========");
s2 = s2.intern(); //返回s2对象在字符串缓存池(区)中的引用
System.out.println(System.identityHashCode(s1)+"\n"+
System.identityHashCode(s2));//比较的是s1在栈中的标识信息 和 s2在字符串缓冲区的标识信息
System.out.println(s1==s2); //true
System.out.println("==========");
String s = new String("test"); //创建了2个对象
System.out.println(System.identityHashCode(s)); //输出的是s对象在栈中的标识信息
System.out.println(System.identityHashCode(s.intern()));//输出s在字符串池中的标识信息
}
}
结果
true
false
366712642
366712642
==========
true
==========
1829164700
2018699554
false
==========
1311053135
118352462
以上所讲仅涉及字符串常量池,实际上还有整型常量池、浮点型常量池(java中基本类型的包装类的大部分都实现了常量池技术,即Byte,Short,Integer,Long,Character,Boolean;两种浮点数类型的包装类Float,Double并没有实现常量池技术) 等等,但都大同小异,只不过数值类型的常量池不可以手动添加常量,程序启动时常量池中的常量就已经确定了。