Q:

How does Java handle initializing arrays made of objects?

I'm learning java and for a particular application I am creating, I am initializing a 2D array of objects. The particular object that would occupy the array when initialized changes multiple variables in its no args constructor. I am wondering if when the array is declared java initializes each variable in all elements of the array:
private Piece positions[][]=new Piece[8][8];

Or is it necessary to do this?
for(int i=0;i<8;i++){
        for(int j=0;j<8;j++){
            Positions[i][j]=new Piece();

Thanks for your help!

A:

Java will initialize the value of an element in the array to the datatype's default value.
The JLS, Section 4.12.5, covers default values:

Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10.2):

For type byte, the default value is zero, that is, the value of (byte)0.

For type short, the default value is zero, that is, the value of (short)0.

For type int, the default value is zero, that is, 0.

For type long, the default value is zero, that is, 0L.

For type float, the default value is positive zero, that is, 0.0f.

For type double, the default value is positive zero, that is, 0.0d.

For type char, the default value is the null character, that is, '\u0000'.

For type boolean, the default value is false.

For all reference types (§4.3), the default value is null.

For primitive types, this is 0 or false, and for reference types, the default value is null.  So yes, you need to initialize each element as in your last code example, with new, else it will be null.

