先贴一段代码
public static void main(String[] args){
String s1 = "abc";
String s2 = "a";
String s3 = "bc";
String s4 = "a" + "bc";
String s5 = "a" + s3;
String s6 = s2 + s3;
System.out.println(s1 == s4); //true
System.out.println(s1 == s5); //false
System.out.println(s1 == s6); //false
}
程序输出最后是true,false,false。之前一直不太懂为什么是这个输出结果,这几天在学字节码,于是查看了这段程序的字节码。
public class com.neo.test.Test {
public com.neo.test.Test();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: ldc #2 // String abc
2: astore_1
3: ldc #3 // String a
5: astore_2
6: ldc #4 // String bc
8: astore_3
9: ldc #2 // String abc
11: astore 4
13: new #5 // class java/lang/StringBuilder
16: dup
17: invokespecial #6 // Method java/lang/StringBuilder."<init>":()V
20: ldc #3 // String a
22: invokevirtual #7 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
25: aload_3
26: invokevirtual #7 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
29: invokevirtual #8 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
32: astore 5
34: new #5 // class java/lang/StringBuilder
37: dup
38: invokespecial #6 // Method java/lang/StringBuilder."<init>":()V
41: aload_2
42: invokevirtual #7 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
45: aload_3
46: invokevirtual #7 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
49: invokevirtual #8 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
52: astore 6
54: getstatic #9 // Field java/lang/System.out:Ljava/io/PrintStream;
57: aload_1
58: aload 4
60: if_acmpne 67
63: iconst_1
64: goto 68
67: iconst_0
68: invokevirtual #10 // Method java/io/PrintStream.println:(Z)V
71: getstatic #9 // Field java/lang/System.out:Ljava/io/PrintStream;
74: aload_1
75: aload 5
77: if_acmpne 84
80: iconst_1
81: goto 85
84: iconst_0
85: invokevirtual #10 // Method java/io/PrintStream.println:(Z)V
88: getstatic #9 // Field java/lang/System.out:Ljava/io/PrintStream;
91: aload_1
92: aload 6
94: if_acmpne 101
97: iconst_1
98: goto 102
101: iconst_0
102: invokevirtual #10 // Method java/io/PrintStream.println:(Z)V
105: return
}
从字节码中可以看出,字符串"+"其实是调用的StringBuilder的append方法,在每次执行"+"的时候都会先new一个StringBuilder对象来存储需要相加的字符串,最后通过toString()方法来转换成String。因此得到的String是一个新的对象。所以s1和s5,s6的地址是不同的。
在代码中s4 = "a" +"bc"对应的字节码是9: ldc #2 // String abc
,和第一句s1 = "abc"对应的字节码是一样的,在网上查找之后,在字符串常量相加时候,预编译时候会把"+"优化,直接把两个字符串常量自动合成一个字符串常量,因此s1和s4其实是常量池中同一个字符串"abc"的引用,因此返回结果是true。
这仅仅是学习记录。。。可能有表达出错或者我理解出错的地方,如果有大佬看到了本咸鱼的笔记emmmm欢迎指正~
网友评论