I'm having trouble understanding why the java-compiler allows non-specific collections to be assigned collections where the variable have been specified. Like this:
ArrayList list = new ArrayList();
// Operations on list
ArrayList<String> stringList = list;
There's potential for all kinds of casting errors in this, it seems to me it would make more sense if the compiler stopped you from doing this in the first place.
I'm only asking because I'm curious of this somewhat weird aspect of the language, I don't actually have trouble getting the code to work (although I might some day when I need to use an ArrayList with all kinds of classes in it).
Its just for supporting legacy code before generics or java 5.
Generics, introduced in Java SE 5
and Collection has been running since long back. So if you see Collection framework before 1.5 you see ArrayList, there is no generic
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With