DRAFT
ArrayElementsUntyped
Misconception:
One can assign values of arbitrary types to elements of an array.
Incorrect
Elements of arrays are untyped
Correct
The type of an array specifies the type of the elements
CorrectionHere is what's right.
Here is what's right.
The following code is illegal:
int[] a = new int[3];
a[0] = "Hi"; // type error
a[1] = 12;
a[2] = 13.5; // type error
Like everything in Java, arrays are strongly typed. If we have an array of type int[]
, we can only assign values of type int
to its elements.