Java Learning: A brief description of the generic wildcard character < Extends t> and <? Super t>

Source: Internet
Author: User
Tags comparable

<, extends t> and < Super t> contains new concepts of JAVA5.0. Because their appearance has caused a lot of people to misunderstand their use:

1.< extends t> first you can easily misunderstand it as a collection of all classes that inherit from T, it is wrong to believe that you must have seen or used list<? extends t>? Why do I say it's wrong to understand a set? If it's understood as a set, why not use list<t> to express it? So< extends t> is not a collection, but the meaning of a certain seed class of T, remembering is a single, the problem is that it is impossible to add an element by adding (), because it is uncertain which one is not. You might also think why not add (T)? because < Extends t> is a seed class of T, a container that can be placed in a subclass is not necessarily in the superclass, that is, it is not possible to put T.

2.< Super t> here is easier to use, not <? extends t> so many restrictions, here means that the T class as the lower bound of a certain kind, simply said T class Super class. But why is add (T) possible? Because a container can be put into a certain class can be put into its sub-class, polymorphic concept.


Erase

Perhaps the most challenging aspect of generics is erasure (Erasure), which is the underlying technology for generic implementations in the Java language. Erasing means that the compiler basically throws out a lot of the type information of the parameterized class when it generates the class file. The compiler generates code with its coercion type conversions, just as the programmer did manually before the generics appeared. The difference is that the compiler has begun to validate a large number of type-safety constraints that are not validated without generics.

It is important to realize the meaning of generics by erasing, and it is confusing at first sight. Although list<integer> cannot be assigned to LIST<NUMBER>, because they are different types, list<integer> and list<number> types of variables are the same class! To understand this, please rate the following code:

New List<number> (). GetClass () = = new List<integer> (). GetClass ()

The compiler only generates a class for the List. When a List's bytecode is generated, traces of its type parameters are seldom left.

When generating bytecode for a generic class, the compiler replaces the type parameter with the erase of the type parameter. For an unrestricted type parameter (<V>), its erase is Object. For the upper-type parameter (<k extends comparable<k>>), its erase is the upper bound (in this case, comparable). For type parameters with more than one limit, use their most left-bound erase.

If you examine the generated bytecode, you cannot tell the difference between the code of List<integer> and list<string>. The type limit T is replaced by the upper bound of T in the bytecode, which is generally an Object.

Multiple limits

A type parameter can have multiple restrictions. This is useful when you want to constrain a type parameter, such as comparable and Serializable. The syntax for multiple restrictions is delimited with the ampersand:


Class C<t extends Comparable<? Super T> & Serializable>

A wildcard type can have a single limit-the upper or lower limit. A specified type parameter can have one or more caps. A type parameter with multiple restrictions can be used to access the methods and fields of each of its restrictions.


Type parameters and type arguments

In the definition of parameterized classes, placeholder names (such as V in collection<v>) are called type parameters (types parameter), which resemble the formal arguments in the method definition. In the declaration of a variable of a parameterized class, the type value specified in the declaration is called the type argument (argument), which resembles the actual argument in the method invocation. But in practice both are generally referred to as "type parameters". So give the definition:

Interface Collection<v> {...}

and statements:

Collection<string> cs = new hashset<string> ();

Then, the name V (which can be used throughout the Collection interface body) is called a type parameter. In the declaration of CS, the two uses of String are type arguments (once for collection<v> and another for hashset<v>).

There are some limitations on when you can use type parameters. Most of the time, you can use a type parameter anywhere you can use the actual type definition. But there are exceptions. They cannot be used to create objects or arrays, and they cannot be used in a static context or in the context of handling exceptions. They cannot be used as a parent type (class Foo<t> extends T), cannot be used in an instanceof expression, and cannot be used as a class constant.

Similarly, there are some limitations on which types can be used as type arguments. A type argument must be an instantiation of a reference type (not a base type), a wildcard, a type parameter, or other parameterized type. So you can define list<string> (reference type), list<?> (wildcard character), or list<list<?>> (instantiation of other parameterized types). In the definition of a parameterized type with type parameter T, you can also declare list<t> (type parameter).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.