Scrivio Interview library
CodingFoundations7 min read · updated 12 November 2026

How is int[][] grid = new int[3][]; different from new int[3][4]? Write code that creates a triangular array where row i has i + 1 elements.

Java has no true rectangular matrices: an int[][] is an array whose elements are references to separate int[] row objects. new int[3][4] creates the outer array and all three rows of four ints, while new int[3][] creates only the outer array, with three null rows that we create ourselves, which is how we build a jagged array such as a triangle where row i has i + 1 elements. Because rows are independent objects, they can be replaced or shared, so grid[0] = grid[1] makes two rows alias the same array.

Why this question is asked

Two-dimensional arrays look the same in Java as in C, but they work differently, and that difference explains several behaviours that surprise people: rows of different lengths, a NullPointerException from a freshly created grid, and two rows that change together. Interviewers ask this to see whether we understand that Java arrays are objects and what that means when they are nested. In this answer, we will recap how multi-dimensional arrays are built, compare the two creation forms, write the triangular array, look at row aliasing and copying, and end with what this layout means for memory and speed.

1A Quick Recap: Arrays of Arrays

In Java every array is an object on the heap, and an array variable holds a reference to it. An int[][] is therefore an array whose element type is int[]: each of its slots holds a reference to another array object, a row. There is no single block of memory with rows and columns; there is an outer array of references and a separate array object for each row.

Step throughWhat new int[3][4] creates
  1. Outer array: int[][] of length 3three references, one per row
  2. Row 0: int[4]its own object with header and 4 ints
  3. Row 1: int[4]another object, somewhere on the heap
  4. Row 2: int[4]another object

4 layers, from the top down. Step through them one at a time.

Four objects in total. The rows are independent, so nothing forces them to have the same length.

2New int[3][4] Versus new int[3][]

When we give both sizes, Java creates the outer array and fills it with three new rows of four ints each, all zero. When we give only the first size, Java creates just the outer array. Its slots are references, and the default value for a reference is null, so all three rows are null until we create them.

Java
int[][] full = new int[3][4];
int v = full[2][3]; // 0: every row exists
int len = full[1].length; // 4
int[][] grid = new int[3][];
int[] row0 = grid[0]; // null: no rows yet
int x = grid[0][0]; // NullPointerException: there is no row to index into
grid[0] = new int[] {1, 2}; // rows can be created later
grid[1] = new int[5]; // with different lengths
int[][] bad = new int[][3]; // compile error: the first size must be given

The form with an empty second size is exactly what we want when rows have different lengths, or when they come from somewhere else, such as lines read from a file. Notice the last line: we can leave trailing sizes empty, but not the first one, because Java must at least create the outer array.

3Building a Triangular Array

A triangular array has n rows, where row i has i + 1 elements. We create the outer array with n empty slots and then give each row its own length.

Java
int n = 4;
int[][] t = new int[n][];
for (int i = 0; i < n; i++) {
t[i] = new int[i + 1]; // row 0 has 1 element, row 3 has 4
}
System.out.println(Arrays.deepToString(t)); // [[0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]

A triangle saves memory compared with an n by n square, which matters for structures such as distance tables, where the distance from a to b is the same as from b to a, so we only store half.

4Filling a Triangle: Pascal's Triangle

A nice use of a jagged array is Pascal's triangle, where each row starts and ends with 1 and each inner value is the sum of the two values above it. Row i naturally has i + 1 values, so the triangular shape fits it exactly.

Java
int[][] p = new int[5][];
for (int i = 0; i < 5; i++) {
p[i] = new int[i + 1];
p[i][0] = p[i][i] = 1;
for (int j = 1; j < i; j++) p[i][j] = p[i - 1][j - 1] + p[i - 1][j];
}
Arrays.deepToString(p); // [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]

Notice that loops over a jagged array must use each row's own length, p[i].length or i + 1 here, never a single column count, or they will read past the end of the shorter rows.

5Rows Can Be Shared or Replaced

Because the outer array holds references, assigning one row to another slot copies the reference, not the numbers. Both slots then point at the same row object, and a change through either one is visible through both.

