3、有下面程序:
public class TestString
{
public static void main(String[] args)
{
String str1 = “abcd”;
String str2 = “abcd”;
String str3 = new String(“abcd”);
String str4 = new String(“abcd”);
System.out.println(str1==str2);
System.out.println(str3==str4);
}
}
输出结果是?
A true true
B true false
C false true
D false false
答案是B 请问一下 str1和str3的声明区别在哪里?
为什么用str1的声明方式 比较的是字符串
而用str3的生命方式比较的是字符串引用。
public class TestString
{
public static void main(String[] args)
{
String str1 = “abcd”;
String str2 = “abcd”;
String str3 = new String(“abcd”);
String str4 = new String(“abcd”);
System.out.println(str1==str2);
System.out.println(str3==str4);
}
}
输出结果是?
A true true
B true false
C false true
D false false
答案是B 请问一下 str1和str3的声明区别在哪里?
为什么用str1的声明方式 比较的是字符串
而用str3的生命方式比较的是字符串引用。