StringBuilder的append方法和String+String的区别
2016-03-02 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517183
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
javac会把 str += "b"; 变成一个StringBuilder的append 操作。
相当于一个 new StringBuilder().append("a").append("b").toString().
比自己调用append费事多了。
Java code
StringBuilder builder = new StringBuilder("a");
String s = builder.append("b").append("c").append("d").toString();
String a = "a";
a += "b";
a += "c";
a += "d";
javap -c 的输出:
Java code
Code:
0: new #2 // class java/lang/StringBuilder
3: dup
4: ldc #3 // String a
6: invokespecial #4 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
9: astore_1
10: aload_1
11: ldc #5 // String b
13: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
16: ldc #7 // String c
18: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
21: ldc #8 // String d
23: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
26: invokevirtual #9 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
29: astore_2
30: ldc #3 // String a
32: astore_3
33: new #2 // class java/lang/StringBuilder
36: dup
37: invokespecial #10 // Method java/lang/StringBuilder."<init>":()V
40: aload_3
41: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
44: ldc #5 // String b
46: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
49: invokevirtual #9 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
52: astore_3
53: new #2 // class java/lang/StringBuilder
56: dup
57: invokespecial #10 // Method java/lang/StringBuilder."<init>":()V
60: aload_3
61: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
64: ldc #7 // String c
66: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
69: invokevirtual #9 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
72: astore_3
73: new #2 // class java/lang/StringBuilder
76: dup
77: invokespecial #10 // Method java/lang/StringBuilder."<init>":()V
80: aload_3
81: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
84: ldc #8 // String d
86: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
89: invokevirtual #9 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
92: astore_3
93: return
相当于一个 new StringBuilder().append("a").append("b").toString().
比自己调用append费事多了。
Java code
StringBuilder builder = new StringBuilder("a");
String s = builder.append("b").append("c").append("d").toString();
String a = "a";
a += "b";
a += "c";
a += "d";
javap -c 的输出:
Java code
Code:
0: new #2 // class java/lang/StringBuilder
3: dup
4: ldc #3 // String a
6: invokespecial #4 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
9: astore_1
10: aload_1
11: ldc #5 // String b
13: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
16: ldc #7 // String c
18: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
21: ldc #8 // String d
23: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
26: invokevirtual #9 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
29: astore_2
30: ldc #3 // String a
32: astore_3
33: new #2 // class java/lang/StringBuilder
36: dup
37: invokespecial #10 // Method java/lang/StringBuilder."<init>":()V
40: aload_3
41: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
44: ldc #5 // String b
46: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
49: invokevirtual #9 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
52: astore_3
53: new #2 // class java/lang/StringBuilder
56: dup
57: invokespecial #10 // Method java/lang/StringBuilder."<init>":()V
60: aload_3
61: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
64: ldc #7 // String c
66: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
69: invokevirtual #9 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
72: astore_3
73: new #2 // class java/lang/StringBuilder
76: dup
77: invokespecial #10 // Method java/lang/StringBuilder."<init>":()V
80: aload_3
81: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
84: ldc #8 // String d
86: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
89: invokevirtual #9 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
92: astore_3
93: return
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询