Why does this compile?
Posted
by akf
on Stack Overflow
See other posts from Stack Overflow
or by akf
Published on 2009-09-29T22:35:14Z
Indexed on
2010/06/14
0:42 UTC
Read the original article
Hit count: 184
I was taken aback earlier today when debugging some code to find that something like the following does not throw a compile-time exception:
public Test () {
HashMap map = (HashMap) getList();
}
private List getList(){
return new ArrayList();
}
As you can imagine, a ClassCastException
is thrown at runtime, but can someone explain why the casting of a List
to a HashMap
is considered legal at compile time?
© Stack Overflow or respective owner