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 - In the code above, we declare a two-dimensional array of int data type by specifying two sets of square brackets. Let’s initialize the array and specify the number of rows and columns: ... We can think of this as an array with two rows and five columns. Let’s assign value to the individual elements using nested indices, where the first index represents the row and the second index represents the column:
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
[Java] Question about initializing all elements in an array to a specific given number.
In Java that array is on the heap, so if you change its elements in the function you’ll change it everywhere. No need to return anything, void is fine More on reddit.com
🌐 r/learnprogramming
4
1
March 4, 2021
Java Initialize an int array in a constructor - Stack Overflow
Instance variables automatically ... default values are all zeroes. If you had locally declared array though they you would need to initialize each element. ... Save this answer. ... Show activity on this post. The best way is not to write any initializing statements. This is because if you write int a[]=new int[3] then by default, in Java all the values ... More on stackoverflow.com
🌐 stackoverflow.com
Why no default values in ArrayList ??
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://i.imgur.com/EJ7tqek.png ) 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
9
2
October 30, 2023
🌐
Sentry
sentry.io › sentry answers › java › how to initialize an array in java?
How to initialize an array in Java? | Sentry
We can declare an array in the same way as any other variable or object in Java, by giving a type and a name: ... However, this merely declares the array, it does not initialize it. The array has the value null. Let’s take a look at the two ways we can initialize our array. If we already know the element values we want stored in the array, we can initialize the array like this: ... This example is functionally equivalent to the previous example that declared and initialized our array separately. Interestingly, the square brackets that symbolize an array [] can be put on either side of the array name during the declaration with no change in functionality:
🌐
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 default value for an integer type array is 0; for a boolean type array, it is false; and for a string type array, it is empty text. It must have occurred to you that there are no values given.
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-in-java
Arrays in Java - GeeksforGeeks
You can use array literals to initialize an array when declaring it. In this case, the new keyword is not required- ... The length of this array determines the length of the created array.
Published   May 8, 2026
🌐
HappyCoders.eu
happycoders.eu › java › initialize-array-java
How to Initialize Arrays in Java
June 12, 2025 - Instead of defining an array with specific values, we can also initialize an array by specifying a size (see also the article Array Length in Java), e.g., an array with ten int elements:
🌐
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 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.
Find elsewhere
🌐
Techie Delight
techiedelight.com › home › java › declare and initialize arrays in java
Declare and initialize arrays in Java | Techie Delight
1 week ago - Here’s the syntax: Type[] arr = new Type[] { comma separated values }; For example, the following code creates a primitive integer array of size 5 using a new operator and array initializer.
🌐
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!!

🌐
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 - The slow way to initialize your array with non-default values is to assign values one by one: ... In this case, you declared an integer array object containing 10 elements, so you can initialize each element using its index value.
🌐
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 - Ideal for larger arrays or sequential ... 2, 3, 4, 5 } Java 8 introduced IntStream to initialize integer arrays efficiently. 1. Using IntStream.range(start, end): generates values from start (inclusive) to end (exclusive)...
🌐
Software Testing Help
softwaretestinghelp.com › home › java tutorial for beginners: 100+ hands-on java video tutorials › java array – declare, create & initialize an array in java
Java Array - Declare, Create & Initialize An Array In Java
April 1, 2025 - Arrays can be initialized using new or by assigning comma-separated values enclosed in curly braces. So when we initialize an array using Array literal as shown below. You do not need to use new ones.
🌐
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 the ...
🌐
Reddit
reddit.com › r/learnprogramming › [java] question about initializing all elements in an array to a specific given number.
r/learnprogramming on Reddit: [Java] Question about initializing all elements in an array to a specific given number.
March 4, 2021 -

I just wanted want someone to check my code , I have a midterm coming tomorrow.

Q). Write a method called arrayInit(int[] a,int initValue) to initialize all elements of an integer array "a" to initValue. What return type should arrayInit() have?

ANS:

public int[] arrayInit(int[] a, int initValue){
for (i=0; i<a.length; i++){
a[i] = initValue;
return a;
}

I am not sure how to return the array? or what type the return should have tho it has to return the array i guess?

🌐
Javadevnotes
javadevnotes.com › java-initialize-array-examples
Java Initialize Array Examples - JavaDevNotes
December 25, 2015 - If the size of the array you wish to initialize is fairly small and you know what values you want to assign, you may declare and initialize an array in one statement. Below shows an example on how to do it in 4 ways: import java.util.Arrays; /** * A Simple Example that Declares And Initialise A Java Array In One Go. */ public class InitializeJavaArray { public static void main(String[] args) { int[] testArray1 = {1, 2, 3, 4, 5}; int testArray2[] = {6, 7, 8, 9, 10}; int[] testArray3 = new int[] {11, 12, 13, 14, 15}; int testArray4[] = new int[] {16, 17, 18, 19, 20}; System.out.println(Arrays.toString(testArray1)); System.out.println(Arrays.toString(testArray2)); System.out.println(Arrays.toString(testArray3)); System.out.println(Arrays.toString(testArray4)); } }
🌐
Pluralsight
pluralsight.com › blog › software development
How to initialize an array in Java | Pluralsight
Right now, though, the array is just a container filled with empty slots waiting to be filled. How do we get that data in there? To add content to your array, just reference the array name, specify the slot in question, and then add the value like so: ... Say you already know the content you want in your array beforehand, so you want to skip the whole separation of initialization and filling it, and just have it all in one single, elegant line of code. In Java, you can supply said data up-front, and Java will just infer the size, so you don’t even need to specify it.
🌐
Hostman
hostman.com › tutorials › how-to-create-an-array-in-java
How to initialize an array in Java – Array Declaration tutorial
Next, you should know how to initialize an array in Java. To do this, specify an array size in square brackets after the data type indicating the number of elements the array contains. ... A new array object with a length of 10 is assigned to the variable myArray.
🌐
Scaler
scaler.com › home › topics › how to initialize an array in java?
How to Initialize an Array in Java? - Scaler Topics
September 7, 2023 - how to initialize an array without assigning values, for this we pass the size to the square braces([]). Doing this, java will assign the default value 0 to each element of the array in the case of an int array.
🌐
SoloLearn
sololearn.com › home › coding languages › how to initialize arrays in java?
How To Initialize Arrays In Java? - Sololearn
March 7, 2023 - To initialize an array means to add items to the array. So to initialize the int array we declared in the last section, we do this: ... In the code above, we added four integers to the int array — 2, 4, 6, 8.