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

🌐
Baeldung
baeldung.com › home › java › java array › initializing arrays in java
Initializing Arrays in Java | Baeldung
December 16, 2024 - Additionally, when using the var keyword for type inference, we cannot initialize an array literal without the new keyword: ... The code above results in a compilation error. 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:
Discussions

How to initialize an array in Java? - Stack Overflow
Input: 10//array size 10 20 30 40 50 60 71 80 90 91 ... OP clearly is trying to assign a bunch of values at once. Your response doesn't acknowledge this fact. 2021-02-18T00:12:37.25Z+00:00 ... You cannot initialize an array like that. 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
[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
Two dimensional ArrayList, need help with constructor
There are probably better ways to solve your problem, but here is the answer to your question: List> list1 = new ArrayList>(); for (int i = 0; i < 10; i++) { List list2 = new ArrayList(); for (int j = 0; j < 10; j++) { list2.add(i * j); } list1.add(list2); } Integer value = list1.get(1).get(5); More on reddit.com
🌐 r/java
5
0
February 17, 2012
🌐
DataCamp
datacamp.com › doc › java › defining-and-initializing-arrays
Java Defining and Initializing Arrays
You can initialize an array at the time of declaration using curly braces {} with the values separated by commas. dataType[] arrayName = {value1, value2, ..., valueN}; int[] numbers = {1, 2, 3, 4, 5}; String[] names = {"Alice", "Bob", "Charlie"}; In this example, the numbers array is initialized ...
🌐
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.
Published   May 8, 2026
🌐
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
If you don’t want to hard code the data immediately when you initialize the array, you can instead create an empty array (with all values initialized to zero) as follows. ... Here, you have to explicitly give the size of the array. In our example, we’ve created an array that can store 3 ints, all of which are zero.
🌐
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:
🌐
Pluralsight
pluralsight.com › blog › software development
How to initialize an array in Java | Pluralsight
To go back to our earlier example, it’s like if you put a box inside a box, so you could more easily organize your things. Java has a special syntax for this: ... This creates an array of size 10, with each element holding a nested array of size 10 themselves. You can also initialize these by content, like before.
Find elsewhere
🌐
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 ... 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 ...
🌐
freeCodeCamp
freecodecamp.org › news › java-array-declaration-how-to-initialize-an-array-in-java-with-example-code
Java Array Declaration – How to Initialize an Array in Java with Example Code
September 9, 2021 - You can now go ahead and put values ... main(String[] args) { // write your code here String [] names = new String[3]; names[0] = "Quincy"; names[1] = "Abbey"; names[2] = "Kolade"; } }...
🌐
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 - Each element ‘i’ of the array is initialized with value = i+1. Apart from using the above method to initialize arrays, you can also make use of some of the methods of ‘Arrays’ class of ‘java.util’ package to provide initial values ...
🌐
Hostman
hostman.com › tutorials › how-to-create-an-array-in-java
How to initialize an array in Java – Array Declaration tutorial
This initializes the numbers array with 5 elements, all assigned the value of 1. Java also provides the copyOf method for initializing arrays to copy an existing array and specify the desired length of the new array.
🌐
freeCodeCamp
freecodecamp.org › news › java-array-how-to-declare-and-initialize-an-array-in-java-example
Java Array – How to Declare and Initialize an Array in Java Example
February 4, 2022 - We have initialized our arrays by passing in values with the same data type with each value separated by a comma. If we wanted to access the elements/values in our array, we would refer to their index number in the array. The index of the first element is 0. Here is an example:
🌐
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 - For example, the initial value of an Integer array is 0 for each member, the initial value of a Boolean array is False, and the initial value of a string type array is empty strings.
🌐
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 - To declare an array, specify the data type followed by square brackets [] and the array name. This only declares the reference variable. The array memory is allocated when you use the new keyword or assign values. Complete working Java example that demonstrates declaring, initializing, and ...
🌐
Study.com
study.com › business courses › java programming tutorial & training
Java: Initializing an Array | Study.com
Larger, more complex arrays require other code to fill the values. In our previous example, it's fairly easy to pre-fill some values for the high scores (although the players of our fictional game might not be happy about that!). Another way is to enclose the values within curly brackets. In this case, we didn't use the default initialization because we are going to fill the values right away: ... We don't need to tell the Java the size of the array.
🌐
Diginatives
diginatives.io › home › 5 easy ways to initialize array java with examples
5 Easy Ways to Initialize Array Java with Examples - Diginatives
April 12, 2025 - You can also fill only part of the array like this: ... This fills from index 1 to 2 with value 90. If you use Java 8 or later, you can use streams.
🌐
Scaler
scaler.com › home › topics › how to initialize an array in java?
How to Initialize an Array in Java? - Scaler Topics
September 7, 2023 - ... The output of this code is: The program declares an array of integers called num with a length of 5 using the new keyword. Since no values are passed during initialization, all elements of the array are set to their default value of
🌐
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 - To declare an array in Java, you specify the type of elements it will hold, followed by square brackets. ... This line declares an array of integers but doesn’t allocate memory yet. ... If you know how many elements the array will hold but not their values yet, you can initialize it with a fixed size.
🌐
Medium
medium.com › @pies052022 › how-to-initialize-an-array-in-java-with-example-7b184ab0a983
How to Initialize an Array in Java with Example | by JOKEN VILLANUEVA | Medium
November 27, 2025 - The first process is the new keyword, which is to initialize the values one by one. The second process which is to put a values in a curly braces. We can declare the array with the use of the syntax below: ... dataType: This is the type of data we want to put in the array. It should be a string, integer, double, and etc. [ ]: It defines that the variable to declare will consist of an array in any values. nameOfArrary: This to define as an array identifier.