6

If I construct:

Object[][] guy = new Object[5][4];

do the spots in my array have default values? Are they null?

Is there a way to assign default values for every spot in the array?

0

6 Answers 6

14

Yes, fields in new arrays are initialized with null in Java.

You can use the method Arrays.fill() to fill all fields in an array with a specific value.

If you have arrays of short length where you statically know what to put it, you can use array initializers:

Object[][] guy = { { obj1, obj2, null, obj3 }, { ... }, ... };

You have to type out the full array with all fields (20 in your case) for that, so if you want to put the same value in every place, fill() is probably more convenient.

Arrays of primitive types btw. are initialized with the various variants of 0 and with false for boolean arrays (as long as you don't use an initializer). The rules for array initialization are the same as for the initialization of fields, and can be found here in the Java Language Specification.

Sign up to request clarification or add additional context in comments.

1 Comment

It should be mentioned that Arrays.fill(array, new MyObject()) only creates a single object which is then assigned to all elements, which might be unintended in many use cases with object arrays.
9

The existing answers are all correct, but there is one bit to add in your particular example. Although reference arrays are defaulted to null, multi-dimensional arrays have the first dimension implictly created.

For example, in your case the first dimension wont be null, but will in fact point to another array:

Object[][] guy = new Object[5][4];
System.out.println(guy[0]);
--> Output will be [Ljava.lang.Object;@5e743399

Comments

1

In Java, objects initializations assume the default value "null".

Comments

1

All variables/fields are initialized in Java.

Consult http://docs.oracle.com/javase/specs/

2 Comments

You mean primitives not Numbers. And booleans are false.
Non primitives are references, initialized to null.
1

Primitive types are initialized by default values, Referenced types are NOT initialised (so they are null)

Try to run code snippet to see what happens public class ArraysSample {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Object[][] objAr = new Object[4][5];
    for (Object[] objects : objAr) {
        for (Object object : objects) {
            System.out.print(object + "\t");
        }
        System.out.println();
    }

    int[][] primitievArray = new int[4][5];
    for (int[] is : primitievArray) {
        for (int i : is) {
            System.out.print(i + "\t");
        }
        System.out.println();
    }

}

}


null null null null null
null null null null null
null null null null null
null null null null null
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

Comments

0

Yes you can have default assigning, for instance :

instead of:

char[][] c = new char[5][4];

you can:

char[][] c = {{'a','b','c','x','B'}, {'A','Z','w','Z','S'},
{'A','Z','w','Z','S'},{'A','Z','w','Z','S'}};

2 Comments

Char needs to be char here.
missed that, Thanks @PhilippWendler :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.