Page 1 of 1

About Question enthuware.ocpjp.v21.2.3095 :

Posted: Sat May 17, 2025 3:33 am
by nkaragulov
if there was option return new ArrayList<>(); it would be correct too right?

Re: About Question enthuware.ocpjp.v21.2.3095 :

Posted: Sat May 17, 2025 4:32 am
by admin
What happened when you tried it out?


Yes, new ArrayList<> is also ok because <> is a diamond operator, which forces the compiler to apply the type based on the context. In this case, it will substitute <> with ArrayList<Number> or ArrayList<Object>, depending on JVM implementation. Exact type is not important because the caller of the getList method will only be able to use the returned object as List<? super Number>.