Java Generic _ upper bound Extends_ Nether Super

Source: Internet
Author: User

? Java Generic _ upper bound Extends_ Nether Super

? 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

<? The 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 (T) (the parent type) until the object


When using a Upper Bound wildcard

The following code,

/** * Code in the wildcard character <?> is <? Shorthand for extends object> * * @param list */public static void Test (list<?> list) {Object e = list.get (0);//Get O         K//List.set (0, E); Set compilation error List.set (0, New Integer (1)); Compile error}

The reason for set error is because the type in the method is not materialized, you can pass a string,number,book, and so on any class that inherits from object as the parameter type of the list to the test method,

The list requires that the type in the collection must be consistent, and there is no way to ensure that the set is in the same type as the list, for example, you pass the test method to the List<book> Then in the method set to enter an object obviously the type is inconsistent. This is also the price at which the wildcard brings flexibility.

Conclusion: the use of < Extends t> such a wildcard character, the test method parameter list becomes only get cannot set (except NULL) or not rigorous said it becomes read-only parameters, some similar to a producer, only provide data.


When using Lower Bound's wildcard
/** * list  is a list  *  list element all must be a supertype of number (parent type) until object (does not contain object)  * * @ Param list */public static void test (List<? super number> list)  {    number n = list.get (0);              //  Compilation Errors     Object o =  List.get (0);             // ok     list.set (0, new object ());           //  compilation Error     number number = new integer (0);     list.set (0, number);                 // ok    list.set (0, new long (0));         &nbsP;  // ok    list.set (0, new integer (0));         // ok}

list<? Super number> indicates that the type of the element contained in the list is a super type of number, and that the type of the list is at least one number type, so you can safely add number and its subtypes to it. list<? The type in super Number> may be a super-type of any number.

============end============

Java Generic _ upper bound Extends_ Nether Super

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.