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

int.class has type Class<Integer>

XMLWordPrintable

    • generic
    • generic

      The type of int.class is Class<Integer>.

      This basically destroys the type safety purported by type tokens:

      import java.util.*;

      class Test {
          public static void main(String[] args) {
              Collection<Integer> c = new Vector<Integer>();
              c = Collections.checkedCollection(c, int.class);
              c.add(0);
          }
      }

      This program fails:
      Exception in thread "main" java.lang.ClassCastException: Attempt to insert class java.lang.Integer element into collection with element type int
              at java.util.Collections$CheckedCollection.typeCheck(Collections.java:2206)
              at java.util.Collections$CheckedCollection.add(Collections.java:2240)
              at Test.main(Test.java:7)
      ###@###.### 10/20/04 06:31 GMT

            ahe Peter Ahe
            ahe Peter Ahe
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: