I found a weird situation when casting generics. I run this code:
class A { }
class B { }
public class Program {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
List<A> listA = new ArrayList<A>();
List<?> list = listA;
((List<B>)list).add(new B());
for (Object item : listA) {
System.out.println(item.toString());
}
}
}
It compiles very well (only with warning but without error) and run without any exception and the output was:
B@88140ed
How did I do that? I mean why Java allow me to do such thing?
I added an instance of B
class to list of A
s?
It is very bad behaviour of generics. Why it is happening?
BTW, I tried it with Java 7.
EDIT:
What surprised me is that Java only notify the problem with warning that every programmer can ignore it. I know that SuppressWarnings
is bad idea, but why Java didn't denied such behavior with error or exception?
In addition, this warning showed always, if you think that your casting is correct you have no choice but to ignore it. But if you think that is good casting and ignore it but it isn't?