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"};
🌐
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 ...
🌐
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
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
How to add something to primitive array in Java?
Allocate a new bigger array, copy existing values over, add the new one. You probably don't really want to be using an array for what you're doing if you're doing this a lot. More on reddit.com
🌐 r/learnprogramming
15
1
December 2, 2015
How do you return an array in a method?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. 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/javahelp
26
15
April 22, 2021
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[3]; myArray[1] = 100; for (int i = 0; i < 3; i++) { System.out.println(myArray[i]); } } } ... You can use the same syntax ...
🌐
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.
🌐
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....
🌐
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!!

Find elsewhere
🌐
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.
🌐
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"};
🌐
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
🌐
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.
🌐
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.
🌐
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)); ...
🌐
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.
🌐
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.
🌐
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 ...
🌐
Oyoclass
docs.oyoclass.com › javaeditor › core › arrays
Arrays - Java Editor Documentation
int[] arr2 = new int[10]; //This makes an array that can hold 10 integers.
🌐
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 ...
🌐
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;