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

5.2: Allow widening before unboxing

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 9
    • 8
    • specification

      The rules for unboxing conversions in assignments (5.2) and various other contexts (invocation, 5.3; casting, 5.5; numeric, 5.6) assert that an unboxing may be followed by a widening primitive conversion, but do not allow a widening reference conversion before the unboxing occurs.

      Despite these rules, longstanding behavior of javac is to allow, e.g., type variables to be widened to a boxed primitive type.

      <T extends Integer> void test(T arg) {
        int i = arg;
        int i2 = (int) arg;
        double d = arg + 1.0;
        Double d2 = Double.valueOf(arg);
        double d3 = (double) arg;
      }

      This behavior is especially important for interoperability with capture: various existing programs expect to be able to treat the elements of a 'List<? extends Integer>' as if they were ints.

      List<? extends Integer> li = null;
      int i = li.get(0);

      Propose to change JLS in these sections to allow a widening reference conversion before unboxing.

            abuckley Alex Buckley
            dlsmith Dan Smith
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: