Assignment of list<> in Java

Source: Internet
Author: User

There is a thunder of the interview question is this:


Class A {}
Class B extends A {}
Class C extends A {}
Class D extends B {}
Which four statements is true?


A.The type List<a>is assignable to List


B.The type List<b>is assignable to list<a>

C.the type List<object>is assignable to list<?>

D.the type List<d>is assignable to List<?extends b>

E.the type list<?extends A>is assignable to List<a>

F.the type List<object>is assignable to any List reference

G.the type list<?extends b>is assignable to List<?extends a>


The correct answer is: a C D G


Analysis Process :


1. Just look inside the angle brackets!! Two concepts of explicit points and ranges

2. If there is a class in the angle brackets, then there is a point in the angle brackets, such as list<a>,list<b>,list<object>

3. If there is a question mark inside the angle brackets, then represent a range, < Extends a> represents a range of less than or equal to a, <? The Super a> represents a range greater than or equal to a,<?> represents the full range

4. All points in the angle brackets are assigned to each other in error, unless they are the same points.

5. The angle bracket small range assigns the value to the large range, to, the large range assigns the value to the small range, is wrong. If a point is contained within a range, it can be assigned, otherwise, it cannot be assigned to a value

6.list<?> and List are equal, both represent the maximum range

7.List is both a point and a range, representing the maximum range when representing a range


Code :

Public static void main (String[] args)  {         List<A> a;        List list;         list = a;   //a Yes, because list is List<?&gt, and represents the largest range, A is just one of the points that must be included         List<B> b;         a = b;      //b wrong, points can not be assigned to each other          List<?> qm;        List< object> o;        qm = o;      //c to,list<?> represents the largest range,list<object> just a point that must be included          List<D> d;        List<? extends B>  downb;    &Nbsp;   downb = d;  //d to,list<? extends b> represents a range of less than or equal to B, List <D> is a point in which         List<?extends A> downA;         a = downa;  //e wrong, range cannot be assigned to point          a = o;      //f wrong,list<object> just a point         downa = downb;  //g, a range less than or equal to a contains a range of less than or equal to B. Because B is smaller than A, B is the subclass of a     }


The above analysis from the cattle network friends @ Xiao Yu big beauty ~



This article is from the "Guardian" blog, please be sure to keep this source http://acevi.blog.51cto.com/13261784/1972063

Assignment of list<> in Java

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.