Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6326080

javac fails to recognize common idiom for converting to string

XMLWordPrintable

    • 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

            wtaosunw Wei Tao (Inactive)
            ahe Peter Ahe
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: