ArrayElementsUntyped
DRAFT

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

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

Language

Java

Concepts

Other Languages

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.