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
133 views
in Technique[技术] by (71.8m points)

java - Definition of type variable and parameter

I am reading up on generics in Java using the Java Language Specification, Third edition. In section "4.6 Erasure" type erasure is defined. On the erasure of a type variable it says

The erasure of a type variable (§4.4) is the erasure of its leftmost bound.

This confuses me a bit regarding the distinction between type variable and type parameter since section "4.4 Type Variables" has the definition: TypeParameter: TypeVariable TypeBound where the bound is optional. But perhaps you can identify a type variable with the type parameter it appears in since a type variable can only (?) appear in one "context" and then a type variable's leftmost bound is defined as the leftmost bound of its corresponding type parameter or Object in case there is no explicit bound appearing in the type parameter ?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

If no bound is given for a type variable, Object is assumed.

Found in your link. That means that given FirstClass<T extends String> and SecondClass<V> you get that:

  1. Class: FirstClass Type Parameter: T extends String. Type Variable: T. Type Bound: String.
  2. Class: SecondClass Type Parameter: V Type Variable: V. Type Bound: defaults to Object.

Edit: By Type Parameter, Type Variable and Type Bound I do not mean the grammar rule, but the concept. Therefore, extends is just the keyword.

About the leftmost bound you can find the answer in the same link, two sentences after the first quote:

The order of types in a bound is only significant in that the erasure of a type variable is determined by the first type in its bound, and that a class type or type variable may only appear in the first position.


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

...