Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 251
Prateek Avatar asked Sep 17 '13 11:09

Prateek


People also ask

What is a Reifiable type?

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.

How does type erasure work?

Type erasure is a process in which compiler replaces a generic parameter with actual class or bridge method. In type erasure, compiler ensures that no extra classes are created and there is no runtime overhead.


1 Answers

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.

like image 72
LBC Avatar answered Sep 23 '22 21:09

LBC