Use with AIdata[10] = {10,20,30,40,50,60,71,80,90,91};

The above is not correct (syntax error). It means you are assigning an array to data[10] which can hold just an element.

If you want to initialize an array, try using Array Initializer:

Use with AIint[] data = {10,20,30,40,50,60,71,80,90,91};

// or

int[] data;
data = new int[] {10,20,30,40,50,60,71,80,90,91};

Notice the difference between the two declarations. When assigning a new array to a declared variable, new must be used.

Even if you correct the syntax, accessing data[10] is still incorrect (You can only access data[0] to data[9] because index of arrays in Java is 0-based). Accessing data[10] will throw an ArrayIndexOutOfBoundsException.

Answer from Prasoon Saurav on Stack Overflow
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java array โ€บ initializing arrays in java
Initializing Arrays in Java | Baeldung
December 16, 2024 - Additionally, when using the var keyword for type inference, we cannot initialize an array literal without the new keyword: ... The code above results in a compilation error. To use the var keyword with array literals, we must introduce the new keyword, allowing the compiler to infer the type from the right-hand side of the assignment: ... Here, the compiler can infer itโ€™s an array of integers. The java.util.Arrays class has several methods named fill() that accept different types of arguments and fill the whole array with the same value:
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ arrays-in-java
Arrays in Java - GeeksforGeeks
When an array is declared and initialized in the same statement, Java allows the array initializer syntax without explicitly writing new int[].
Published: October 8, 2026
๐ŸŒ
Pluralsight
pluralsight.com โ€บ blog โ€บ software development
How to initialize an array in Java | Pluralsight
When you make an array in Java for the first time, youโ€™ve got to declare its size. This is because the arrayโ€™s size is fixed from the moment you create it. The simplest way to do this is to initialize an empty array of a declared size.
๐ŸŒ
HappyCoders
happycoders.eu โ€บ home โ€บ java โ€บ how to declare and initialize arrays in java
How to Declare and Initialize Arrays in Java
September 24, 2026 - A two-dimensional array in Java is an array of arrays: each row is a one-dimensional array of its own. You declare it with two pairs of square brackets and initialize it with values or with a fixed size:
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ java โ€บ defining-and-initializing-arrays
Java Defining and Initializing Arrays
To define an array in Java, you specify the data type of its elements followed by square brackets. You can define an array without initializing it immediately.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ java โ€บ how to initialize an array in java?
Java Array Initialization Methods and Examples | Sentry
September 25, 2026 - Initialize Java arrays using known values with curly braces or by specifying length to create default-filled arrays, with type-specific defaults for all data types
๐ŸŒ
Opensource.com
opensource.com โ€บ article โ€บ 19 โ€บ 10 โ€บ initializing-arrays-java
Initializing arrays in Java | Opensource.com
October 22, 2019 - The int[] to the extreme left declares the type of the variable as an array (denoted by the []) of int. To the right is the name of the variable, which in this case is ia. Next, the = tells us that the variable defined on the left side is set to whatโ€™s to the right side. To the right of the = we see the word new, which in Java indicates that an object is being initialized, meaning that storage is allocated and its constructor is called (see here for more information).
Find elsewhere
Top answer
1 of 16
3265

You can either use array declaration or array literal (but only when you declare and affect the variable right away, array literals cannot be used for re-assigning an array).

For primitive types:

Use with AIint[] myIntArray = new int[3]; // each element of the array is initialised to 0
int[] myIntArray = {1, 2, 3};
int[] myIntArray = new int[]{1, 2, 3};

// Since Java 8. Doc of IntStream: https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html

int [] myIntArray = IntStream.range(0, 100).toArray(); // From 0 to 99
int [] myIntArray = IntStream.rangeClosed(0, 100).toArray(); // From 0 to 100
int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).toArray(); // The order is preserved.
int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).sorted().toArray(); // Sort 

For classes, for example String, it's the same:

Use with AIString[] myStringArray = new String[3]; // each element is initialised to null
String[] myStringArray = {"a", "b", "c"};
String[] myStringArray = new String[]{"a", "b", "c"};

The third way of initializing is useful when you declare an array first and then initialize it, pass an array as a function argument, or return an array. The explicit type is required.

Use with AIString[] myStringArray;
myStringArray = new String[]{"a", "b", "c"};
2 of 16
335

There are two types of array.

One Dimensional Array

Syntax for default values:

Use with AIint[] num = new int[5];

Or (less preferred)

Use with AIint num[] = new int[5];

Syntax with values given (variable/field initialization):

Use with AIint[] num = {1,2,3,4,5};

Or (less preferred)

Use with AIint num[] = {1, 2, 3, 4, 5};

Note: For convenience int[] num is preferable because it clearly tells that you are talking here about array. Otherwise no difference. Not at all.

Multidimensional array

Declaration

Use with AIint[][] num = new int[5][2];

Or

Use with AIint num[][] = new int[5][2];

Or

Use with AIint[] num[] = new int[5][2];

Initialization

Use with AI num[0][0]=1;
 num[0][1]=2;
 num[1][0]=1;
 num[1][1]=2;
 num[2][0]=1;
 num[2][1]=2;
 num[3][0]=1;
 num[3][1]=2;
 num[4][0]=1;
 num[4][1]=2;

