? Super T and? Extends T difference

Source: Internet
Author: User

Java Generic keyword Description
    • ? Wild-Letter wildcard type
    • <? Extends t> represents the upper bound of the type, which indicates that the parameterized type may be a subclass of T or T
    • <? Super t> represents the lower bound of the type (called the superclass in Java core), which indicates that the parameterized type is a supertype of this type (the parent type) until the object
extends example

list<? Extends frut> means "with any list of inherited types from fruit", the compiler cannot determine the type that the list holds, so it is not safe to add objects to it. You can add null, because null can represent any type. So the Add method of the list cannot add any meaningful elements, but it can accept the existing subtype list<apple> assignment.

Fruit fruit = flist.get(0);Apple apple = (Apple)flist.get(0);

Because, where placement is a type inherited from fruit, the fruit type can be safely removed.

flist.contains(new Fruit());flist.contains(new Apple());

When using the Contains method in collection, the object parameter type is accepted, and no wildcard characters are involved, and the compiler allows this to be called.

Super Example
List<? super Fruit> flist = new ArrayList<Fruit>();flist.add(new Fruit());flist.add(new Apple());flist.add(new RedApple());// compile error:List<? super Fruit> flist = new ArrayList<Apple>();

list<? Super fruit> represents "a list with any Fruit superclass", and the list type is at least one Fruit type, so you can safely add Fruit and its subtypes to it. Due to list<? The type in super fruit> may be of any Fruit type and cannot be assigned a subtype of Fruit Apple list<apple>.

// compile error:Fruit item = flist.get(0);

Because, list< The type in super fruit> may be a supertype of any Fruit, so the compiler cannot determine whether the object type returned by get is Fruit or Fruit's parent food or object.

Summary
extends 可用于的返回类型限定,不能用于参数类型限定。super 可用于参数类型限定,不能用于返回类型限定。>带有super超类型限定的通配符可以向泛型对易用写入,带有extends子类型限定的通配符可以向泛型对象读取。——《Core Java》

? Super T and? Extends T difference

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.