Last active
August 29, 2015 14:25
-
-
Save bwmcadams/5d54f269a92b03a55bce to your computer and use it in GitHub Desktop.
Java String Concatenation Performance
This file contains hidden or 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
// decompiled with straight javap -c | |
//Compiled from "TestStrings.java" in JAVA 1.6.... same in newer versions. | |
public class TestStrings extends java.lang.Object{ | |
public TestStrings(); | |
Code: | |
0: aload_0 | |
1: invokespecial #1; //Method java/lang/Object."<init>":()V | |
4: return | |
public static void main(java.lang.String[]); | |
Code: | |
// Note that "Foo " + "bar" was replaced (by compiler) with a static string of "Foo bar" | |
0: ldc #2; //String Foo bar | |
2: astore_1 | |
// Note that "OMG" + "WTF" + "BBQ" was replaced (by compiler) with a static string of "OMGWTFBBQ" | |
3: ldc #3; //String OMGWTFBBQ | |
5: astore_2 | |
// String variable 'foo' + String variable 'omg' is compiler substituted with a StringBuilder construction... | |
6: new #4; //class java/lang/StringBuilder | |
9: dup | |
10: invokespecial #5; //Method java/lang/StringBuilder."<init>":()V | |
13: aload_1 | |
14: invokevirtual #6; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; | |
17: aload_2 | |
18: invokevirtual #6; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; | |
21: invokevirtual #7; //Method java/lang/StringBuilder.toString:()Ljava/lang/String; | |
24: astore_3 | |
25: return | |
} |
This file contains hidden or 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 TestStrings { | |
public static void main(String[] args) { | |
String foo = "Foo " + "bar"; | |
String omg = "OMG" + "WTF" + "BBQ"; | |
String test = foo + omg; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment