About Question enthuware.ocpjp.v8.2.1265 :
Posted: Thu Mar 10, 2016 2:15 pm
Regarding option 2:
with the explanation
Code: Select all
List<String> list = new ArrayList<>();
list.add("A");
list.addAll(new ArrayList<>());
This only applies to Java 7. In Java 8, the code above compiles and runs fine. Mind you, they do say you should avoid doing it, so I suppose this wouldn't be considered "appropriate use of generics".Java SE 7 supports limited type inference for generic instance creation; you can only use type inference if the parameterized type of the constructor is obvious from the context. In this option, the last line will not compile because list.addAll() method expects Collection<? extends String> and so the compiler cannot infer the generic type for ArrayList.