Java
int[][] grid = new int[3][4];
grid[0] = grid[1]; // both slots now refer to the same row
grid[1][0] = 7;
System.out.println(grid[0][0]); // 7: the "two" rows are one object
// the original row 0 is no longer referenced and will be garbage collected
int[][] copy = grid.clone(); // copies the outer array only
copy[2][0] = 9;
int y = grid[2][0]; // 9: the rows are still shared

The same reasoning explains why clone() on a 2D array is shallow: it copies the outer array of references, and both arrays still point at the same rows. A real deep copy clones each row, for example with a loop of copy[i] = grid[i].clone(), or with Arrays.stream(grid).map(int[]::clone).toArray(int[][]::new). Aliasing can also be intentional, for example to share one read-only row of defaults among many rows, but then it should be documented.

6What the Layout Means for Memory and Speed

Each row is a full object with its own header of about 16 bytes, and the outer array holds a reference per row, so a grid of many short rows wastes noticeable memory. Rows are also separate objects that may not sit next to each other in memory, so code that walks down a column jumps from row object to row object, while code that walks along a row reads contiguous ints. That is why row-by-row loops, with the inner loop over the second index, are usually much faster than column-by-column loops on large grids.

For large rectangular data where speed matters, such as images or matrices, a common alternative is a single flat array with an index computed from the row and column. It is one object, fully contiguous, and easy for the JIT to optimise.

Java
int rows = 1000, cols = 1000;
int[] flat = new int[rows * cols];
flat[r * cols + c] = 42; // element (r, c), row-major order

7Follow-up questions

Is new int[3][4] stored as one contiguous block of memory?

No. It creates four objects: the outer array and three row arrays. In practice HotSpot allocates them one after another, so right after creation the rows often sit close together, but nothing guarantees it, and the garbage collector may later move them independently. Code that needs a guaranteed contiguous layout uses a flat one-dimensional array with computed indexes.

How do you deep-copy a two-dimensional array?

By copying every row as well as the outer array. clone() and Arrays.copyOf on the outer array copy only the references, so the copies share rows with the original. A loop with copy[i] = original[i].clone(), or Arrays.stream(original).map(int[]::clone).toArray(int[][]::new), creates new row objects. For arrays of mutable objects, even that copies only the object references inside the rows, so a fully independent copy would also need to copy those objects.

How do you print a two-dimensional array?

With Arrays.deepToString(grid), which prints nested brackets such as [[1, 2], [3, 4]]. Arrays.toString(grid) prints only the outer level, showing each row as something like [I@1b6d3586, because a row is an int[] whose toString is inherited from Object. For grids shown to people, a loop that prints each row with Arrays.toString(row) on its own line is easier to read.

8Conclusion

In this answer, we saw that a Java int[][] is an array of references to separate row arrays, not a true matrix. new int[3][4] creates the outer array and three rows, while new int[3][] leaves three null rows that we create ourselves, which lets us build a triangular array with new int[i + 1] for row i. Because rows are independent objects, assigning grid[0] = grid[1] makes two rows alias one array and clone() is only shallow, and the layout makes row-by-row loops faster and flat arrays a good choice for large rectangular data.

What to say in the interviewAbout 90 seconds aloud

Open with

Java doesn't have real rectangular matrices; an int[][] is an array of arrays. The outer array holds references, and each row is a separate int array object on the heap. new int[3][4] creates the outer array and all three rows of four ints, all zero, so you can use any element right away. new int[3][] only creates the outer array; its three slots are references, so they're null, and grid[0][0] throws a NullPointerException until you assign the rows yourself.

Then, if they want more

That second form is what you use when rows have different lengths. For a triangular array, I'd write int[][] t = new int[n][], then loop i from 0 to n and set t[i] = new int[i + 1], so row 0 has one element and the last row has n. Loops over it must use each row's own length.

Because rows are just references, they can be replaced or shared: grid[0] = grid[1] makes both slots point at the same row object, so writing grid[1][0] also changes grid[0][0], and the old row 0 becomes garbage. The same reason makes clone shallow on a 2D array; a deep copy clones each row.

Finally, the layout matters for performance: each row has its own header, and rows aren't guaranteed to be contiguous, so I loop row by row, and for large rectangular data like images I'd often use one flat array with the index row times columns plus column.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How is int[][] grid = new int[3][]; different from new int[3][4]? Write code that...": https://scrivio-interview-library.netlify.app/java/arrays/3e8071699c/