Java core Technology notes for busy people (6, generic programming)

Source: Internet
Author: User

Points:

A generic class is a class with one or more type parameters

A generic method is a method with a type parameter

You can require a type parameter to be a subtype of one or more types

A generic type is not to become: when S is a subtype of T is,g<s> and g<t> is not related

By using wildcard characters g<? Extends t> or g<? Super T> You can specify a method to accept an instance of a generic type that takes a string class or parent parameter

Type parameters are erased when generic classes and methods are compiled

Type erasure has many limitations on generics. In particular, you cannot instantiate a generic class or array. cannot be converted to a generic type, or throws a generic type object.

Even if generic classes and methods are erased in the virtual machine, you can also find out how they are declared at run time.

1. Generic type

※the type parameter cannot be instantiated with a basic type. For example, entry<string, int> is not valid in Java.

When constructing a generic class object, you can omit the type parameter in the constructor.

1 New Entry<> ("1", 1);

2. Generic method

When declaring an anti-wash method, the type parameter is placed after the modifier, before the return type. When called, do not specify a type parameter. It can be inferred from the parameter and return type of the method.

can also be displayed at the time of the call to write

1  Public Static void int int j); 2 Arrays.<string>swap (friends,0,1);

3. Type limit

The type parameters of a generic class or method need to meet certain requirements, or you need to be able to invoke some common methods . You can qualify the type.

T extends Autocloseable

Type parameters can have multiple qualifications: T extends Runnable & autocloseable

※ Multiple interfaces can be qualified, but only one of them is a class. If a qualification is a class, it must be placed first in the qualified list.

Java core Technology notes for busy people (6, generic programming)

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.