Card 10 / 42: Which of the following is a correct declaration and instantiation of list?
A)
List<String> list = new ArrayList<String>();
B)
List<?> list = new ArrayList<String>();
C)
List<? extends Object> list = new ArrayList<String>();
D)
List<Object> list = new ArrayList<String>();
E)
List list = new ArrayList<String>();
F)
List list = new ArrayList<?>();
Answer:
A) List<String> list = new ArrayList<String>();
B) List<?> list = new ArrayList<String>();
C) List<? extends Object> list = new ArrayList<String>();
E) List list = new ArrayList<String>();
Previous Card | ← Previous Card Button |
Next Card | → Next Card Button |
Flip Card | Space-Bar |
Type in declaration must match type in instantiation.
When <?> is used as declaration type, any type can be used in instantiation.
When <? extends X> as declaration type, X and any sub type of X can be used in instantiation.