Why following types are reifiable& non-reifiable in java?

Prateek picture Prateek · Sep 17, 2013 · Viewed 9.2k times · Source

In computing, reification has come to mean an explicit representation of a type—that is, run-time type information.

oracle tutorials says ,

A reifiable type is a type whose type information is fully available at runtime. This includes primitives, non-generic types, raw types, and invocations of unbound wildcards.

Non-reifiable types are types where information has been removed at compile-time by type erasure — invocations of generic types that are not defined as unbounded wildcards.

A type is reifiable if it is one of the following:

  1. A primitive type (such as int) //understood
  2. A nonparameterized class or interface type (such as Number, String, or Runnable) // why
  3. A parameterized type in which all type arguments are unbounded wildcards (such as List<?>, ArrayList<?>, or Map<?, ?>) // why
  4. A raw type (such as List, ArrayList, or Map) // why
  5. An array whose component type is reifiable(such as int[], Number[], List<?>[], List[], or int[][]) // why

A type is not reifiable if it is one of the following:

  1. A type variable(such as T) // why
  2. A parameterized type with actual parameters (such as List<Number>, ArrayList<String>, or Map<String, Integer>) // why
  3. A parameterized type with a bound (such as List<? extends Number> or Comparable<? super String>) // why

Why 2,3,4,5 is reifiable and 6,7,8 as non-reifiable?

Answer

LBC picture LBC · Mar 15, 2017

Sun/Oracle says the reason is combo of:

  • Need: compile time type checking is sufficient
  • Code size: avoid STL-like code bloat
  • Performance: avoid type checking at runtime that was already done at compile

Type erasure ensures that no new classes are created for parameterized types; consequently, generics incur no runtime overhead.

In short, 1-5 are reifiable because they simply remain the same types as specified in the code so there is no type information lost/erased, but 6-8 will lose type information during compilation (the stuff between the <>) so can't be accessed at runtime.