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
An array is a collection of elements of the same data type stored in contiguous memory locations. It allows multiple values to be stored under a single name and accessed using an index.
Published: May 8, 2026
Discussions

How do I declare and initialize an array in Java? - Stack Overflow
Esta pregunta también tiene respuestas ... en java ... Before you post a new answer, consider there are already 25+ answers for this question. Please, make sure that your answer contributes information that is not among existing answers. ... Save this answer. ... Show activity on this post. You can either use array declaration ... More on stackoverflow.com
🌐 stackoverflow.com
ELI5: What is an Array in Java
If you imagine a normal variable as a box that can hold one single element, an array is a group of such boxes (under one single name) that each can hold one single element, where all boxes hold the same type of element and where each of the boxes has a unique number: the array index. You need to know how many "boxes" you need in advance - this is the array size that you need to know when declaring your array. "Normal variable": int x = 5; +-------+ int x ----> | 5 | +-------+ "Array": int[] x = new int[5]; +-----+-----+-----+-----+-----+ int x ----> | 0 | 0 | 0 | 0 | 0 | +-----+-----+-----+-----+-----+ Array index 0 1 2 3 4 More on reddit.com
🌐 r/javahelp
25
12
February 15, 2017
How do I determine whether an array contains a particular value in Java? - Stack Overflow
Yes, you can write those in 1 minute; but I still went over to StackOverflow expecting to find them somewhere in the JDK. ... Warning: this doesn't work for arrays of primitives (see the comments). More on stackoverflow.com
🌐 stackoverflow.com
[JAVA] Arrays. ELI5 please.
Think of an array kind of like the cabinets in your kitchen, all in a line. Each cabinet has a specific location in your kitchen. If you were to tell someone where to find the cups when they're over at your house, you might say "They're in the cabinet over the stove, on the left". Your cabinets are used for storage of your dishes and other kitchen items, and they can be organized a certain way to make finding what you need easier. A one dimensional array is essentially a line of "cabinets" that you can store data in, where each cabinet has its own index, or location, that you reference it by. An array can be unsorted or sorted, and many algorithms exist to sort them, with some being fast, and some requiring very little extra memory. Something like .length or .size simply returns the amount of cabinets in your kitchen, or in programming terms, the number of spaces in your array. You can also make a two dimensional array, which you can think of like a grid of cubby holes at a pre-school, or more mathematically, as a matrix. These work exactly the same as a 1D array, but each element has two indexes instead of one, like an X and Y coordinate on graph paper. An array is stored linearly in memory and its indexes usually start at zero. The following example is how to create an array and fill it with numbers using Java. int[] myArray = new int[10]; //Creates an array of size 10 to store integers for(int i = 0; i < myArray.length; i++) { myArray[i] = i; } //myArray.length = 10, i = 0, loop until i > 9 //On each iteration, myArray at index i will contain the current value of i //When this loop finishes, myArray will contain 0,1,2,3,4,5,6,7,8,9 in that order Arrays are the simplest data structure you will be dealing with, but they are incredibly useful and have a huge amount of applications. Many complex data structures use arrays in some way. For example, you can represent a graph structure as a matrix, or two dimensional array, of connected edges. You can efficiently sort data without using any extra memory using a heap structure, which at its most basic form is just an array that can be sorted in place. Once you grasp arrays, you will be able to grasp the more complex structures much more easily. If there is anything else I can help you with, please let me know. More on reddit.com
🌐 r/learnprogramming
14
3
May 13, 2013
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Arrays.html
Arrays (Java Platform SE 8 )
July 21, 2026 - Java™ Platform Standard Ed. 8 ... This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists. The methods in this class all throw a NullPointerException, if the specified array ...
🌐
CodeGym
codegym.cc › java blog › java arrays › java arrays
Java arrays with Examples
April 24, 2025 - An array is a data structure that stores elements of the same type. You can think of it as a set of numbered cells. You can put some data in each cell (one data element per cell).
🌐
Runestone Academy
runestone.academy › ns › books › published › apcsareview › ArrayBasics › abasics.html
8.1. Arrays in Java — AP CSA Java Review - Obsolete
An array is consecutive storage for multiple items of the same type. You can store a value in an array using an index (location in the array). You can get a value from an array using an index. An array is like a row of lockers, except that you can’t cram lots of stuff into it.
🌐
Opensource.com
opensource.com › article › 22 › 11 › arrays-java
Use arrays in Java | Opensource.com
In the Java programming language, an array is an ordered collection of data. You can use an array to store information in a structured way. It's useful to know the various ways you can retrieve that data when you need it.
Find elsewhere
Top answer
1 of 16
3264

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
335

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

🌐
Reddit
reddit.com › r/javahelp › eli5: what is an array in java
r/javahelp on Reddit: ELI5: What is an Array in Java
February 15, 2017 -

I keep reading over on the Oracle site and I just can't seem to understand it.

