A friend of mine noticed that
var<Integer> list = new ArrayList<Double>();
was valid in Java. It turns out that the type of list
is evaluated to ArrayList<Double>
.
When using var<Integer> list = new ArrayList<>();
, list
is just ArrayList<Object>
.
Both of us were not able to figure out, what the generic type of var
does, as it seems to be ignored. But if so, why is this even syntactically correct in the first place?
Main.java:5: error: illegal reference to restricted type 'var' var<Integer> list = new java.util.ArrayList<Double>();
. – ZoharaList<Integer> list = new ArrayList<Double>();
can now bevar<Integer> list = new ArrayList<Double>();
... because typing is hard for some people 🙄. This is only available, AFAIK to local variables (ie within a closed context) – Huang