Java Generic Learning Notes-(vii) A brief analysis of the use of wildcard characters in generics

Source: Internet
Author: User

First, the basic concept:
In the process of learning Java generics, wildcards are a more difficult part to understand. There are three main categories:
1. The borderless wildcard (unbounded wildcards) is <?>, such as List<?>.
The main function of borderless wildcards is to allow generics to accept data of unknown types.
2. Fixed the upper boundary wildcard character (Upper bounded wildcards):
You can accept data for a specified class and its subclass type by using generics that hold the upper boundary of the wildcard character. To declare the use of the wildcard character, using <? Extends the E> form, where E is the upper boundary of the generic type. Note: Although the extends keyword is used here, it is not limited to inheriting the subclass of the parent class E, but also referring to the class that shows the interface E.
3. The wildcard character (Lower bounded wildcards) that secures the lower boundary:
You can accept data from a specified class and its parent class type by using the generic of a wildcard character that has a fixed lower boundary. To declare the use of the wildcard character, using <? Super E> form, where E is the bottom boundary of the generic type. Note: You can specify a top or bottom boundary for a generic, but you cannot specify both the upper and lower bounds.

Java Generic Learning Notes-(vii) A brief analysis of the use of wildcard characters in generics

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.