No object is created at runtime.
The variable will be set to reference one object from the "constant pool".
Example
For this code:
public class MyClass {{
String result = "a" + "b" + "c";
}}
Compiled using Oracle Java build 1.7.0_04-b20 you get this bytecode:
Constant pool:
#1 = Class #2 // MyClass
#2 = Utf8 MyClass
#3 = Class #4 // java/lang/Object
#4 = Utf8 java/lang/Object
#5 = Utf8 <init>
#6 = Utf8 ()V
#7 = Utf8 Code
#8 = Methodref #3.#9 // java/lang/Object."<init>":()V
#9 = NameAndType #5:#6 // "<init>":()V
#10 = String #11 // abc
#11 = Utf8 abc
#12 = Utf8 LineNumberTable
#13 = Utf8 LocalVariableTable
#14 = Utf8 this
#15 = Utf8 LMyClass;
#16 = Utf8 SourceFile
#17 = Utf8 MyClass.java
{
public MyClass();
flags: ACC_PUBLIC
Code:
stack=1, locals=2, args_size=1
0: aload_0
1: invokespecial #8 // Method java/lang/Object."<init>":()V
4: ldc #10 // String abc
6: astore_1
7: return
LineNumberTable:
line 2: 0
line 3: 4
line 2: 7
LocalVariableTable:
Start Length Slot Name Signature
0 8 0 this LMyClass;
}
However, concatenating three String variables like this:
public class MyClass {{
String a = "a";
String b = "b";
String c = "c";
String result = a + b + c;
}}
and the compiler will instead use a StringBuilder like this:
0: aload_0
1: invokespecial #8 // Method java/lang/Object."<init>":()V
4: ldc #10 // String a
6: astore_1
7: ldc #12 // String b
9: astore_2
10: ldc #14 // String c
12: astore_3
13: new #16 // class java/lang/StringBuilder
16: dup
17: aload_1
18: invokestatic #18 // Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
21: invokespecial #24 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
24: aload_2
25: invokevirtual #27 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
28: aload_3
29: invokevirtual #27 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
32: invokevirtual #31 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
35: astore 4
37: return
The variable will be set to reference one object from the "constant pool".
Example
For this code:
public class MyClass {{
String result = "a" + "b" + "c";
}}
Compiled using Oracle Java build 1.7.0_04-b20 you get this bytecode:
Constant pool:
#1 = Class #2 // MyClass
#2 = Utf8 MyClass
#3 = Class #4 // java/lang/Object
#4 = Utf8 java/lang/Object
#5 = Utf8 <init>
#6 = Utf8 ()V
#7 = Utf8 Code
#8 = Methodref #3.#9 // java/lang/Object."<init>":()V
#9 = NameAndType #5:#6 // "<init>":()V
#10 = String #11 // abc
#11 = Utf8 abc
#12 = Utf8 LineNumberTable
#13 = Utf8 LocalVariableTable
#14 = Utf8 this
#15 = Utf8 LMyClass;
#16 = Utf8 SourceFile
#17 = Utf8 MyClass.java
{
public MyClass();
flags: ACC_PUBLIC
Code:
stack=1, locals=2, args_size=1
0: aload_0
1: invokespecial #8 // Method java/lang/Object."<init>":()V
4: ldc #10 // String abc
6: astore_1
7: return
LineNumberTable:
line 2: 0
line 3: 4
line 2: 7
LocalVariableTable:
Start Length Slot Name Signature
0 8 0 this LMyClass;
}
However, concatenating three String variables like this:
public class MyClass {{
String a = "a";
String b = "b";
String c = "c";
String result = a + b + c;
}}
and the compiler will instead use a StringBuilder like this:
0: aload_0
1: invokespecial #8 // Method java/lang/Object."<init>":()V
4: ldc #10 // String a
6: astore_1
7: ldc #12 // String b
9: astore_2
10: ldc #14 // String c
12: astore_3
13: new #16 // class java/lang/StringBuilder
16: dup
17: aload_1
18: invokestatic #18 // Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
21: invokespecial #24 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
24: aload_2
25: invokevirtual #27 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
28: aload_3
29: invokevirtual #27 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
32: invokevirtual #31 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
35: astore 4
37: return
No comments:
Post a Comment