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

Javac compile error on cast to primitive when using generics wirldcards

XMLWordPrintable

    • generic
    • generic

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      The following program does not compile using javac (but does compile using the eclipse compiler).

        public static void main(String... s) {
          List<?> l = Arrays.asList(1, 2, 3, 4);
          if (l.get(0) instanceof Integer) {
            System.out.println((int) l.get(0));
          }
        }

      incompatible types: capture#1 of ? cannot be converted to int


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
        public static void main(String... s) {
          List<?> l = Arrays.asList(1, 2, 3, 4);
          if (l.get(0) instanceof Integer) {
            System.out.println((int) l.get(0));
          }
        }

      -> incompatible types: capture#1 of ? cannot be converted to int
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
        public static void main(String... s) {
          List<?> l = Arrays.asList(1, 2, 3, 4);
          if (l.get(0) instanceof Integer) {
            System.out.println((Integer) l.get(0));
          }
        }

      -> OK ! Just changing from primitive to object

            dlsmith Dan Smith
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: