About Question enthuware.ocpjp.v21.2.3095 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
nkaragulov
Posts: 17
Joined: Mon Sep 16, 2024 1:49 am
Contact:

About Question enthuware.ocpjp.v21.2.3095 :

Post by nkaragulov »

if there was option return new ArrayList<>(); it would be correct too right?

admin
Site Admin
Posts: 10382
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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>.

Post Reply

Who is online

Users browsing this forum: No registered users and 90 guests