String是一个具有值类型和引用类型特点的特殊类型
常量池(constant pool)指的是在编译期被确定,并被保存在已编译的.class文件中的一些数据。它包括了关于类、方法、接口等中的常量,也包括字符串常量。
String s0 = "hello";
String s1 = "hello";
String s2 = "he" + "llo";
System.out.println(s0 == s1);// true
System.out.println(s0 == s2);// true
System.out.println(s0 == "hel" + "lo"); // true
首先,我们要知道Java会确保一个字符串常量只有一个拷贝。
因为例子中的s0和s1中的”hello”都是字符串常量,它们在编译期就被确定了,所以s0==s1为true;而”he”和”llo”也都是字符串常量,当一个字符串由多个字符串常量连接而成时,它自己肯定也是字符串常量,所以s2也同样在编译期就被解析为一个字符串常量,所以s2也是常量池中”hello”的一个引用。所以我们得出s0==s1==s2;
用new String() 创建的字符串不是常量,不能在编译期就确定,所以new String() 创建的字符串不放入常量池中,它们有自己的地址空间。
看例2:
String s4 = "abc";
String s5 = new String("abc");
String s6 = "ab"+ new String("c");
System.out.println(s3 == s4);//false
System.out.println(s3 == s5);//false
System.out.println(s4 == s6);//false
System.out.println(s4 == s5);//false
例2中在第一行,会产生两个对象,先在常量区找看有没有字符串常量“abc”,如果没有,则在常量区开辟一块内存,存放字符串常量“abc”,再在堆(heap)区存创建新的“abc”对象(字符串常量“abc”的copy),s3(在栈()区)新对象“hello”的引用.
第二行,先会在常量池(pool)中查找有没有常量“hello”,如果有,则指向字符串常量“abc”,如果没有,则在常量池中创建字符串常量“abc”,s4是字符串常量“abc”的引用,所以第二句没有创建对象.
第三行代码,会产生一个对象,和第一句一样,字符串常量“abc”的copy,s5(在栈()区)新对象“hello”的引用.
s3因为无法在编译期确定,所以是运行时创建的新对象”abc”的引用,s6因为有后半部分new String(“c”)所以也无法在编译期确定,所以也是一个新创建对象”abc”的应用;明白了这些也就知道为何得出此结果了。
4. String.intern():
再补充介绍一点:存在于.class文件中的常量池,在运行期被JVM装载,并且可以扩充。 String的intern()方法就是扩充常量池的一个方法;当一个String实例str调用intern()方法时,Java查找常量池中是否有相同Unicode的字符串常量,如果有,则返回其的引用,如果没有,则在常量池中增加一个Unicode等于str的字符串并返回它的引用;看例3就清楚了
例3:
String s3 = new String("abc");
String s4 = "abc";
String s5 = new String("abc");
//String的intern()返回的是常量池中"abc”的引用
System.out.println(s3 == s3.intern());//false
System.out.println(s4 == s5.intern());//true
System.out.println(s3.intern() == s5.intern());//true
System.out.println(s4.intern()==s4);//true
全部代码:
public class StringTest {
public static void main(String[] args) {
String s0 = "hello";
String s1 = "hello";
String s2 = "he" + "llo";
System.out.println(s0 == s1);// true
System.out.println(s0 == s2);// true
System.out.println(s0 == "hel" + "lo");// true
/**
* 分配一个新的 String,它表示当前字符数组参数中包含的字符序列。
* 该字符数组的内容已被复制;后续对字符数组的修改不会影响新创建的字符串。 参数:value - 此字符串的初始值。
*/
String s3 = new String("abc");
String s4 = "abc";
String s5 = new String("abc");
String s6 = "ab" + new String("b");
System.out.println(s3 == s4);// false
System.out.println(s3 == s5);// false
System.out.println(s4 == s6);// false
System.out.println(s4 == s5);// false
/**
* new String().intern() 返回字符串对象的规范化表示形式。 一个初始时为空的字符串池,它由类 String 私有地维护。
* 当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串 (该对象由 equals(Object)
* 方法确定),则返回池中的字符串。否则, 将此 String 对象添加到池中,并且返回此 String 对象的引用。 它遵循对于任何
* 两个字符串 s 和 t,当且仅当 s.equals(t) 为 true 时, s.intern() == t.intern() 才为
* true。 返回:一个字符串,内容与此字符串相同,但它保证来自字符串池中。
*/
System.out.println("******************************");
// String的intern()返回的是常量池中'abc'的引用
System.out.println(s3 == s3.intern());// false
System.out.println(s4 == s5.intern());// true
System.out.println(s3.intern() == s5.intern());// true
System.out.println(s4.intern() == s4);// true
System.out.println("******************");
String hello = "hello";
String hel = "hel";
String lo = "lo";
System.out.println(hello == "hel" + "lo");// true
System.out.println(hello == "hel" + lo);// false
}
}