0

I was under the impression that storing and accessing data beyond the upper bound specified for arrays will result in Exception but I was coding a solution and I stumbled upon a scenario where the array stored more elements than the upper bound.

public static void main(String[] args) {
        int[][] arr = new int[1][5];
        String values = "1 2 3 4 5 6";
        arr[0] = Arrays.asList(values.split(" ")).stream().mapToInt(Integer::parseInt).toArray();
        for (int i : arr[0]) {
            System.out.println(i);
        }
    }

The above program outputs 1 2 3 4 5 6 in separate lines while clearly I have specified my upperbound as 5. Can someone explain this weird phenomenon?

2
  • 2
    a[0] is just a reference to an array, initialized to a new array of size 5. You change the reference to a new array of size 6. Commented Sep 22, 2019 at 2:10
  • @JohannesKuhn Ohh basically then it treats as an array initialization rather than an assignment then. Commented Sep 22, 2019 at 2:12

2 Answers 2

2

Can someone explain this weird phenomenon?

Here goes:

    int[][] arr = new int[1][5];

is declaring an int[][] and initializing it to a structure consisting of one top-level int[][] of length 1, and one second level int[] subarray of length 5. The first cell of the former refers to the latter.

Then:

    arr[0] = Arrays.asList(values.split(" "))
                   .stream()
                   .mapToInt(Integer::parseInt)
                   .toArray();

is creating an int[] of length 6 and assigning it to arr[0]. This replaces the int[] subarray of length 5 that was created in the earlier initialization.

The intuitive behavior is that the "shape" of arr changes from a int[1][5] to int[1][6] when you do that assignment.


You then commented:

"Ohh basically then it treats as an array initialization rather than an assignment then."

Strictly, no. If you think of arr[0] = ... as an array initialization, it gets confusing. It is actually an assignment of a newly create array of type int[] to a cell of an existing "array of int[]".

This may sound like a fine distinction, but it goes to the heart of the matter. If you read the JLS and the JVM specs deeply, you will see that Java array types and objects are fundamentally one dimensional. Thus int[][] is a syntactic shorthand1 for "array of int[]", and arr[i][j] is a shorthand for (arr[i])[j].

Thus when we do arr[0] = ... in the example, we are not initializing something. Instead, we are changing a data structure that was initialized previously. At least, that is the Java language perspective.

From the perspective of the class where this is happening, this could be getting something into the initial state that is required by the class semantics. Thus it is reasonable to call that "initialization" ... from that perspective.


1 - This shorthand is so ingrained in the language that there isn't any other way to write the type "array of int[]" using Java syntax. The syntax for array types comes from C via C++, though the particular Java semantics of arrays doesn't.

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

1 Comment

Hey Stephen thanks for the answer it cleared my doubts
0

After assignment, arr[0] is holding a list object that has bounds set to whatever Arrays.asList defines.

Comments

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.