先看下面2个程序
public static void main(String[] args) {
String a = "a1";
String b = "a" + 1;
System.out.println(a == b);
}
public static void main(String[] args) {
String a = "a1";
String b = "1";
String c = "a" + b;
System.out.println(a == c);
}
第一个程序输出是true,由于“a” 和1算是字符串常量,所以
在编译期b的值就确定了,在运行期不会产生StringBuilder对象,所以在运行期,由于“a1“已经在String Pool中存在, 所以对象”a1“的引用同时指向a和b。
第二个程序中输出是false。因为在运行期才能确定“a”+b的值,所以为了提高效率,在运行期会产生一个StringBuilder对象,对它调用append方法,最后调用toString()方法,返回一个String对象的引用。
下面开始说几个面试题:
1.
public static void main(String[] args) {
//打印true,原因见上面分析
String a = "a1";
String b = "a" + 1;
System.out.println(a == b);
}
2
public static void main(String[] args) {
//打印false,原因见上面分析
String a = "ab";
String bb = "b";
String b = "a" + bb;
System.out.println(a == b);
}
3.
public static void main(String[] args) {
//打印true,final说明bb始终指向”b“,不能把其他对象的引用给bb,所以,在编译期,bb的值是确定的,即
//"a" + bb的值也是确定的,所以和上面1同理
String a = "ab";
final String bb = "b";
String b = "a" + bb;
System.out.println(a == b);
}
4
public static void main(String[] args) {
//打印false, 简言之,就是编译期不能确定,在运行期才能确定,因此会产生StringBulder对象,通过toSring()返回一个String
//的引用,肯定他a和b的内存地址是不同的。
String a = "ab";
final String bb = getBB();
String b = "a" + bb;
System.out.println(a == b);
}
private static String getBB() {
return "b";
}
5
public class Test {
private static String a = "ab";
public static void main(String[] args) {
//打印false,true, static数据放在方法区中,其他和不是static的变脸一样
String s1 = "a";
String s2 = "b";
String s = s1 + s2;
System.out.println(s == a);
System.out.println(s.intern() == a);
}
}
分析图如下:
6
public class Test {
private static String a = new String("ab");
public static void main(String[] args) {
//打印false,false,true。分析看后面的分析图
String s1 = "a";
String s2 = "b";
String s = s1 + s2;
System.out.println(s == a);
System.out.println(s.intern() == a);
System.out.println(s.intern() == a.intern());
}
}