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"};
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_arrays.asp
Java Arrays
Java Examples Java Videos Java ... Plan Java Interview Q&A ... Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ arrays-in-java
Arrays in Java - GeeksforGeeks
Java arrays can hold both primitive types (like int, char, boolean, etc.) and objects (like String, Integer, etc.)
Published ย  May 8, 2026
Discussions

How do I declare and initialize an array in Java? - Stack Overflow
This answer fails to properly address the question: "How do I declare and initialize an array in Java?" Other answers here show that it is simple to initialize float and int arrays when they are declared. There is no need to "Initialize later". More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
java - Difference between int[] array and int array[] - Stack Overflow
I have recently been thinking about the difference between the two ways of defining an array: int[] array int array[] Is there a difference? More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to make an array of integer arrays in Java? - Stack Overflow
I'm looking for something that looks like this if N = 3 and a,b,c are integers: ... This is not an array, but list. You might read this : https://www.geeksforgeeks.org/array-vs-arraylist-in-java/ More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ util โ€บ Arrays.html
Arrays (Java Platform SE 8 )
April 21, 2026 - Sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty. ... The sorting algorithm is a parallel sort-merge that breaks ...
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

๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ apcsareview โ€บ ArrayBasics โ€บ abasics.html
8.1. Arrays in Java โ€” AP CSA Java Review - Obsolete
When you create an array of a primitive type (like int) with initial values specified, space is allocated for the specified number of items of that type and the values in the array are set to the specified values. When you create an array of an object type (like String) with initial values, ...
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ java arrays โ€บ java arrays
Java arrays with Examples
April 24, 2025 - Thus, an array of integers contains only integers (int), an array of strings โ€” only strings, and an array of instances of a Dog class that we've created will contain only Dog objects.
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
new int[] says we want to immediately allocate memory to store data in this array. {1,2,3} is the data we want to store in the array. Java will count the elements in our initial array and automatically initialize the array with enough memory ...
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java array โ€บ initializing arrays in java
Initializing Arrays in Java | Baeldung
December 16, 2024 - This approach can be useful when we need to populate an array based on specific conditions or calculations. Letโ€™s look at an example that uses a loop-based approach: int[] array = new int [3]; for (int i = 0; i < array.length; i++) { array[i] ...
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ tutorial โ€บ java โ€บ nutsandbolts โ€บ arrays.html
Arrays (The Javaโ„ข Tutorials > Learning the Java Language > Language Basics)
See Java Language Changes for a ... enhancements, and removed or deprecated options for all JDK releases. An array is a container object that holds a fixed number of values of a single type....
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ how do i make integer lists in java?
r/learnprogramming on Reddit: How do I make integer lists in Java?
February 4, 2019 -

It seems a lot easier from the very little time I spent on Python before making the jump to Java. I'm taking a college night class on Java, and lists are one thing that are confusing me. How do they work in Java?

๐ŸŒ
Princeton CS
introcs.cs.princeton.edu โ€บ java โ€บ 14array
Arrays
April 8, 2020 - Write a program DiscreteDistribution.java that takes a variable number of integer command-line arguments and prints the integer i with probability proportional to the ith command-line argument. Write a code fragment Transpose.java to transpose a square two-dimensional array in place without creating a second array.
๐ŸŒ
Trinket
books.trinket.io โ€บ thinkjava โ€บ chapter8.html
Arrays | Think Java | Trinket
You can make an array of ints, doubles, or any other type, but all the values in an array must have the same type. To create an array, you have to declare a variable with an array type and then create the array itself. Array types look like other Java types, except they are followed by square ...
๐ŸŒ
Quora
quora.com โ€บ In-Java-what-is-an-integer-array
In Java, what is an integer array? - Quora
Answer (1 of 7): It depends on whether you are referring to [code ]int[/code] arrays or [code ]Integer[/code] arrays. While both hold a list of integers, the first holds the integers as 32-bit primitives- meaning that they are not stored as objects, but rather as numbers. The latter, however, hol...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ how to add something to primitive array in java?
r/learnprogramming on Reddit: How to add something to primitive array in Java?
December 2, 2015 -

Hi all, so I have an empty array I just created like this:

int[] array = new int[5];

How do I add numbers to it? I tried array.append() but it's not working. I don't want to do it manually like array[0] etc I want to just keep adding to the tail.

EDIT: I'm sure I won't go over the limit of what the array can contain. I just want to know how to add to the tail of the array without having to specify what position is being added.

๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-create-an-array-in-java
How to Create an Array in Java โ€“ Array Declaration Example
March 16, 2023 - Java arrays can store elements of any data type, including primitive types such as int, double, and boolean, as well as object types such as String and Integer.
๐ŸŒ
Jenkov
jenkov.com โ€บ tutorials โ€บ java โ€บ arrays.html
Java Arrays
A Java Array is a collection of variables of the same type. For instance, an array of int is a collection of variables of the type int. The variables in the array are ordered and each have an index.