-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
5.0
-
generic
-
generic
public class Test {
private Test() {}
public static void main(String... args) {
int i = 1;
String s = ""+i;
}
}
$ /usr/java/jdk1.6.0/bin/javac -J-showversion Test.java
java version "1.6.0-ea"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-ea-b52)
Java HotSpot(TM) Client VM (build 1.6.0-ea-b52, mixed mode, sharing)
$ javap -c Test
Compiled from "Test.java"
public class Test extends java.lang.Object{
public static void main(java.lang.String[]);
Code:
0: iconst_1
1: istore_1
2: new #2; //class java/lang/StringBuilder
5: dup
6: invokespecial #3; //Method java/lang/StringBuilder."<init>":()V
9: ldc #4; //String
11: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
14: iload_1
15: invokevirtual #6; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
18: invokevirtual #7; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
21: astore_2
22: return
}
The compiler should be able to generate this code:
Code:
0: iconst_1
1: istore_1
2: iload_1
3: invokestatic #2; //Method java/lang/String.valueOf:(I)Ljava/lang/String;
6: astore_2
7: return
private Test() {}
public static void main(String... args) {
int i = 1;
String s = ""+i;
}
}
$ /usr/java/jdk1.6.0/bin/javac -J-showversion Test.java
java version "1.6.0-ea"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-ea-b52)
Java HotSpot(TM) Client VM (build 1.6.0-ea-b52, mixed mode, sharing)
$ javap -c Test
Compiled from "Test.java"
public class Test extends java.lang.Object{
public static void main(java.lang.String[]);
Code:
0: iconst_1
1: istore_1
2: new #2; //class java/lang/StringBuilder
5: dup
6: invokespecial #3; //Method java/lang/StringBuilder."<init>":()V
9: ldc #4; //String
11: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
14: iload_1
15: invokevirtual #6; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
18: invokevirtual #7; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
21: astore_2
22: return
}
The compiler should be able to generate this code:
Code:
0: iconst_1
1: istore_1
2: iload_1
3: invokestatic #2; //Method java/lang/String.valueOf:(I)Ljava/lang/String;
6: astore_2
7: return
- duplicates
-
JDK-4059189 Compiler generated code for String.operator+() could avoid allocations.
- Closed