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
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 ...
Published   May 8, 2026
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

Discussions

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
in terms of memory, any difference between an int array with 1 element, and 1 int ?
Depends on the language. In C, no. In most other languages I think the answer is "yes", because an array is a special data type that (usually) knows how long it is and that requires extra memory. More on reddit.com
🌐 r/learnprogramming
53
80
January 29, 2023
[Java][LeetCode Easy] Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. How does this solution work?
When i is 1, diff is 5 because nums[1] is 1. m.get(5) returns 2, so the indexes are 1 and 2. More on reddit.com
🌐 r/learnprogramming
5
3
August 10, 2022
🌐
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 ...
🌐
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 ...
🌐
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.
🌐
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!!

🌐
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 - Although this method is not the ... using another array is to create a new array with a larger size, copy the elements from the original array to the new one, and then add the desired integers....
Find elsewhere
🌐
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
🌐
TutorialKart
tutorialkart.com › java › java-array › java-array-of-integers
Java Int Array
November 23, 2020 - Following is the syntax to declare an Array of Integers in Java. ... 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.
🌐
Alvin Alexander
alvinalexander.com › blog › post › java › java-faq-create-array-int-example-syntax
Java ‘int’ array examples (declaring, initializing, populating) | alvinalexander.com
*/ private void intArrayExample2() { int[] intArray = new int[] {4,5,6,7,8}; System.out.println("intArray output (version 2)"); for (int i=0; i<intArray.length; i++) { System.out.println(intArray[i]); } } } As a quick summary, if you were looking for examples of the Java int array syntax, I ...
🌐
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.
🌐
Medium
nishanthmekala.medium.com › understanding-java-array-initialization-new-int-vs-shorthand-syntax-1dff81b16cc3
Understanding Java Array Initialization: new int[] vs Shorthand Syntax | by Nishanth Mekala | Medium
June 27, 2025 - When initializing an array, you can specify its values directly. Let’s break down the two syntaxes: The syntax new int[]{-1, 0, 1, 0} explicitly creates an array using the new keyword.
🌐
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.
🌐
Trinket
books.trinket.io › thinkjava › chapter8.html
Arrays | Think Java | Trinket
Array types look like other Java types, except they are followed by square brackets ([]). For example, the following lines declare that counts is an “integer array” and values is a “double array”: To create the array itself, you have to use the new operator, which we first saw in Section ...
🌐
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 ...
🌐
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 - 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.
🌐
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;
🌐
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 ...
🌐
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;