Why does Java allow type-unsafe Array assignments?

arrays, declaration, java, runtime-error, type-safety

Solution

I don't think there's an answer to this besides "legacy design". (Which I admit is a fancy way of saying "because".) You pretty much need to be able to do an equivalent of the last assignment you show somehow. (Otherwise you're stuck to making lots and lots of copies with manual up/down casts, assuming language features of Java pre 1.4)

In Java 1 when type semantics for arrays were basically set in stone, generics weren't available, or even up for consideration for a long while yet. So there was no mechanism available to express the higher-order type constraints needed to make this construct type-safe – and Gosling (IIRC a fan of simplicity) felt resolving this edge case of compile-time type safety wasn't worth complicated the language with whichever solutions were available. Or wasn't bothered by doing the check at runtime enough to even look for a solution. (At the end of the day language design decisions are arbitrary to at least some degree, and there's only one person that could answer this with any certainty.)

Problem

Generally, Java can be considered as a type-safe language. I know that there are some flaws with generics, but I recently came across a Problem I never had before. To break it down: ``` Object[] objects = new Integer[10]; objects[0] = "Hello World"; ``` will NOT result in a compile-time error as expected. I would assume that the declaration of an Array of `Object` will disallow to point to to an array of something else. In Generics I'm not allowed to make such weird things like: ``` ArrayList<Object> objs = new ArrayList<Integer> ``` and if I try to kind of trick Java into doing something with ``` ArrayList<? extends Object> objects = new ArrayList<Integer> ``` I'm allowed to declare it, but I can only add Objects of type `null`. Why doesn't Java prevent the declaration of such weired arrays?

Original source