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
This Java program demonstrates how to pass an array to a method. An integer array arr is declared and initialized in the main method. The sum() method is called with arr as an argument. Inside the sum() method, all array elements are added using a for loop. The final sum is then printed to the console. As usual, a method can also return an array. For example, the below program returns an array from method m1.
Published   May 8, 2026
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › arrays.html
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
One way to create an array is with the new operator. The next statement in the ArrayDemo program allocates an array with enough memory for 10 integer elements and assigns the array to the anArray variable.
Discussions

How do I declare and initialize an array in Java? - Stack Overflow
The number between the bracket says how large the new array will be and how much memory to allocate. For instance, if Java knows that the base type Type takes 32 bytes, and you want an array of size 5, it needs to internally allocate 32 * 5 = 160 bytes. You can also create arrays with the values already there, such as ... which not only creates the empty space but fills it with those values. Java can tell that the primitives are integers ... More on stackoverflow.com
🌐 stackoverflow.com
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
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 do I make integer lists in Java?
Java has arrays, like int[], but they are not dynamically resizable, and there are some other idiosyncracies as well. Instead prefer to use the generic List interface. Most often, you will want to specifically use the ArrayList implementation, as this gives you O(1) random access and is the Java equivalent to a Python list. Java generics have the caveat that you can't use them with primitive types like int, so instead you should use a wrapper class like Integer. tl;dr: List More on reddit.com
🌐 r/learnprogramming
4
0
February 4, 2019
🌐
Alvin Alexander
alvinalexander.com › blog › post › java › java-faq-create-array-int-example-syntax
Java ‘int’ array examples (declaring, initializing, populating) | alvinalexander.com
// (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]); } Sometimes it helps to see source code used in a complete Java program, so the following program demonstrates the ...
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 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 ...
🌐
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 with default values, you first declare the array variable using the syntax datatype[] arrayName = new datatype[length], where datatype is the type of data the array will hold, arrayName is the name of the array, and length ...
🌐
IIT Kanpur
iitk.ac.in › esc101 › 05Aug › tutorial › java › data › arraybasics.html
Creating and Using Arrays
public class ArrayDemo { public static void main(String[] args) { int[] anArray; // declare an array of integers anArray = new int[10]; // create an array of integers // assign a value to each array element and print for (int i = 0; i < anArray.length; i++) { anArray[i] = i; System.out.pri...
Find elsewhere
🌐
TutorialKart
tutorialkart.com › java › java-array › java-array-of-integers
Java Int Array
November 23, 2020 - 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. This will create an int array in memory, with all elements initialized to their corresponding static default value. The default value for an int is 0. Following is an example program to initialize an int array of size 10....
🌐
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"};
🌐
Java67
java67.com › 2019 › 10 › how-to-make-new-array-in-java.html
How to create a String or int Array in Java? Example Tutorial | Java67
If you want to make an array with values, then you need to first decide which type of array you want to create? e.g., do you want a String array, which can contain String objects like "abc," "def," or do you want to create an int array that contains int values like 10, 20, etc. In Java, you can create an array of any type, including primitives like byte, int, long, and objects like String, Integer, and other user-defined objects. Let's some code to make a new array in Java.
🌐
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.
🌐
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
🌐
TutorialsPoint
tutorialspoint.com › create-integer-array-with-array-newinstance-in-java
Create integer array with Array.newInstance in Java
Let us see a program to create an integer array with Array.newInstance with Java Reflection - ... 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); ...
🌐
Delft Stack
delftstack.com › home › howto › java › add integer to array java
How to Add Integers to an Array in Java | Delft Stack
February 2, 2024 - In this example, we start by importing the ArrayList class from the java.util package. We then create an instance of ArrayList called integerArray to store integers.
🌐
Jenkov
jenkov.com › tutorials › java › arrays.html
Java Arrays
Thus, in the example above with an array with 10 elements the indexes go from 0 to 9. You can access the length of an array via its length field. Here is an example: int[] intArray = new int[10]; int arrayLength = intArray.length;
🌐
Trinket
books.trinket.io › thinkjava › chapter8.html
Arrays | Think Java | Trinket
For example, if we set a[0] = 17.0, and then display b[0], the result is 17.0. Because a and b are different names for the same thing, they are sometimes called aliases. If you actually want to copy the array, not just a reference, you have to create a new array and copy the elements from the ...
🌐
Runestone Academy
runestone.academy › ns › books › published › apcsareview › ArrayBasics › abasics.html
8.1. Arrays in Java — AP CSA Java Review - Obsolete
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). ... Array elements are initialized to 0 if they are a numeric type (int or double), false if they are of type boolean, or null if they are an object type like String. Figure 2: Two 5 element arrays with their values set to the default values for integer ...
🌐
Igmguru
igmguru.com › blog › arrays-in-java
Arrays in Java: Declare, Define, and Access Array
May 11, 2026 - I have compiled some important techniques for declaring, initializing, and manipulating arrays in Java in this section. Here is a standard syntax for declaring an array - The statement int[] numbers declares an array called numbers. It can store ...
🌐
Baeldung
baeldung.com › home › java › java array › initializing arrays in java
Initializing Arrays in Java | Baeldung
December 16, 2024 - 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: