About Question enthuware.ocpjp.v21.2.3095 :
Moderator: admin
-
- Posts: 17
- Joined: Mon Sep 16, 2024 1:49 am
- Contact:
About Question enthuware.ocpjp.v21.2.3095 :
if there was option return new ArrayList<>(); it would be correct too right?
-
- Site Admin
- Posts: 10382
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v21.2.3095 :
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>.
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>.
Who is online
Users browsing this forum: No registered users and 90 guests