This is array initializer syntax, and it can only be used on the right-hand-side when declaring a variable of array type. Example:

int[] x = {1,2,3,4};
String[] y = {"a","b","c"};

If you're not on the RHS of a variable declaration, use an array constructor instead:

int[] x;
x = new int[]{1,2,3,4};
String[] y;
y = new String[]{"a","b","c"};

These declarations have the exact same effect: a new array is allocated and constructed with the specified contents.

In your case, it might actually be clearer (less repetitive, but a bit less concise) to specify the table programmatically:

double[][] m = new double[4][4];

for(int i=0; i<4; i++) {
    for(int j=0; j<4; j++) {
        m[i][j] = i*j;
    }
}
Answer from nneonneo on Stack Overflow
๐ŸŒ
Huda Tutorials
hudatutorials.com โ€บ java โ€บ basics โ€บ java-arrays โ€บ java-double-array
Java double Array - double Array in Java
May 18, 2024 - In this tutorial you can learn how to declare Java double Array, how to assign values to Java double Array and how to get values from Java double Array.
๐ŸŒ
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
How to initialize a global array in which number of columns varies? eg: int a[][] = new int[3][]; How to initialize it if I don't know the number of columns at present?ReplyDelete ... Hello @Akshay, you can do this with two dimensional array where just first index is mandatory but with one dimensional array you must specify number of items. You can see my post 6 ways to declare two dimensional array in Java for more details.Delete
๐ŸŒ
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 - When you initialize a 2D array, you must always specify the first dimension(no. of rows), but providing the second dimension(no. of columns) may be omitted. Java compiler is smart enough to manipulate the size by checking the number of elements ...
๐ŸŒ
Codecademy
codecademy.com โ€บ learn โ€บ learn-java โ€บ modules โ€บ java-two-dimensional-arrays โ€บ cheatsheet
Learn Java: Two-Dimensional Arrays Cheatsheet | Codecademy
In Java, initializer lists can be used to quickly give initial values to 2D arrays. This can be done in two different ways. If the array has not been declared yet, a new array can be declared and initialized in the same step using curly brackets.
๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ apcsareview โ€บ Array2dBasics โ€บ a2dDAS.html
10.3. Declaring 2D Arrays โ€” AP CSA Java Review - Obsolete
When arrays are created their contents are automatically initialized to 0 for numeric types, null for object references, and false for type boolean. To explicitly put a value in an array you give the name of the array followed by the row index in brackets followed by the column index in brackets ...
๐ŸŒ
Know Program
knowprogram.com โ€บ java โ€บ double-array-java
Double Array in Java - Know Program
Know Program | Learn to Code for Free. Java Tutorial, Program, and Quiz, C Tutorial, Program, and Quiz. Learn Oracle database, JDBC.
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ multidimensional-array
Java Multidimensional Array (2d and 3d Array)
And also, unlike C/C++, each row of the multidimensional array in Java can be of different lengths. ... 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); System.out.println("Length of row 3: " + a[2].length); } } ... In the above example, we are creating a multidimensional array named a.
Find elsewhere
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ java/util โ€บ arrays.fill() method in java
Java Arrays fill(double[], double) Method
September 1, 2008 - Using fill(double[], double) method, we're filling the array with a give value and then updated array elements are printed again. package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { // initializing double array double arr[] = new double[] { 10.0, 20.0, 15.0 }; ...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ 2d-array-in-java-two-dimensional-and-nested-arrays
2D Array in Java โ€“ Two-Dimensional and Nested Arrays
August 10, 2022 - To create a two dimensional array in Java, you have to specify the data type of items to be stored in the array, followed by two square brackets and the name of the array.
๐ŸŒ
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
This is the key to writing an algorithm involving multi-dimensional arrays like looping through a 2D array, searching elements, etc. Now, that you know what is a 2D or two-dimensional array in Java, let's see a couple of examples of how to create and initialize a 2D array.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ java โ€บ how to initialize an array in java?
How to initialize an array in Java? | Sentry
In Java, the array object is used to store multiple elements of the same type. These elements are stored in contiguous memory, the length of which is immutable once initialization is complete.
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ arrays
Java Array (With Examples)
In Java, here is how we can declare an array. ... Here, data is an array that can hold values of type double.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java array โ€บ initializing arrays in java
Initializing Arrays in Java | Baeldung
December 16, 2024 - These values represent the array elementsโ€™ initial state before we explicitly assign any values. Arrays of int, short, long, float, and double data types set all elements to zero by default:
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ multidimensional-arrays-in-java
Java Multi-Dimensional Arrays - GeeksforGeeks
Then print the multi-dimensional array and close the scanner object. Example: Java program to demonstrate how to create Two Dimensional Array with User input.
Published ย  November 13, 2025
๐ŸŒ
Software Testing Help
softwaretestinghelp.com โ€บ home โ€บ java โ€บ multidimensional arrays in java (2d and 3d arrays in java)
MultiDimensional Arrays In Java (2d and 3d Arrays In Java)
April 1, 2025 - Each element in the above 2d array ... value. You already know that when initializing the 2d array, you can initialize the individual elements of the array to a value....
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];