Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Java allow type-unsafe Array assignments?

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?

like image 408
Rafael T Avatar asked Dec 03 '12 14:12

Rafael T


People also ask

Why does Java not allow generic arrays?

If generic array creation were legal, then compiler generated casts would correct the program at compile time but it can fail at runtime, which violates the core fundamental system of generic types.

Is an array type safe?

Type Safety On the other hand, type checking is not possible with arrays as it does not have a support of Generics, therefore array is not type-safe.

Can we use generics with array in Java?

Java allows generic classes, methods, etc. that can be declared independent of types. However, Java does not allow the array to be generic. The reason for this is that in Java, arrays contain information related to their components and this information is used to allocate memory at runtime.

How do you initialize a generic array in Java?

Array#newInstance to initialize our generic array, which requires two parameters. The first parameter specifies the type of object inside the new array. The second parameter specifies how much space to create for the array.


2 Answers

Firstly, I should point out that this is type-safe.

Object[] objects = new Integer[10];
objects[0] = "Hello World";

because an exception will be thrown. (It is not statically type-safe ... but that is a different statement entirely.)

The reason that Java allows this is historical. Until Java 5, Java did not support any form of generics. Gosling has said that if they had had the time to figure out and incorporate generics into Java 1.0, they would have done so.

Unfortunately, they didn't. But they still wanted to be able write things like a general purpose sort method with the following signature:

    void sort(Object[] array, Comparator comp) ...

To make this method work for any kind of object array (without generics), it was necessary to make arrays covariant; i.e. to make it legal to pass a String[] or Integer[] as an argument where the formal type is Object[]. If they hadn't done that you would have had to copy the String[] to an Object[], sort it, and then copy it back.

like image 120
Stephen C Avatar answered Oct 05 '22 23:10

Stephen C


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.)

like image 40
millimoose Avatar answered Oct 06 '22 00:10

millimoose