Consider this Java code which attempts to instantiate some List
s:
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<>();
List<String> list3 = new ArrayList<String>() { };
List<String> list4 = new ArrayList<>() { };
List<String> list5 = new ArrayList<Integer>() { };
list1
and list2
are straightforward; list2
uses the new diamond operator in Java 7 to reduce unnecessary repetition of the type parameters.
list3
is a variation on list1
using an anonymous class, potentially to override some methods of ArrayList
.
list4
attempts to use the diamond operator, similar to list2
, but this is a compile error, with the message '<>' cannot be used with anonymous classes.
list5
produces an error that proves the compiler knows what type is actually needed. The error message is Type mismatch: cannot convert from new ArrayList<Integer>(){} to List<String>
So, with the declaration of list4
, why can't the diamond operator be used with anonymous classes? There is a similar question here with an accepted answer that contains the following explanation from JSR-334:
Using diamond with anonymous inner classes is not supported since doing so in general would require extensions to the class file signature attribute to represent non-denotable types, a de facto JVM change.
I need some help understanding that reasoning. Why would an explicit type versus the identical and apparently easily inferred type require any difference in the resulting class file? What difficult use case would be covered by "doing so in general"?
This was discussed on the "Project Coin" mailing list. In substance (emphasis mine):
Note that it is not supported in Java 8 either but will be included as a new feature in Java 9 (Item 3 of "Milling Project Coin").
You can use diamond opeator in Java9
You can use it in java 9 Example Diamond operator
From Java 10, you can do easily with
var
, the compiler will take care of the Type inference.