JAVA generic wildcard characters? The usage of EXTENDS SUPER

Source: Internet
Author: User

1. <? Extends hero>

ArrayList herolist<? Extends hero> indicates that this is a Hero generic or its subclass generic
The generics of the herolist may be hero
The generics of the herolist may be Aphero
The generics of the herolist may be Adhero
So it is hard to be sure that the objects taken out of the herolist can be transformed into hero.


But, you can't put things inside, because
It's not enough to put Aphero <ADHero>
Put Adhero and not satisfied <APHero>

2. <? Super hero>

ArrayList herolist<? Super Hero> indicates that this is a Hero generic or its parent generic type
The generics of the herolist may be hero
Herolist generics may be object

You can insert a subclass of hero and hero inside.
But it's risky to take out, because I'm not sure if it's hero or object.

3. Generic wildcard characters?

Generic wildcard characters? Represents any generic type
Since it represents any generic type, in other words, what generics are possible in this container

So it can only be taken out in the form of an object.
And you can't put objects in it, because you don't know what a generic container is.

JAVA generic wildcard characters? The usage of EXTENDS SUPER

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.