Upper and lower bound wildcard characters in Java generics

Source: Internet
Author: User

Java generics, wildcard? denotes unknown type, equivalent to <? Extends Object>,<? Extends t> is the upper boundary limited wildcard character, <? The Super T> is the next boundary qualified wildcard character.

First, the difference

In a list, the upper and lower bounds wildcard characters can be stored and read as shown in the type of object:

Second, the principle

The use of upper and lower bounds wildcard characters should follow the pecs principle: Producer extends,consumer Super.

Qualifying wildcard characters always include their own

Upper bound type wildcard: The Add method is limited

Nether Type Wildcard: Get Method Limited

If you want to get data from a data type, use the? Extends wildcard characters

If you want to write objects into a data structure, use the? Super wildcard characters

If you want to save and take it, don't use wildcards

You cannot declare both the upper and lower bounds of a generic wildcard

Reference:

Java generic <? Super T> How does super understand? How is it different from extends? -Chubby Answer-https://www.zhihu.com/question/20400700/answer/117464182

51943742

Upper and lower bound wildcard characters in Java generics

Related Article

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.