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
For non primitive types, references to items are stored at contiguous locations. The first element of the array is at index 0. After creating an array, its size is fixed; we can not change it.
Published   2 weeks ago
Top answer
1 of 16
3263

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

🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-array-size-explained-by-example
Java array size, length and loop examples
The 32-bit Java int can go to a maximum of 2,147,483,647, so that is the theoretical maximum Java array size. However, virtual machines for different operating systems may not allocate every available bit to the elements in the Java array. Thus, the maximum number of elements a Java array can hold is typically a little less than the upper limit of a Java int. In Java, once you set the Java array size it is fixed, and it can’t be changed.
🌐
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
🌐
Pluralsight
pluralsight.com › tech insights & how-to guides › software development
How to initialize an array in Java | Pluralsight
When you make an array in Java for the first time, you’ve got to declare its size. This is because the array’s size is fixed from the moment you create it. The simplest way to do this is to initialize an empty array of a declared size.
🌐
Coderanch
coderanch.com › t › 411644 › java › declare-size-Array-java
Why cant we declare the size of Array in java? (Beginning Java forum at Coderanch)
August 28, 2008 - Once you create the 'house' with five bedrooms via the "new int[5]", you can now write the address on the paper. so the reason "int[2] array;" is wrong is because the reference doesn't know about the size of the array... it just needs to know it will point to an array that holds 'int's. There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors ... One more think I observe, When a programmer has a strong background of C and C++, the code : int arr[5]; seems to be perfectly legal and good to him, but in Java arrays are handled as object so new has to be ther to make it sense ..
🌐
Educative
educative.io › answers › how-to-resize-an-array-in-java
How to resize an array in Java
Line 7: We copy the elements of ... newArray using the arraycopy() method. We have copied only the last 5 elements from oldArray into newArray. Line 8: We set oldArray to null, freeing up the memory it was using. Line 10: We print out the contents of newArray. The java.util.Arrays class has a method named copyOf(), which can also be used to increase or decrease the size of an array...
🌐
Baeldung
baeldung.com › home › java › java array › initializing arrays in java
Initializing Arrays in Java | Baeldung
December 16, 2024 - Arrays have a fixed size, determined during initialization, that cannot be altered during runtime. In this tutorial, we’ll see how to declare an array. Also, we’ll examine the different ways we can initialize an array and the subtle differences between them. A simple and complete reference guide to understanding and using Arrays in Java...
Find elsewhere
🌐
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
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. Here’s a runnable example of dynamically setting the values in an array.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-create-an-array-in-java
How to Create an Array in Java – Array Declaration Example
March 16, 2023 - Array declaration with default values is a way to create an array in Java and initialize it with default values of the specified data type. This approach is useful when you need to create an array with a fixed size, but you don't have specific values to initialize it with.
🌐
Quora
quora.com › Can-we-able-to-change-the-size-of-the-array-during-runtime-in-Java
Can we able to change the size of the array during runtime in Java? - Quora
Answer (1 of 2): No, you can't and that's why we use linked list. But there is a concept called dynamic arrays. In these arrays whenever the number of elements stored reaches the initial capacity of the array a new array is created and all the ...
🌐
LabEx
labex.io › tutorials › java-how-to-create-java-arrays-with-fixed-size-418028
How to create Java arrays with fixed size | LabEx
This tutorial explores the essential techniques for creating and using fixed-size arrays in Java. Arrays are fundamental data structures that allow you to store multiple elements of the same type in a single variable.
🌐
TutorialsPoint
tutorialspoint.com › can-you-change-size-of-array-in-java-once-created
Can you change size of Array in Java once created?
July 2, 2020 - Thus the size of the array is determined at the time of its creation or, initialization once it is done you cannot change the size of the array.
🌐
Quora
quora.com › How-can-we-create-array-without-specifying-a-size-in-java
How can we create array without specifying a size in java? - Quora
Answer (1 of 11): Yes. You can, technically, declare the type of a variable as an array (of specific type) like int[ ] a; But you can't use it before initializing it. This array ‘a’ doesn't exist anywhere in the memory. The compiler only knows that ‘a’ will be an array.
🌐
W3Schools
w3schools.com › java › ref_arrays_length.asp
Java Array length Property
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... The length property returns the length of an array.
🌐
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 - In contrast to several other programming languages, a Java array’s size is fixed upon startup. This implies that once the array is constructed, its size cannot be altered. You can’t just suddenly make additional room in your memory later on; you have to set aside a certain amount of space for the furnishings.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-declare-and-initialize-an-array-in-java
How to Initialize an Array in Java?
October 11, 2025 - int[] numbers = new int[5]; // Array of size 5 numbers[0] = 10; numbers[1] = 20; numbers[2] = 30; numbers[3] = 40; numbers[4] = 50; ... int[] arr = new int[5]; for (int i = 0; i < arr.length; i++) { arr[i] = i + 1; // Fills array with 1, 2, ...