StringBuilderとStringの文字列接続の違いをバイトコードから解析

6186 ワード


テストのソースコードと対応するバイトコード
 
//StringEx.java
public class StringEx{
	public static void main(String[]args){
		String str="a";
		str+="b";
		str+="c";
		System.out.println(str);
	}
}
C:\Users\hello\Desktop\test>javap -c StringEx
Compiled from "StringEx.java"
public class StringEx extends java.lang.Object{
public StringEx();
  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 a
   2:   astore_1
   3:   new     #3; //class java/lang/StringBuilder
   6:   dup
   7:   invokespecial   #4; //Method java/lang/StringBuilder."<init>":()V
   10:  aload_1
   11:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   14:  ldc     #6; //String b
   16:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   19:  invokevirtual   #7; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
   22:  astore_1
   23:  new     #3; //class java/lang/StringBuilder
   26:  dup
   27:  invokespecial   #4; //Method java/lang/StringBuilder."<init>":()V
   30:  aload_1
   31:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   34:  ldc     #8; //String c
   36:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   39:  invokevirtual   #7; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
   42:  astore_1
   43:  getstatic       #9; //Field java/lang/System.out:Ljava/io/PrintStream;
   46:  aload_1
   47:  invokevirtual   #10; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   50:  return

}

 
/* , ,
** StringBuilder :str="a"+"b"+"c";
**/
public class StringEx{
	public static void main(String[]args){
		String str="a";
		str=new StringBuilder().append(str).append("b").toString();
		str=new StringBuilder().append(str).append("c").toString();
		System.out.println(str);
	}
}
 
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 
 //StringBuilderEx.java
public class StringBuilderEx{
	public static void main(String[]args){
		StringBuilder sb=new StringBuilder();
		sb.append("a");
		sb.append("b");
		sb.append("c");
		System.out.println(sb);
	}
}
C:\Users\hello\Desktop\test>javap -c StringBuilderEx
Compiled from "StringBuilderEx.java"
public class StringBuilderEx extends java.lang.Object{
public StringBuilderEx();
  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:   aload_1
   9:   ldc     #4; //String a
   11:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   14:  pop
   15:  aload_1
   16:  ldc     #6; //String b
   18:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   21:  pop
   22:  aload_1
   23:  ldc     #7; //String c
   25:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   28:  pop
   29:  getstatic       #8; //Field java/lang/System.out:Ljava/io/PrintStream;
   32:  aload_1
   33:  invokevirtual   #9; //Method java/io/PrintStream.println:(Ljava/lang/Object;)V
   36:  return

}

 
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 
//StringEx2.java
public class StringEx2{
	public static void main(String[]args){
		String str="a";
		String str2="b";
		str+=str2+"c"+"d";
	}
}
C:\Users\hello\Desktop\test>javap -c StringEx2
Compiled from "StringEx2.java"
public class StringEx2 extends java.lang.Object{
public StringEx2();
  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 a
   2:   astore_1
   3:   ldc     #3; //String b
   5:   astore_2
   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:  ldc     #7; //String c
   23:  invokevirtual   #6; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   26:  ldc     #8; //String d
   28:  invokevirtual   #6; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   31:  invokevirtual   #9; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
   34:  astore_1
   35:  return

}