That’s because Java is not C language.
When you write int arr[][] = {{1, 2, 3, 4}, {4, 3, 2, 1}};, you declare and initialize a multidimensional array of 2 by 4. It is equivalent to the C language initialization you proposed (int arr[2][4] = {{1, 2, 3, 4}, {4, 3, 2, 1}};)

In Java, we preferably declare arrays in a Java-style manner:

int[][] arr = {{1, 2, 3, 4}, {4, 3, 2, 1}};

where int[][] is just a type identifier, we cannot specify size.
Moreover, in Java, we can initialize a 2D array that is not rectangular:

int[][] arr = {{1, 2, 3, 4}, {4, 3}};

If you want to declare an array of specific size, but don’t want to specify all it’s values at once, the only solution is to use new:

int[][] arr = new int[2][4];
arr[0][0] = 1;
Answer from Auktis on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › different-ways-to-declare-and-initialize-2-d-array-in-java
Different Ways To Declare And Initialize 2-D Array in Java - GeeksforGeeks
July 23, 2025 - ... import java.io.*; class GFG ... arr = new int[n][m]; // initializing the array elements using for loop for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = i + j; } } // printing the first three rows ...
Discussions

Syntax for creating a two-dimensional array in Java - Stack Overflow
Local variables (inside methods) must be manually initialized before use. 2014-05-03T05:33:25.643Z+00:00 ... Save this answer. ... Show activity on this post. It is also possible to declare it the following way. It's not good design, but it works. ... Save this answer. ... Show activity on this post. ... Note that in your code only the first line of the 2D array ... More on stackoverflow.com
🌐 stackoverflow.com
Need help in Java specifically 2D Array
Show the code you’ve tried so we can give you hints to point you in the right direction or to help explain any misconceptions you have. More on reddit.com
🌐 r/learnprogramming
8
2
April 25, 2023
Two dimensional ArrayList, need help with constructor
There are probably better ways to solve your problem, but here is the answer to your question: List> list1 = new ArrayList>(); for (int i = 0; i < 10; i++) { List list2 = new ArrayList(); for (int j = 0; j < 10; j++) { list2.add(i * j); } list1.add(list2); } Integer value = list1.get(1).get(5); More on reddit.com
🌐 r/java
5
0
February 17, 2012
How to add arrays to make a 2d array?
Can you not just make an empty arraylist, and append each array that you are reading into it? More on reddit.com
🌐 r/learnjava
6
12
April 9, 2019
🌐
Runestone Academy
runestone.academy › ns › books › published › apcsareview › Array2dBasics › a2dDAS.html
10.3. Declaring 2D Arrays — AP CSA Java Review - Obsolete
To declare a 2D array, specify the type of elements that will be stored in the array, then ([][]) to show that it is a 2D array of that type, then at least one space, and then a name for the array. Note that the declarations below just name the variable and say what type of array it will reference.
🌐
Ruby-Doc.org
ruby-doc.org › home › two dimensional array in java – the ultimate guide with examples
Two Dimensional Array in Java - The Ultimate Guide with Examples - Ruby-Doc.org
April 10, 2026 - javaCopyEditint[][] numbers; // Declaring a 2D array of integers String[][] names; // Declaring a 2D array of Strings double[][] grades; // Declaring a 2D array of doubles · There are several ways to initialize a 2D array in Java.
🌐
GeeksforGeeks
geeksforgeeks.org › java › multidimensional-arrays-in-java
Java Multi-Dimensional Arrays - GeeksforGeeks
... public class Geeks { public static void main(String[] args) { // Declaring a 2D array int[][] arr; // Initializing row and column sizes arr = new int[1][3]; // Assigning values arr[0][0] = 3; arr[0][1] = 5; arr[0][2] = 7; // Displaying values ...
Published   May 4, 2026
Top answer
1 of 13
889

Try the following:

int[][] multi = new int[5][10];

... which is a short hand for something like this:

int[][] multi = new int[5][];
multi[0] = new int[10];
multi[1] = new int[10];
multi[2] = new int[10];
multi[3] = new int[10];
multi[4] = new int[10];

Note that every element will be initialized to the default value for int, 0, so the above are also equivalent to:

int[][] multi = new int[][] {
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

... or, more succinctly,

int[][] multi = {
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
2 of 13
86

We can declare a two dimensional array and directly store elements at the time of its declaration as:

int marks[][]={{50,60,55,67,70},{62,65,70,70,81},{72,66,77,80,69}};

Here int represents integer type elements stored into the array and the array name is 'marks'. int is the datatype for all the elements represented inside the "{" and "}" braces because an array is a collection of elements having the same data type.

Coming back to our statement written above: each row of elements should be written inside the curly braces. The rows and the elements in each row should be separated by a commas.

Now observe the statement: you can get there are 3 rows and 5 columns, so the JVM creates 3 * 5 = 15 blocks of memory. These blocks can be individually referred ta as:

marks[0][0]  marks[0][1]  marks[0][2]  marks[0][3]  marks[0][4]
marks[1][0]  marks[1][1]  marks[1][2]  marks[1][3]  marks[1][4]
marks[2][0]  marks[2][1]  marks[2][2]  marks[2][3]  marks[2][4]


NOTE:
If you want to store n elements then the array index starts from zero and ends at n-1. Another way of creating a two dimensional array is by declaring the array first and then allotting memory for it by using new operator.

int marks[][];           // declare marks array
marks = new int[3][5];   // allocate memory for storing 15 elements

By combining the above two we can write:

int marks[][] = new int[3][5];
🌐
Java67
java67.com › 2014 › 10 › how-to-create-and-initialize-two-dimensional-array-java-example.html
How to declare and Initialize two dimensional Array in Java with Example | Java67
This way you can initialize 2D array with different length sub array as shown below : String[][] squares = new String[3][]; squares[0] = new String[10]; squares[1] = new String[20]; squares[2] = new String[30]; You can see that our 2 dimensional ...
Find elsewhere
🌐
Codecademy
codecademy.com › learn › learn-java › modules › java-two-dimensional-arrays › cheatsheet
Learn Java: Two-Dimensional Arrays Cheatsheet | Codecademy
2D arrays are declared by defining a data type followed by two sets of square brackets. ... In Java, when accessing the element from a 2D array using arr[first][second], the first index can be thought of as the desired row, and the second index ...
🌐
Java2Blog
java2blog.com › home › core java › java array › initialize 2d array in java
Initialize 2D array in Java - Java2Blog
January 11, 2021 - If we want to initialize a variable lenght columns array at the same time of creating an array then use initialilzer block as we did in the given example. ... Java provides a class Array in reflection package that can be used to create an array.
🌐
Medium
medium.com › @iamprem021 › a-simple-guide-to-2d-arrays-in-java-4f5f5e5e1a96
A Simple Guide to 2D Arrays in Java | by premprakash | Medium
May 24, 2024 - A 2D array in Java is an array of arrays. Think of it like a table with rows and columns, where each cell holds a value. It’s like a grid or matrix that can store data in a more organized way. Here’s how you can declare and initialize a 2D array in Java:
🌐
Blogger
javarevisited.blogspot.com › 2016 › 02 › 6-example-to-declare-two-dimensional-array-in-java.html
6 ways to declare and initialize a two-dimensional (2D) String and Integer Array in Java - Example Tutorial
June 28, 2025 - We have actually declared int[] only Another thing to remember about this code is that if multiple variables are declared in the same line they would be the type of int[] which is one dimensional, not two dimensional like in the following example prices is a 2D array but abc is just a one-dimensional int array. int[] prices[], abc; Again, this is a tricky array concept in Java and that's why you will often find questions on this topic on various Java certifications.
🌐
Scaler
scaler.com › home › topics › two dimensional array in java
Two Dimensional Array In Java with Examples - Scaler Topics
June 8, 2024 - Let's look at each of these initialization methods in detail: To declare a two-dimensional array using both dimensions, we just have to pass the number of rows and the number of columns of the matrix in the square brackets as shown below: This syntax will declare a 2D Integer array having two ...
🌐
Runestone Academy
runestone.academy › ns › books › published › csjava › Unit9-2DArray › a2dSummary.html
9.3. 2D Arrays Summary — CS Java
Remember that the last valid row index is arr.length - 1. The last valid column index is arr[0].length - 1. 9-3-1: Drag the item from the left and drop it on its corresponding answer on the right. Click the "Check Me" button to see if you are correct. Review the summaries above. ... 9-3-2: Drag the description from the left and drop it on the correct code on the right. Click the "Check Me" button to see if you are correct. Review the summaries above. ... Initialize a 2d array of integers named nums so that it has 1,2,3 in the first row and 4,5,6 in the second row.
🌐
HappyCoders.eu
happycoders.eu › java › initialize-array-java
How to Initialize Arrays in Java
June 12, 2025 - In this case, intMatrix[0] and intMatrix[1] are each initially null. Incidentally, it is not mandatory for all sub-arrays to be the same length. The following is also permitted: int[][] intMatrix = new int[2][]; intMatrix[0] = new int[]{2, 3, 6}; intMatrix[1] = new int[]{4, 5, 1, 9, 7};Code language: Java (java) And we can write that too: int[][] intMatrix = {{2, 3, 6}, {4, 5, 1, 9, 7}};Code language: Java (java) The 2D arrays created in the last two examples can be visualized as follows: The methods discussed in the previous sections – Arrays.fill(), Arrays.setAll(), clone(), System.arraycopy(), Arrays.copyOf(), and Arrays.copyOfRange() – can be applied to both levels of such a nested array.
🌐
Programiz
programiz.com › java-programming › multidimensional-array
Java Multidimensional Array (2d and 3d Array)
... class MultidimensionalArray { public static void main(String[] args) { // create a 2d array int[][] a = { {1, 2, 3}, {4, 5, 6, 9}, {7}, }; // calculate the length of each row System.out.println("Length of row 1: " + a[0].length); System.out.println("Length of row 2: " + a[1].length); ...
🌐
Blogger
javarevisited.blogspot.com › 2024 › 07 › how-to-create-2-dimensional-array-in.html
How to declare and initialize two dimensional Array in Java? Example Tutorial
July 12, 2024 - You can even declare a 2 dimensional array where each sub array has different lengths. You can also initialize the array at the time of declaration or on some later time by using nested for loop.
🌐
Hero Vired
herovired.com › learning-hub › topics › two-dimensional-array-in-java
Two-Dimensional Array in Java - Hero Vired
July 29, 2024 - ... The syntax declares a two-dimensional array with the name arrName that can store the object of class className in tabular form. Arrays are stored in contiguous memory locations, and numeric indexing references these memory locations.
🌐
iO Flood
ioflood.com › blog › 2d-array-java
2D Array in Java: Configuring Two-Dimensional Arrays
February 27, 2024 - This code creates a 2D array named array with three rows and three columns. The values of the array are initialized to the numbers 1 to 9. In this section, we’ve explored the fundamentals of arrays in Java, including what they are, why they are used, and how they work.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › two-dimensional array in java
Two-Dimensional Array in Java | Syntax & Example Program
September 2, 2025 - Learn how to use two-dimensional array in Java with syntax, examples, and step-by-step explanations. Covers primitive and object arrays in detail.
🌐
Baeldung
baeldung.com › home › java › java array › initializing arrays in java
Initializing Arrays in Java | Baeldung
December 16, 2024 - First, we indicate the second 2D array via its index [1]. Then, we add an element to the first column of the first row of that 2D array. Simply put, a three-dimensional array is an array of two-dimensional arrays.