Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

generics - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...