如下的输出值都是什么
static void t1() {
String str2 = new String("str01");
str2.intern();
String str1 = "str01";
System.out.println(str2==str1);
}
static void t2() {
String str2 = new String("str01");
String str3 = str2.intern();
String str1 = "str01";
System.out.println(str2==str1);
System.out.println(str3==str1);
}
static void t4() {
String str2 = new String("str") + new String("01");
String str1 = "str01";
String str3 = str2.intern();
System.out.println(str2==str1);
System.out.println(str3==str1);
}
static void t3() {
String str2 = new String("str")+new String("01");
// String str3 = str2.intern();
str2.intern();
String str1 = "str01";
System.out.println(str2==str1);
// System.out.println(str3==str1);
}
static void t5() {
String s1 = "Programming";
String s3 = "Program";
String s4 = "ming";
String s6 = s3 + s4;
System.out.println(s1 == s6);
}
static void t6() {
String s1 = "Programming";
final String s3 = "Program";
final String s4 = "ming";
String s6 = s3 + s4;
System.out.println(s1 == s6);
}
String s1 = "Programming";
String s3 = "Program";
String s4 = "ming";
String s6 = s3 + s4;
System.out.println(s1 == s6);
通过 javap -c
进行反编译发现底层通过调用 StringBuilder.append 进行拼接后调用 toString 方法,所以 s6 对象是在生成在【堆】中,所以返回 false
static void t6() {
String s1 = "Programming";
final String s3 = "Program";
final String s4 = "ming";
String s6 = s3 + s4;
System.out.println(s1 == s6);
}
通过 javap -c
进行反编译发现底层通过将变量 s3,s4
替换成了对应字符串,所以 s6 对象也是在常量池中
网友评论