Java中字符串String加法解析
Java字符串String变量加法解析
测试代码:
public static void main(String[] args) {
String a = "hello2";
final String b = "hello";
String d = "hello";
String c = b + 2;
// 由于b是常量所以编译期,编译器会直接将b替换为其值,并直接拼接成"Hello2"赋值给c,这又叫做“常量优化”
// 故在编译期就能确定b值
String e = d + 2;
// 由于赋值语句右侧使用的不同类型常量赋值,因此会使用StringBuilder实现字符串拼接
// 会先调用其append方法最后调用toString方法,而toString方法是new String并返回,即在堆中创建对象
// 故e的值需要运行时确定
String f = "hello" + "2";
// 而f的赋值语句右侧也都是字面量,或者说常量,同字符串c的解释,故在编译期就能确定f值
// 故最终的输出结果为:
System.out.println(a == c);// true
System.out.println(a == e);// false
System.out.println(a == f);// true
}
反汇编class文件输出:
C:\Users\DELL\Desktop>javap -c Solution.class
Compiled from "Solution.java"
public class LeetCodeStudy.Solution {
public LeetCodeStudy.Solution();
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 hello2
2: astore_1
3: ldc #3 // String hello
5: astore_2
6: ldc #3 // String hello
8: astore_3
9: ldc #2 // String hello2
11: astore 4
13: new #4 // class java/lang/StringBuilder
16: dup
17: invokespecial #5 // Method java/lang/StringBuilder."<init>":()V
20: aload_3
21: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
24: iconst_2
25: invokevirtual #7 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
28: invokevirtual #8 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
31: astore 5
33: ldc #2 // String hello2
35: astore 6
37: getstatic #9 // Field java/lang/System.out:Ljava/io/PrintStream;
40: aload_1
41: aload 4
43: if_acmpne 50
46: iconst_1
47: goto 51
50: iconst_0
51: invokevirtual #10 // Method java/io/PrintStream.println:(Z)V
54: getstatic #9 // Field java/lang/System.out:Ljava/io/PrintStream;
57: aload_1
58: aload 5
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 6
77: if_acmpne 84
80: iconst_1
81: goto 85
84: iconst_0
85: invokevirtual #10 // Method java/io/PrintStream.println:(Z)V
88: return
}
参考文档:
String 相加解析原理
String字符串相加的问题
java 字符串拼接为什么要用 StringBuilder 而不直接用 String 相加连接