Top answer
1 of 4
43
If you imagine a normal variable as a box that can hold one single element, an array is a group of such boxes (under one single name) that each can hold one single element, where all boxes hold the same type of element and where each of the boxes has a unique number: the array index. You need to know how many "boxes" you need in advance - this is the array size that you need to know when declaring your array. "Normal variable": int x = 5; +-------+ int x ----> | 5 | +-------+ "Array": int[] x = new int[5]; +-----+-----+-----+-----+-----+ int x ----> | 0 | 0 | 0 | 0 | 0 | +-----+-----+-----+-----+-----+ Array index 0 1 2 3 4
2 of 4
2
There are already good explanations in other comments, but here is a copypaste of a previous answer of mine in a similar thread: Arrays are data structures of a particular data or object type, that join together several consecutive memory storage locations of that same type conveniently into one variable, the individual memory locations of which can be referenced and accessed using a numerical index. A single variable of a basic data type or object type can only store and reference one value of that type. So one variable of type "int" called "measurement" can store one int value, like the value "10" this way: int measurement = 10; Now in case we need to store and reference five different measurements, without using arrays we would need to declare five distinct variables of type "int", all named differently: int measurementOne = 10; int measurementTwo = 11; int measurementThree = 12; int measurementFour = 13; int measurementFive = 14; Using arrays, we can declare instead an array variable of type "int" called "measurements" that is of the length 5: int[] measurements = new int[5]; And then we can store our five measurement values into this single int array variable by using the numerical array index in square brackets: measurements[0] = 10; measurements[1] = 11; measurements[2] = 12; measurements[3] = 13; measurements[4] = 14; This way we can use five distinct int storage locations without having to declare and name each one of them separately. Note that the numeric array index references are always zero-based, that is the valid indexes for our array of length 5 were 0 through 4, not 1 through 5.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-create-an-array-in-java
How to Create an Array in Java – Array Declaration Example
March 16, 2023 - In Java, you can create multi-dimensional arrays with two or more dimensions. A two-dimensional array is an array of arrays, while a three-dimensional array is an array of arrays of arrays, and so on. To create a two-dimensional array in Java, you first declare the array variable using the syntax datatype[][] arrayName, where datatype is the type of data the array will hold, and arrayName is the name of the array.
🌐
Oracle
docs.oracle.com › javase › specs › jls › se7 › html › jls-10.html
Chapter 10. Arrays
March 16, 2026 - In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2).
🌐
Trinket
trinket.io › thinkjava › chapter8.html
Arrays | Think Java
Downloads are no longer available. Export tools were offered on the site in the months before shutdown, but they went offline with the rest of the service.
🌐
CodeSignal
codesignal.com › learn › courses › mastering-complex-data-structures-in-java › lessons › understanding-and-using-arrays-in-java
Understanding and Using Arrays in Java
Arrays are a core part of Java programming, allowing you to define collections of elements of the same type. There are two primary ways to create arrays in Java: using array literals and using constructors with the new keyword.
🌐
Igmguru
igmguru.com › home › blog › java › arrays in java
Arrays in Java: Declare, Define, and Access Array
2 days ago - Java Arrays allow you to store multiple values of the same type in a single variable. It is important to understand them for efficient coding whether you are building a simple or complex application.
🌐
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....
🌐
Princeton CS
introcs.cs.princeton.edu › java › 14array
Arrays
April 8, 2020 - We refer to an array element by putting its index in square brackets after the array name: the code a[i] refers to element i of array a[]. For example, the following code makes an array of n numbers of type double, all initialized to 0: ArrayExamples.java contains typical examples of using ...
🌐
The Knowledge Academy
theknowledgeacademy.com › blog › java-array
Java Array: A Complete Guide With Examples
March 31, 2023 - Arrays in Java are a fundamental data structure that allow you to store multiple values of the same type in a single variable. They provide a convenient way to manage and manipulate collections of data, whether you're dealing with numbers, strings, ...
🌐
DataCamp
datacamp.com › doc › java › common-array-operations
Java Common Array Operations
Java keywordsIntroduction To JavaJava File HandlingJava Language BasicsJava ArraysJava Object-Oriented Programming ... Arrays are a fundamental data structure in Java, used to store multiple values of the same type in a single variable.
🌐
Study.com
study.com › business courses › java programming tutorial & training
What is an Array in Java? | Study.com
August 3, 2026 - A Java array is a sequence of values, each of the same type. It can be a simple list, a matrix/table (2-dimensional), or a 3d matrix (3-dimensional array). An array is a single unit, even though it is made up of any number of data elements.
🌐
Baeldung
baeldung.com › home › java › java array › arrays in java: a reference guide
Arrays in Java: A Reference Guide | Baeldung
July 24, 2024 - According to the Java documentation, an array is an object containing a fixed number of values of the same type. The elements of an array are indexed, which means we can access them with numbers (called indices).