Why cannot I write "Map<String,String> m = (Map<>)obj"?
maurizio.cimadamore at oracle.com
Wed Jul 25 02:58:18 PDT 2012
On 25/07/12 10:11, Weijun Wang wrote:
> Just curious, if obj is of type Object, and I want to cast it to
> Map<String,String>, why cannot I use the diamond operator?
currently, diamond operator is only supported in the context of an
instance creation expression - i.e. in the type following the 'new'
keyword. As you point out, there are other places where diamond syntax
might be useful - cast is one them, as the right-hand-side of a variable
declaration, or the type of a constructor reference (in Project Lambda).
Experiments with a very fat corpus of Java code confirmed that the
'new'/variable type declaration cases are the most recurrent ones - down
cast to generic types, albeit important, tends to occur less frequently
- unchecked warnings probably play a role here.
More information about the compiler-dev