E and? What is the difference between extends E and? Extends E

Source: Internet
Author: User
And? Extends E

This is Java's generic syntax. e indicates the classes currently used by generics? Extends e indicates the subclass of E or the implementation of E.

Why is it necessary? Syntax like extends e?

For example, the add method of list <number> numblist can naturally accept subclasses of number, including double or integer. Therefore, you only need to add (E) The add parameter, and do not need to add (? Extends E ).

This is because the addall method is required. addall (collection <? Exntends E>) in this interface, if addall (collection <E>) is used, list <double> is not accepted as the parameter, because according to the official statement, java generics are "invariant. A list <double> is not a list <number>. It is indeed a list <? Extends number>.

It is worth noting that arrays are different. A double [] is a number [].

Reference: http://stackoverflow.com/questions/2770264/what-is-the-difference-between-e-extends-number-and-number

This is Java's generic syntax. e indicates the classes currently used by generics? Extends e indicates the subclass of E or the implementation of E.

Why is it necessary? Syntax like extends e?

For example, the add method of list <number> numblist can naturally accept subclasses of number, including double or integer. Therefore, you only need to add (E) The add parameter, and do not need to add (? Extends E ).

This is because the addall method is required. addall (collection <? Exntends E>) in this interface, if addall (collection <E>) is used, list <double> is not accepted as the parameter, because according to the official statement, java generics are "invariant. A list <double> is not a list <number>. It is indeed a list <? Extends number>.

It is worth noting that arrays are different. A double [] is a number [].

Reference: http://stackoverflow.com/questions/2770264/what-is-the-difference-between-e-extends-number-and-number

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.