2

Below code:

int[][][] aT = { {{9, 4}, null},  {null},  {{1, 2, 3}} };
System.out.println(aT.length + " " + aT[1].length);  // 3 1

shows aT[1].length as 1.

Is it not that, length will be counted only when you have an element of type int[][] at second index in the object pointed by aT? If yes, Why aT[1] that is null is of length 1? Because null is not of type int[][]

Similar example:

MyType[] t = {null};
System.out.println(t.length); //1

For me t.length should be 1, only when array has element like {new MyType()} or element is an instance of subtype of MyType, something like,

MyType[] t = {new MyType()};
System.out.println(t.length); //1

2 Answers 2

7

Yes.

In fact, when you declare and initialize an array of object references, all the values inside it will be null by default. Remember that the length of an array will never vary, despite the contents (null elements) in the array. There's a difference between length and size, the length is how many elements can be stored in an array, while the size is the amount of valid elements in the array. Also, an array is not a list.

Example:

String[] stringArray = { null, null };
System.out.println(stringArray.length);
stringArray[0] = "hello";
stringArray[1] = "world";
System.out.println(stringArray.length);

Prints:

2
2

From JLS(emphasis mine):

An array object contains a number of variables. The number of variables may be zero, in which case the array is said to be empty. (...) If an array has n components, we say n is the length of the array; the components of the array are referenced using integer indices from 0 to n - 1, inclusive

(...)

Once an array object is created, its length never changes.

(...)

An array is created by an array creation expression (§15.10.1) or an array initializer (§10.6)

An array creation expression specifies the element type, the number of levels of nested arrays, and the length of the array for at least one of the levels of nesting. The array's length is available as a final instance variable length.

An array initializer creates an array and provides initial values for all its components.

(...)

The length of the array to be constructed is equal to the number of variable initializers immediately enclosed by the braces of the array initializer. Space is allocated for a new array of that length. If there is insufficient space to allocate the array, evaluation of the array initializer completes abruptly by throwing an OutOfMemoryError. Otherwise, a one-dimensional array is created of the specified length, and each component of the array is initialized to its default value.

(...)

  • The public final field length, which contains the number of components of the array. length may be positive or zero.

From here, you may understand the following:

  • When you initialize an array, you have to define at least the length of the first level (a.k.a. dimension). If you have an array of a single level, then this length will be the length of the whole array.
  • When the array is initialized, all its components (a.k.a. elements) are initialized with the default value of the type. For primitive types, it may be 0 or false (depends on the type). For object references, it's null.
  • The length of an array represents how many components can have.
  • An array has a fixed length. This length cannot be changed.

Again, the length of an array (how many components can store) is not the same as the size of an array (how many components that we consider proper values is storing at the moment).

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

6 Comments

But in second there are 2 strings, which is fine. IN first case, {null, null} are not two strings. why 2 in first case?
@overexchange, "null" is certainly a valid instance for an object like a String. It is, essentially, a String because it /behaves/ as it it can be cast to any reference type.
How null being casted to any reference type has anything to do with length of the array?
For your point: "when you declare and initialize an array of object references, all the values inside it will be null by default." syntactically this is not possible, except mentioning as null(explicitly), may be you are trying to say: "When you declare and create and array of object", something like MyType mt[][] = new MyType[1][2];.
How to find size of an array?
|
3

Yes, null can be considered an object of the array's type. Null can be a string, a Date, a JFrame, anything you want it to be.

3 Comments

Because null can be cast to any reference type? java array is one example of reference type.
null doesn't belong to an exact reference type. It's not like it can be casted.
String string = (String) null;

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.