Or

Use with AI int[][] num={ {1,2}, {1,2}, {1,2}, {1,2}, {1,2} };

Ragged Array (or Non-rectangular Array)

Use with AI int[][] num = new int[5][];
 num[0] = new int[1];
 num[1] = new int[5];
 num[2] = new int[2];
 num[3] = new int[3];

So here we are defining columns explicitly.
Another Way:

Use with AIint[][] num={ {1}, {1,2}, {1,2,3,4,5}, {1,2}, {1,2,3} };

For Accessing:

Use with AIfor (int i=0; i<(num.length); i++ ) {
    for (int j=0;j<num[i].length;j++)
        System.out.println(num[i][j]);
}

Alternatively:

Use with AIfor (int[] a : num) {
  for (int i : a) {
    System.out.println(i);
  }
}

Ragged arrays are multidimensional arrays.
For explanation see multidimensional array detail at the official java tutorials

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-declare-and-initialize-an-array-in-java
How to Declare and Initialize an Array in Java - GeeksforGeeks
October 11, 2025 - int[] arr = new int[5]; for (int i = 0; i < arr.length; i++) { arr[i] = i + 1; // Fills array with 1, 2, 3, 4, 5 } Java 8 introduced IntStream to initialize integer arrays efficiently.
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ java array โ€บ declare and initialize an array in java (with examples)
Declare and Initialize an Array in Java (With Examples)
October 10, 2026 - Declaring an array in Java means writing the element type, square brackets and a name, such as int[] scores, and initializing it means creating the array object, either with new and a length or with a list of values in curly braces.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ java โ€บ how do i declare and initialize an array in java?
Declare and Initialize Arrays in Java with Examples | Sentry
October 2, 2026 - {1,2,3} is the data we want to store in the array. Java will count the elements in our initial array and automatically initialize the array with enough memory for three ints.
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ java โ€บ java-array โ€บ initialize-array-in-java
How to Initialize Array in Java?
September 5, 2026 - Do not try to resize an existing Java array; creating a larger array creates a different array object. An array stored in an object field can be initialized inside the class constructor.
๐ŸŒ
JavaBeat
javabeat.net โ€บ initialize-array-java
How to Initialize an Array in Java
June 25, 2025 - We can use the square bracket syntax to initialize an array with specific values. To do that, we need to initialize each array index one by one. This approach works fine if the array size is small; however, this approach is not recommended for large arrays. Letโ€™s learn how this approach works ...
๐ŸŒ
Techies Grow
techiesgrow.com โ€บ home โ€บ how to initialize an array in java (clear guide with examples)
How To Initialize an Array in Java (Clear Guide With Examples)
February 16, 2026 - 1. Declaration You tell Java that a variable will hold a reference to an array of a certain type: ... At this point, no array object exists yet. The variable value is null. 2. Creation You allocate the actual array object with a fixed length: ... This reserves space for the elements. The size is now locked. You cannot grow or shrink this array. 3. Element initialization When an array is created, Java automatically gives each element a default value:
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ how-to-declare-and-initialize-an-array-in-java
How to Declare and Initialize an Array in Java
September 20, 2022 - Note that we've only created an array reference. No memory has been allocated to the array as the size is unknown, and we can't do much with it. To use the array, we can initialize it with the new keyword, followed by the data type of our array, and rectangular brackets containing its size:
๐ŸŒ
NavTutorial
navtutorial.com โ€บ home โ€บ code snippets โ€บ how to declare and initialize an array in java: 11 ways with examples
Declare and Initialize an Array in Java (11 Ways)
July 11, 2026 - If you know how many elements you need but not their values yet, you can allocate the array with a specific size. Java will fill it with default values automatically: ... If you already know the values, you can use a literal (curly-brace) initializer.
๐ŸŒ
Diginatives
diginatives.io โ€บ home โ€บ 5 easy ways to initialize array java with examples
5 Easy Ways to Initialize Array Java with Examples
August 7, 2026 - Learn how to initialize array Java the easy way. This guide covers literal syntax, the new keyword, loops, Arrays.fill(), and Streams with code examples, a comparison table, and FAQs.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ java-array-declaration-how-to-initialize-an-array-in-java-with-example-code
Java Array Declaration โ€“ How to Initialize an Array in Java with Example Code
September 9, 2021 - The basic syntax for initializing an array in this way looks like this: ... The size is usually expressed with a numberic value. It signifies how many values you want to hold in the array. Its value is immutable, meaning you wonโ€™t be able to put more than the number specified as the size in the array. You can now go ahead and put values in the array like this: package com.kolade; import java.util.Arrays; public class Main { public static void main(String[] args) { // write your code here String [] names = new String[3]; names[0] = "Quincy"; names[1] = "Abbey"; names[2] = "Kolade"; } }
๐ŸŒ
Hostman
hostman.com โ€บ tutorials โ€บ how-to-create-an-array-in-java
How to initialize an array in Java โ€“ Array Declaration tutorial
To create a 2D array with 3 rows ... row. Typically, arrays are declared with a fixed size and are initialized with specific values โ€‹โ€‹at declaration time....