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:

int[] 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:

String[] 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.

String[] 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 [].
Published   2 weeks ago
🌐
W3Schools
w3schools.com › java › java_arrays.asp
Java Arrays
We have now declared a variable that holds an array of strings. To insert values to it, you can place the values in a comma-separated list, inside curly braces { }:
Top answer
1 of 16
3264

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:

int[] 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:

String[] 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.

String[] myStringArray;
myStringArray = new String[]{"a", "b", "c"};
2 of 16
336

There are two types of array.

One Dimensional Array

Syntax for default values:

int[] num = new int[5];

Or (less preferred)

int num[] = new int[5];

Syntax with values given (variable/field initialization):

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

Or (less preferred)

int 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

int[][] num = new int[5][2];

Or

int num[][] = new int[5][2];

Or

int[] num[] = new int[5][2];

Initialization

 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

 int[][] num={ {1,2}, {1,2}, {1,2}, {1,2}, {1,2} };

Ragged Array (or Non-rectangular Array)

 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:

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

For Accessing:

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

Alternatively:

for (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)
Like declarations for variables of other types, an array declaration has two components: the array's type and the array's name. An array's type is written as type[], where type is the data type of the contained elements; the brackets are special symbols indicating that this variable holds an array.
🌐
DataCamp
datacamp.com › doc › java › defining-and-initializing-arrays
Java Defining and Initializing Arrays
There are several ways to initialize an array in Java. You can initialize an array at the time of declaration using curly braces {} with the values separated by commas.
🌐
Runestone Academy
runestone.academy › ns › books › published › apcsareview › ArrayBasics › abasics.html
8.1. Arrays in Java — AP CSA Java Review - Obsolete
If you want to keep track of the ... the names. To declare an array specify the type of elements that will be stored in the array, then ([ ]) to show that it is an array of that type, then at least one space, and then a name for the array....
🌐
freeCodeCamp
freecodecamp.org › news › how-to-create-an-array-in-java
How to Create an Array in Java – Array Declaration Example
March 16, 2023 - In this example, we declare a two-dimensional array of booleans named flags with 2 rows and 3 columns. Since we haven't specified any values, each element in the array is initialized with the default value of false. Multi-dimensional arrays are useful when you need to store data in a table or grid-like structure, such as a chess board or a spreadsheet. Creating arrays is a fundamental skill in Java programming.
Find elsewhere
🌐
CodeGym
codegym.cc › java blog › java arrays › java arrays
Java arrays with Examples
April 24, 2025 - In other words, Java won't let us put an integer in the first cell of the array, a String in the second, and a Dog in the third. Like any variable, an array must be declared in Java.
🌐
Programiz
programiz.com › java-programming › arrays
Java Array (With Examples)
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.
🌐
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 - There are two ways you can declare and initialize an array in Java. The first is with the new keyword, where you have to initialize the values one by one.
🌐
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
In the example below, we use Integer ... the variable and allocate memory Integer[] myArray = new Integer[3]; myArray[1] = 100; for (int i = 0; i < 3; i++) { System.out.println(myArray[i]); } } }...
🌐
Dev.java
dev.java › learn › creating-arrays-in-your-programs
Creating Arrays in Your Programs
Launching simple source-code Java programs with the Java launcher. ... jshell interactively evaluates declarations, statements, and expressions of the Java programming language in a read-eval-print loop (REPL).
🌐
JanBask Training
janbasktraining.com › community › java › how-do-i-declare-and-initialize-an-array-in-java
How do I declare and initialize an array in Java? | JanBask Training Community
May 21, 2025 - This line declares an array of integers but doesn’t allocate memory yet. ... If you know how many elements the array will hold but not their values yet, you can initialize it with a fixed size. ... This creates an array that can hold 5 integers. All elements are automatically initialized to 0 (the default for int). ... This is called array literal initialization. Java automatically calculates the array size based on the number of values.
🌐
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 - To declare an array, specify the data type followed by square brackets [] and the array name. This only declares the reference variable. The array memory is allocated when you use the new keyword or assign values.
🌐
Runestone Academy
runestone.academy › ns › books › published › csjava › Unit7-Arrays › topic-7-1-array-basics.html
7.1. Array Creation and Access — CS Java
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. // Declaration for a single int variable int score; // Declaration for an array of ints int[] scores;
🌐
Baeldung
baeldung.com › home › java › java array › initializing arrays in java
Initializing Arrays in Java | Baeldung
December 16, 2024 - In the code above, we initialize a numbers array to hold seven elements of int type. After initialization, we can assign values to individual elements using their indices: ... Here, we add 10 and 20 to indices 0 and 1, respectively. Furthermore, we can retrieve an element value by using its index: ... In the code above, we assert that the element in index one is 20. It’s possible to declare and initialize an array in a single step:
🌐
Trinket
books.trinket.io › thinkjava › chapter8.html
Arrays | Think Java | Trinket
You can make an array of ints, doubles, or any other type, but all the values in an array must have the same type. To create an array, you have to declare a variable with an array type and then create the array itself. Array types look like other Java types, except they are followed by square ...
🌐
Quora
quora.com › How-do-you-declare-an-array-globally-in-Java
How to declare an array globally in Java - Quora
Answer (1 of 9): That’s not possible in Java, or at least the language steers you away from attempting that. Global variables have significant disadvantages in terms of maintainability, so the language itself has no way of making something truly global. The nearest approach would be to abuse so...
🌐
CodeSignal
codesignal.com › learn › courses › mastering-complex-data-structures-in-java › lessons › understanding-and-using-arrays-in-java
Understanding and Using Arrays in Java
Arrays are a core part of Java ... literals and using constructors with the new keyword. Array Literals: An array can be declared and initialized with specific values directly using an array literal....
🌐
Rheinwerk Computing
blog.rheinwerk-computing.com › working-with-arrays-in-java
Working with Arrays in Java
October 3, 2022 - The declaration has two types of information at its core: that it “is an array” and that it will “store elements of type int.” · The square brackets are tokens, so whitespace isn’t mandatory for separation. Another valid example is the following: ... Placing the square brackets before the variable is also syntactically valid but not at all recommended: double []prices; // Violation of the usual Java ...