Created
August 25, 2015 04:27
-
-
Save aows/6a7a147e52e30b0765b9 to your computer and use it in GitHub Desktop.
String vs StringBuilder example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test1 { | |
public Test1(); | |
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 | |
2: astore_1 | |
3: iconst_0 | |
4: istore_2 | |
5: iload_2 | |
6: bipush 10 | |
8: if_icmpge 36 | |
11: new #3 // class java/lang/StringBuilder | |
14: dup | |
15: invokespecial #4 // Method java/lang/StringBuilder."<init>":()V | |
18: aload_1 | |
19: invokevirtual #5 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; | |
22: iload_2 | |
23: invokevirtual #6 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder; | |
26: invokevirtual #7 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; | |
29: astore_1 | |
30: iinc 2, 1 | |
33: goto 5 | |
36: getstatic #8 // Field java/lang/System.out:Ljava/io/PrintStream; | |
39: aload_1 | |
40: invokevirtual #9 // Method java/io/PrintStream.println:(Ljava/lang/String;)V | |
43: return | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test1 { | |
public static void main(String[] args) { | |
String aString = ""; | |
for (int i=0; i<10; i++) { | |
aString += i; | |
} | |
System.out.println(aString); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test2 { | |
public Test2(); | |
Code: | |
0: aload_0 | |
1: invokespecial #1 // Method java/lang/Object."<init>":()V | |
4: return | |
public static void main(java.lang.String[]); | |
Code: | |
0: new #2 // class java/lang/StringBuilder | |
3: dup | |
4: invokespecial #3 // Method java/lang/StringBuilder."<init>":()V | |
7: astore_1 | |
8: iconst_0 | |
9: istore_2 | |
10: iload_2 | |
11: bipush 10 | |
13: if_icmpge 28 | |
16: aload_1 | |
17: iload_2 | |
18: invokevirtual #4 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder; | |
21: pop | |
22: iinc 2, 1 | |
25: goto 10 | |
28: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream; | |
31: aload_1 | |
32: invokevirtual #6 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; | |
35: invokevirtual #7 // Method java/io/PrintStream.println:(Ljava/lang/String;)V | |
38: return | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.StringBuilder; | |
public class Test2 { | |
public static void main(String[] args) { | |
StringBuilder aString = new StringBuilder(); | |
for (int i=0; i<10; i++) { | |
aString.append(i); | |
} | |
System.out.println(aString.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment