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"};
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ arrays-in-java
Arrays in Java - GeeksforGeeks
In Java, an array is declared by specifying the data type, followed by the array name, and empty square brackets []. ... When an array is declared, only a reference is created.
Published: 4 weeks ago
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_arrays.asp
Java Arrays
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
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

๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ tutorial โ€บ java โ€บ nutsandbolts โ€บ arrays.html
Arrays (The Javaโ„ข Tutorials > Learning the Java Language > Language Basics)
You can also declare an array of arrays (also known as a multidimensional array) by using two or more sets of brackets, such as String[][] names. Each element, therefore, must be accessed by a corresponding number of index values. In the Java programming language, a multidimensional array is an array whose components are themselves arrays.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-create-an-array-in-java
How to Create an Array in Java โ€“ Array Declaration Example
March 16, 2023 - Arrays in Java are zero-indexed, which means that the first element in an array has an index of 0, the second element has an index of 1, and so on. The length of an array is fixed when it is created and cannot be changed later.
๐ŸŒ
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
1 month ago - ... class Main { public static void main(String[] args) { // declare the variable and allocate memory int[] myArray = new int[]{1, 2, 3}; for (int i = 0; i < 3; i++) { System.out.println(myArray[i]); } } } ... If you donโ€™t want to hard code ...
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ arrays
Java Array (With Examples)
March 21, 2024 - Here, the array can store 10 elements. We can also say that the size or length of the array is 10. In Java, we can declare and allocate the memory of an array in one single statement.
Find elsewhere
๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ apcsareview โ€บ ArrayBasics โ€บ abasics.html
8.1. Arrays in Java โ€” AP CSA Java Review - Obsolete
If the array hasnโ€™t been created ... any object yet). Try the the following. To create an array use the new keyword, followed by a space, then the type, and then in square brackets the size of the array (the number of elements it can hold)....
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java array โ€บ initializing arrays in java
Initializing Arrays in Java | Baeldung
December 16, 2024 - But we need to know the size when we initialize it because the Java virtual machine must reserve the contiguous memory block for it. As we discussed earlier, we can initialize an array with the new keyword: ... If we want to resize the array, we can do it by creating an array of larger size ...
๐ŸŒ
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 - The array is created first with a specific size, and values are assigned later. int[] numbers = new int[5]; // Array of size 5 numbers[0] = 10; numbers[1] = 20; numbers[2] = 30; numbers[3] = 40; numbers[4] = 50; ... int[] arr = new int[5]; for ...
๐ŸŒ
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-create-array-of-objects-in-java
How to Create Array of Objects in Java? - GeeksforGeeks
A member function of the respective class is created and that is used to assign the initial values to the objects. ... // Java program to demonstrate initializing // an array of objects using a method class GFG { public static void main(String args[]) { // Declaring an array of student Student[] arr; // Allocating memory for 2 objects // of type student arr = new Student[2]; // Creating actual student objects arr[0] = new Student(); arr[1] = new Student(); // Assigning data to student objects arr[0].setData(1, "Sai"); arr[1].setData(2, "Omm"); // Displaying the student data System.out.println(
Published: July 15, 2025
๐ŸŒ
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.
๐ŸŒ
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)
3 weeks ago - 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.
๐ŸŒ
Medium
medium.com โ€บ @pies052022 โ€บ how-to-declare-array-in-java-use-creating-types-and-class-d2d52b4c49d3
How to Declare Array in Java โ€” Use, Creating, Types, and Class | by JOKEN VILLANUEVA | Medium
October 3, 2025 - The Arrays in Java is a container object that holds a set number of values of the same type. When an array is created, its size is decided.
๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ csjava โ€บ Unit7-Arrays โ€บ topic-7-1-array-basics.html
7.1. Array Creation and Access โ€” CS Java
Arrays and lists in most programming ... in Java โ€“ the first character is at index 0. When we declare a variable, we specify its type and then the variable name. To make a variable into an array, we put square brackets after the data type. This data type will be for all the elements in the array.
๐ŸŒ
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 - In our previous tutorial, we discussed the basics of arrays in Java along with the different concepts associated with arrays which we will learn in detail in this tutorial series. The first and foremost step with arrays is to create them. You need to be aware of what type of elements and how many elements you are going to store in arrays.
๐ŸŒ
HappyCoders
happycoders.eu โ€บ home โ€บ java โ€บ how to declare and initialize arrays in java
How to Declare and Initialize Arrays in Java
September 24, 2026 - We do not yet specify the size of the array in the declaration. The array variables (in the example intArray and stringArray) do not contain the array itself but a reference to an array, which is ultimately an object on the Java heap. Only when we create the array object itself do we have to define its size.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ java-array-how-to-declare-and-initialize-an-array-in-java-example
Java Array โ€“ How to Declare and Initialize an Array in Java Example
February 4, 2022 - In Java, you use an array to store multiple values of the same data type in one variable. You can also see it as a collection of values of the same data type.