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

For boxing conversion javac uses Long.valueOf which does not guarantee caching according to its javadoc

    Details

    • Subcomponent:
    • Introduced In Version:
      9
    • Resolved In Build:
      b12
    • CPU:
      generic
    • Verification:
      Verified

      Description

      FULL PRODUCT VERSION :
      Since JDK 9.

      ADDITIONAL OS VERSION INFORMATION :
      Unrelated to OS.

      A DESCRIPTION OF THE PROBLEM :
      public class Temp {
      public static void main(String[] args) {
      final Long a = 121L;
      System.out.println(a);
      }
      }

      javap -v Temp (partial result)
               0: ldc2_w #2 // long 121l
               3: invokestatic #4 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;

      Compiling this code reveals that the class generated by javac uses Long.valueOf(121L).

      JLS 9 in 5.1.7. Boxing Conversion says (this part was changed in this version of JLS):
      "If the value p being boxed is the result of evaluating a constant expression (§15.28) of type boolean, char, short, int, or long, and the result is true, false, a character in the range '\u0000' to '\u007f' inclusive, or an integer in the range -128 to 127 inclusive, then let a and b be the results of any two boxing conversions of p. It is always the case that a == b."
      so the value 121L should always be cached.

      However, javadoc for Long.valueOf(long) says:
      " Note that unlike the corresponding method in the Integer class, this method is not required to cache values within a particular range."

      This is a bug for javac to use a method which does not guarantee caching where the value MUST be cached according to JLS.

      It is a bug either in javac or a wrong documentation of Long.valueOf(long)


      REGRESSION. Last worked in version 8u172

      ADDITIONAL REGRESSION INFORMATION:
      JLS was changed between version 8 and 9.
      Under JLS 8 it was not required to cache Longs so this was not a bug.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      As in the description.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      A method that guarantees caching is used for boxing conversion of longs.

      More practical solution: adjust javdoc of Long.valueOf(long) to say it guarantee caching.
      ACTUAL -
      As in the description.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class Temp {
      public static void main(String[] args) {
      final Long a = 121L;
      System.out.println(a);
      }
      }

      ---------- END SOURCE ----------

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                darcy Joe Darcy
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: