Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 754
user847614 Avatar asked Aug 16 '11 08:08

user847614


People also ask

What is variable and parameters?

Variables and parameters are objects to which you assign different values. Variables and parameters are useful when you have values that change depending on your job streams and jobs. Job stream, job, and prompt definitions that use them are updated automatically at the start of the production cycle.

What is parameter and type of parameter?

A parameter is a named variable passed into a function. Parameter variables are used to import arguments into functions. For example: function example(parameter) { console.

What is parameter type?

Supported parameter types are string, integer, Boolean, and array.

What is a parameter and example?

A parameter is used to describe the entire population being studied. For example, we want to know the average length of a butterfly. This is a parameter because it is states something about the entire population of butterflies.


2 Answers

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.

like image 60
Serabe Avatar answered Sep 28 '22 13:09

Serabe


TL;DRContext is everything!


Informally, "type variable" and "type parameter" are used interchangeably as synonyms for each other [even by Gilad Bracha — the creator of Java's Generics implementation].

Formally, however, the JLS explicitly describes them as two different — but closely-related — abstractions.

In the past, I have had similar questions myself about the less-than-crystal-clear usage of the terms "type parameter" and "type variable" in a lot of the literature on generics.

From my interpretation of the more-recent Java 8 JLS, a TypeParameter is what appears in a parameterized class' or method's type parameter section [the <> part of a declaration].

The JLS says, "A type variable is introduced by the declaration of a type parameter...". I interpret that to mean that, in order to use a TypeVariable, you must first fill out the method's [or the class'] type parameter section by following the rules specified for declaring a TypeParameter...

TypeParameter:
    {TypeParameterModifier} Identifier [TypeBound]

TypeParameterModifier:
    Annotation

TypeBound:
    extends TypeVariable
    extends ClassOrInterfaceType {AdditionalBound}

AdditionalBound:
    & InterfaceType

Reading the above syntactic production for TypeParameter and this one for TypeVariable...

TypeVariable:
    {Annotation} Identifier 

...I interpret those two productions to mean that if an identifier T is being used in a context where T has [or can have] a TypeBound after it, then T is a TypeParameter. Alternatively, if an identifier T is being used in a context where TypeBounds are not allowed, then T is a TypeVariable.


I think of a TypeParameter as being analogous to a formal parameter of a method declaration.

When the JLS says, "A type variable is an unqualified identifier used as a type in class, interface, method, and constructor bodies", I interpret that to mean that declaring a TypeParameter in the type parameter section, is also sort of analogous to declaring a class that can be subsequently used as a reference type of, say, an instance variable — in a sense.

By that I mean, in order for the following to be legal...

class Foo { 

   private Bar bar;
}

class Boff extends Foo { }

..then you must first introduce the declaration of the Bar type before it can be used in the body of Foo. Analogously, the type parameter <T extends Foo> must first be declared in order for the following to be legal...

class Baz<T extends Foo> { /* The TypeParameter that "introduces" T comes first */

    private T quux; /* now T is  a TypeVariable in this context */

    /* <U extends Number> is the TypeParameter that "introduces" the TypeVariable, U */

    public <U extends Number> List<? super U> m( Class<U> clazz ) throws Exception { /* <U extends Number> is the TypeParameter */

        U u = clazz.newInstance( ); /* U is a TypeVariable in this context */

        /*...*/

        List<? super U> list = new LinkedList<>(); /* U is a TypeVariable in this context; just like it is in this method's return type */          

        /*...*/

        list.add( u );

        /*...*/

        return list;

    }

} 

And if I'm allowed to be even more specific...

Baz<Boff> buzz = new Baz<>(); 

...the Boff inside the <> diamond, is neither a type variable nor a type parameter. It is a type argument .

like image 24
deduper Avatar answered Sep 28 '22 14:09

deduper