data[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:

int[] 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
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-in-java
Arrays in Java - GeeksforGeeks
You can use array literals to initialize an array when declaring it. In this case, the new keyword is not required- ... The length of this array determines the length of the created array.
Published   May 8, 2026
🌐
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:
🌐
W3Schools
w3schools.com › java › java_arrays.asp
Java Arrays
Use new with a size when you want to create an empty array and fill it later. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
HappyCoders.eu
happycoders.eu › java › initialize-array-java
How to Initialize Arrays in Java
June 12, 2025 - As with one-dimensional arrays, all Java style guides prefer the first variant. A two-dimensional array can be initialized directly in the declaration – with new followed by the type and two pairs of square brackets:
🌐
Runestone Academy
runestone.academy › ns › books › published › apcsareview › ArrayBasics › abasics.html
8.1. Arrays in Java — AP CSA Java Review - Obsolete
To put a value in an array you give the name of the array and the index number in brackets and then an = and finally the value and a semicolon (highScores[0] = 99;). The first item in an array is at index 0. You can also initialize (set) the values in the array when you create it.
Find elsewhere
🌐
Sentry
sentry.io › sentry answers › java › how do i declare and initialize an array in java?
How do I declare and initialize an array in Java? | Sentry
April 15, 2023 - {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.
🌐
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).
🌐
Sentry
sentry.io › sentry answers › java › how to initialize an array in java?
How to initialize an array in Java? | Sentry
We can declare an array in the same way as any other variable or object in Java, by giving a type and a name: ... However, this merely declares the array, it does not initialize it.
🌐
Oracle
docs.oracle.com › javase › specs › jls › se7 › html › jls-10.html
Chapter 10. Arrays
March 16, 2026 - The Java Virtual Machine checks for such a situation at run time to ensure that the assignment is valid; if not, an ArrayStoreException is thrown. An array initializer may be specified in a declaration (§8.3, §9.3, §14.4), or as part of an array creation expression (§15.10), to create an array and provide some initial values.
🌐
Sentry
sentry.io › sentry answers › java › how to initialize an array in one line in java
How to initialize an array in one line in Java | Sentry
May 15, 2023 - You can specify the initial values for each item in an array using braces{...}. The code sample below is equivalent to the previous one. public class Main { public static void main(String[] args) { String[] words = {"java", "arrays", "are", ...
🌐
DataCamp
datacamp.com › doc › java › defining-and-initializing-arrays
Java Defining and Initializing Arrays
In this example, matrix is a two-dimensional array with 3 rows and 3 columns, initialized with values. Bounds Checking: Always ensure you access array elements within their bounds to avoid ArrayIndexOutOfBoundsException. Default Values: Remember that arrays in Java are initialized with default values (0 for numeric types, false for boolean, null for object references).
🌐
Coderanch
coderanch.com › t › 656499 › java › ways-initialize-array
Different ways to initialize an array (Beginning Java forum at Coderanch)
October 12, 2015 - You can see that so much better with arrays of reference types than of primitives. If you have code like this (from this post):-If you print that out with methods of the Arrays class you get the obvious output:-What you have here is a declaration and array initialiser later.
🌐
Reddit
reddit.com › r/javahelp › phil the java noob - help on declaring/initializing arrays
r/javahelp on Reddit: Phil the Java Noob - Help on declaring/initializing arrays
July 10, 2018 -

Me yet again! Arrays are really kicking my ass haha, can't seem to grasp them all that well.

Have a (seemingly) very basic question in regards to arrays:

" Return an int array length 3 containing the first 3 digits of pi, {3, 1, 4}."

What I have is this:

"public int[] makePi() {

int [3] arr;

int [1] arr;

int [4] arr;

}"

Can one of you kind souls explain to me what place(s) I'm going wrong? MOREOVER, can someone give me a brief rundown on how one declares arrays? Admittedly, my resources that I'm learning from aren't the best at explaining some concepts.

As always, thank you very much!!

🌐
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.
🌐
Software Testing Help
softwaretestinghelp.com › home › java tutorial for beginners: 100+ hands-on java video tutorials › java array – declare, create & initialize an array in java
Java Array - Declare, Create & Initialize An Array In Java
April 1, 2025 - Here, as you can see we have initialized the array using for loop. Each element ‘i’ of the array is initialized with value = i+1. Apart from using the above method to initialize arrays, you can also make use of some of the methods of ‘Arrays’ class of ‘java.util’ package to provide initial values for the array.
🌐
Study.com
study.com › business courses › java programming tutorial & training
Java: Initializing an Array | Study.com
To reference a bucket in the array you need to provide its index. Remember, Java starts counting at zero! Therefore, the first bucket will have an index of 0. Right now all buckets are zero. So, if we printed the following, the FOURTH bucket in highScores: ... Now that we've covered the general way to initialize an array, let's look at some more options.