java的String是不可变类。为了提高效率,java为String类提供了String池。
当我们使用形如String s="abc"的代码为字符串赋值时,JVM首先会检查字符串常量池中是否有"abc"这个字符串,如果有就直接将其地址赋给s;若没有,则在Stirng池中创建一个字符串对象“abc”,再将其地址赋给s。
这里,s是对象“abc”的引用,可以将引用理解为对象的地址。
例如:
public static void main(String[] args) {
String s1="hello";
String s2="hello";
String s3="he"+"llo";
System.out.println(s1==s2);
System.out.println(s2==s3);
}
上述代码中,执行String s1="hello"时JVM在String池中创建了“hello”对象;当执行到String s2="hello"时,因为此时String池中已经有了“hello”,编译器就直接将“hello”的地址赋给s2。因此s1、s2指向了同一个对象,所以输出为true。S3也是同样道理。
注意:当使用String s=new String("");这种形式赋值时,实际上产生了两个对象。
例如:String s=new String("abc"),此时先是在String常量池中产生了一个“abc”对象,然后new String()又在堆中分配了内存空间,将常量区中的“abc"复制一份给了堆中的String对象。因此这段代码产生了两个对象,一个在常量区、一个在堆区。
例如:
public static void main(String[] args) {
String s1="hello";
String s2=new String("hello");
System.out.println(s1==s2);
}
这里s1指向的是常量区中的”hello“,s2指向的是堆中的”hello“,因此输出为false。
注:对于非基本数据类型,“==”比较的是对象的内存地址是否相同,虽然s1、s2的值都是“hello”,但是地址不同,所以s1==s2为false。