Java Generics drill-down

Source: Internet
Author: User

1.<? Extends E><? Extends e> is the wildcard character of the Upper Bound (upper limit), which is used to limit the type of the element. The   assignment list<? extends fruit> fruits;  indicates that the upper limit of the element type in the collection is Fruit type, that is, only subclasses of Fruit or Fruit. So for the following assignment is reasonable fruits = new arraylist<fruit> ();  fruits = new  Arraylist<apple> ();    write:  fruits.add (New fruit ());//compilation does not pass   fruits.add (New apple ());//compilation does not pass  fruits can point to arraylist<fruit&gt, or it can point to arraylist<apple>, arraylist< banana> When you add an apple, but fruits point to the arraylist<banana> type is unsure so you can't add   read:     No matter what the fruits points to, The compiler can determine that the obtained element is the fruit type, and that all elements in the Read collection are allowed  2.<? Super E><? Super E> is a wildcard character for Lower Bound (lower bound) to limit the type lower bounds   assignment of an element list<? super apple> apples; Indicates that the lower bound of the element type in the collection is the Apple type, which can only be the parent of Apple or Apple, so the following assignment is reasonable apples = new arraylist<apple> ();   apples = new ArrayList<Fruit> (  apples = new array);List<object> ();   write to:  because the apples element is a parent of Apple or Apple, we cannot determine which specific type But it is certain that Apple and Apple's subclasses are compatible with this "indeterminate class" because it is definitely a subclass of this "indeterminate type", meaning that we can add objects to the collection from Apple or Apple subclasses, So for the following additions is allowed Apples.add (New apple ());   apples.add (New redapple ());  read:  The compiler allows the element to be fetched from the apples, but it is not possible to determine what type of the obtained element, but only the subclass of the object type, so we want to get the element of the corresponding type that is stored in the only forced type conversion apple apple  =  (Apple) apples.get (0);//The element obtained is the object type    pecs Law Pecs law: The producer (Producer) uses extends, Consumers (Consumer) use Super1, producers         If you need a collection that provides an e-type element, use the generic wildcard character < Extends E>. It is like a producer that can provide data. 2, consumer         If you need a collection that can only load e-type elements, use the generic wildcard character <? Super e>. It is like a consumer that can consume the data you provide. 3, both producers and consumers         store and read, don't use generic wildcard characters.

Java Generics drill-down

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.