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"};
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:

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

🌐
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
class Main { public static void main(String[] args) { // declare 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]); } } } ... This will create ...
Discussions

How to make an array of integer arrays in Java? - Stack Overflow
I have been trying to make an array of integer arrays. I know that the outer array will have length N whilst every integer array within in only needs to hold two values. Originally, I made an Arra... More on stackoverflow.com
🌐 stackoverflow.com
[Java] Why can't we create an array of type T?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnprogramming
11
8
July 30, 2023
Phil the Java Noob - Help on declaring/initializing arrays
The first step in your solution should be to declare and create an array that can hold three digits/integers, which would look something like: int[] pi = new int[3]; That statement declares an a integer array variable named pi, and assigns a new array (with a length of three) to that variable. An integer array is initially filled with all zeros, so you've got to fill it with the values that you want. In other words, you need to put 3 in the first index (index 0), 1 in the second index, and 4 in the third index. This should look something like: pi[0] = 3; // assign 3 to first index pi[1] = 1; pi[2] = 4; Now that the array is filled, your method should return the array, so that it can be used by whoever calls your makePi() method: return pi; There are several ways to declare an initialize arrays. A more concise way of doing it would be: int[] pi = {3, 1, 4}; or int[] pi = new int[]{3, 1, 4}; More on reddit.com
🌐 r/javahelp
9
3
July 10, 2018
How to create an array and fill it with a specific range of numbers?
I know how to declare an array ... an int array with a specific set of numbers (i.e. 1 to 100). I've tried searching elsewhere and couldn't find anything. I've also read the rules of the sub. If I'm breaking a rule here, I'm sorry; it's unintentional. ... Archived post. New comments cannot ... More on reddit.com
🌐 r/learnjava
23
28
December 7, 2020
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › arrays.html
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
The following program, ArrayDemo, creates an array of integers, puts some values in the array, and prints each value to standard output. class ArrayDemo { public static void main(String[] args) { // declares an array of integers int[] anArray; // allocates memory for 10 integers anArray = new int[10]; // initialize first element anArray[0] = 100; // initialize second element anArray[1] = 200; // and so forth anArray[2] = 300; anArray[3] = 400; anArray[4] = 500; anArray[5] = 600; anArray[6] = 700; anArray[7] = 800; anArray[8] = 900; anArray[9] = 1000; System.out.println("Element at index 0: " +
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-in-java
Arrays in Java - GeeksforGeeks
The primitive array stores integer values and is traversed using a loop. The non-primitive array stores String objects and is printed in the same way using its length property. ... 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. Memory is allocated using the new keyword by specifying the array size.
Published   May 8, 2026
🌐
IIT Kanpur
iitk.ac.in › esc101 › 05Aug › tutorial › java › data › arraybasics.html
Creating and Using Arrays
anArray = new int[10]; // create an array of integers In general, when creating an array, you use the new operator, plus the data type of the array elements, plus the number of elements desired enclosed within brackets—[ and ]. new elementType[arraySize] If the new statement were omitted ...
🌐
W3Schools
w3schools.com › java › java_arrays.asp
Java Arrays
You can also create an array by specifying its size with new.
🌐
Alvin Alexander
alvinalexander.com › blog › post › java › java-faq-create-array-int-example-syntax
Java ‘int’ array examples (declaring, initializing, populating) | alvinalexander.com
Depending on your needs you can also create an int array with initial elements like this: // (1) define your java int array int[] intArray = new int[] {4,5,6,7,8}; // (2) print the java int array for (int i=0; i<intArray.length; i++) { System.out.println(intArray[i]); }
Find elsewhere
🌐
Hostman
hostman.com › tutorials › how-to-create-an-array-in-java
How to initialize an array in Java – Array Declaration tutorial
To initialize an array, use the keyword new, followed by the data type and the number of elements in the array. ... This code will create an array named numbers with 5 elements of type integer. Multi-dimensional arrays store and manipulate data in a table or spreadsheet format.
🌐
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 and copying the previous array elements to the new array:
🌐
Igmguru
igmguru.com › blog › arrays-in-java
Arrays in Java: Declare, Define, and Access Array
May 11, 2026 - Each value is accessed by referring to its specific index within the array. Here is a Java program that tells you how to create an integer array, assign values to it and display each element on the console.
🌐
Trinket
books.trinket.io › thinkjava › chapter8.html
Arrays | Think Java | Trinket
The following method creates an int array and fills it with random numbers between 0 and 99. The argument specifies the size of the array, and the return value is a reference to the new array.
🌐
TutorialKart
tutorialkart.com › java › java-array › java-array-of-integers
Java Int Array
November 23, 2020 - ... You can use any of these two notations. To initialize an integer array, you can assign the array variable with new integer array of specific size as shown below. ... You have to mention the size of array during initialization.
🌐
TutorialsPoint
tutorialspoint.com › create-integer-array-with-array-newinstance-in-java
Create integer array with Array.newInstance in Java
import java.lang.reflect.Array; public class Example { public static void main(String[] args) { int[] arr = (int[]) Array.newInstance(int.class, 3); // creates a new array Array.set(arr, 0, 5); Array.set(arr, 1, 9); Array.set(arr, 2, 2); System.out.println("Element 1 = " + Array.get(arr, 0)); ...
🌐
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; Ideal for larger arrays or sequential ...
🌐
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 - Therefore, that array object is of size three. This method work for objects as well. If we wanted to initialize an array of three Strings, we would do it like this: ... int[] intArray = new int[]{13, 14, 15}; String[] stringArray = new String[]{"zelda", "link", "ganon"};
🌐
freeCodeCamp
freecodecamp.org › news › how-to-create-an-array-in-java
How to Create an Array in Java – Array Declaration Example
March 16, 2023 - To create an array in a single statement, you first declare the type of the array, followed by the name of the array, and then the values of the array enclosed in curly braces, separated by commas.
🌐
xperti
xperti.io › home › a comprehensive guide to initializing arrays in java
Complete Guide: How to Initialize an Array in Java with Values
April 15, 2024 - The following example shows an integer type array of size six and initializes it with six non-default values. int[] array = new int[6]; array[0] = 8; array[1] = 6; array[2] = 4; array[3] = 2; array[4] = 1; array[5] = 9;
🌐
HappyCoders.eu
happycoders.eu › java › initialize-array-java
How to Initialize Arrays in Java
June 12, 2025 - int[] intArray; // . . . intArray = new int[10];Code language: Java (java) The various array types have the following default values: The default value for the integers byte, short, int, and long is 0. The default value for the floating point ...