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
Tip: The shortcut syntax is most often used when the values are known at the start. 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
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-in-java
Arrays in Java - GeeksforGeeks
Syntax: int arr[] = new int[size]; Once an array is created, its size is fixed and cannot be changed. For collections that can grow or shrink dynamically, Java provides classes like ArrayList or Vector.
Published   May 8, 2026
Discussions

How do I declare and initialize an array in Java? - Stack Overflow
Both the outer arrays and the inner arrays (and those in between, if they exist) are just regular arrays. 2017-07-18T15:19:10.57Z+00:00 ... Perhaps worth noting that the second (less-preferred) form stems from C/C++. This might be the reason why it is supported in Java. More on stackoverflow.com
🌐 stackoverflow.com
Can you explain to me like I’m five about what indexes and arrays are in Java?
An array is an ordered grouping of objects of a similar type. The index is where in the grouping an object exists. Think of a book as the array and the pages as the objects in the array. The index is the page number. *Note: in Java array indexes start at 0. So page 1 of the book array would be at index 0. More on reddit.com
🌐 r/javahelp
14
19
October 13, 2019
Java Arrays
Arrays are just a convenient way of storing multiple things. Think of arrays as sacks of balls - you could have just a single ball: int myBall = 42; Or five: int myBall1 = 42; int myBall2 = 43; int myBall3 = 44; int myBall4 = 45; int myBall5 = 46; But what if you want, say, a hundred balls? Things get out of hand rather quickly. Instead of having these loose balls lying around, we can have an array of balls: int[] balls1 = new int[100]; // Holds a hundred balls int[] balls2 = new int[5]; // Holds five balls But wait, there's more! Arrays work like lockers - each "slot" in an array has an index. The first one in an array is always at index 0, the second one at index 1 and so on. To populate the five-item array, we do this: int[] balls = new int[5]; balls[0] = 42; balls[1] = 43; balls[2] = 44; balls[3] = 45; balls[4] = 46; We can retrieve them just as well: int thirdBall = balls[2]; // Now thirdBall is 44 Why use arrays, then, if we can just declare as many variables as we need? Well, it makes life a lot easier. Say you want a hundred numbers and you want them to run from 1 to 100. You could do it like this: int num1 = 1; int num2 = 2; ... int num100 = 100; Or, you could do this: int[] nums = new int[100]; for(int i = 0; i < 100; ++i) nums[i] = i + 1; Now, after all this: what specifically you have trouble understanding? More on reddit.com
🌐 r/learnjava
11
10
March 29, 2017
[Java] How are arrays implemented in Java.
when you can't do memory allocation or pointer arithmetic Java, the language, can't do pointer arithmetic. Java, the implementation of that language, most certainly can. More on reddit.com
🌐 r/learnprogramming
35
54
March 14, 2014
People also ask

How to slice an array in Java?
We use Arrays.copyOfRange(array, startIndex, endIndex) method to copy a range of array elements into a new array.
🌐
srijaninstitute.com
srijaninstitute.com › home › blog › java array tutorial complete guide
Java Arrays Tutorial (Syntax, Examples, Slicing & Best Practices 2026)
Can you sort a 2d array in Java?
Sure, you can Java sort array rows or columns, but if you want to Java sort 2d array, then you need to write your own code for that.
🌐
srijaninstitute.com
srijaninstitute.com › home › blog › java array tutorial complete guide
Java Arrays Tutorial (Syntax, Examples, Slicing & Best Practices 2026)
How do we get the length of the Java array?
The property arr. length obtains the Java array length, which helps to avoid index errors while iterating or manipulating.
🌐
srijaninstitute.com
srijaninstitute.com › home › blog › java array tutorial complete guide
Java Arrays Tutorial (Syntax, Examples, Slicing & Best Practices 2026)
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › arrays.html
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
You can also declare an array of arrays (also known as a multidimensional array) by using two or more sets of brackets, such as String[][] names. Each element, therefore, must be accessed by a corresponding number of index values. In the Java programming language, a multidimensional array is ...
🌐
Igmguru
igmguru.com › blog › arrays-in-java
Arrays in Java: Declare, Define, and Access Array
May 11, 2026 - It can store multiple integer values. At this stage, the array exists as a variable, but no memory has been allocated yet for the actual elements. The array must later be initialized with a size or assigned values to use it. Here is a standard syntax for initializing an array -
🌐
Tutorialspoint
tutorialspoint.com › java › java_arrays.htm
Java - Arrays
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables.
🌐
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. In other words, Java won't let us put an integer in the first cell of the array, a String in the second, and a Dog in the third.
Find elsewhere
🌐
Medium
yuvrajscorpio.medium.com › arrays-in-java-be5bad06099a
Arrays in Java. Check out the full Java series here. | by Yuvraj Kevit | Medium
September 3, 2025 - Syntax: dataType[] arrayName; Example: int[] arr; OR // String[] cars; int arr[]; Creating an array: int[] arr = new int[10]; // To define the array's capacity, use the keyword new
🌐
Srijan Institute
srijaninstitute.com › home › blog › java array tutorial complete guide
Java Arrays Tutorial (Syntax, Examples, Slicing & Best Practices 2026)
February 18, 2026 - Arrays are zero-based, so the first element is always accessed at index 0. Java Array elements are much faster to access than other structure elements because of the contiguous memory.
🌐
Syntaxdb
syntaxdb.com › ref › java › arrays
Arrays in Java - SyntaxDB - Java Syntax Reference
Arrays are initialized with a size. The array index range is 0 to the initialized size minus 1. This means that the first index in the array is 0, and the last is size minus 1. Trying to access an index outside the index range will result in an error.
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

🌐
Scaler
scaler.com › home › topics › java › java arrays
Understanding Array in Java - Scaler Topics
April 8, 2024 - Arrays in Java are non-primitive data types that store elements of a similar data type in the memory. Arrays in Java can store both primitive and non-primitive types of data in it. Array declaration is the process of defining an array variable with a specific data type and syntax; it does not ...
🌐
Jenkov
jenkov.com › tutorials › java › arrays.html
Java Arrays
Here are a few more Java array declaration examples: ... The first line declares an array of String references. The second line declares an array of references to objects of the class MyClass, which symbolizes a class you have created yourself. You actually have a choice about where to place the square brackets [] when you declare an array in Java.
🌐
Programiz
programiz.com › java-programming › arrays
Java Array (With Examples)
Java Arrays initialization · Note: ... element of an array using the index number. Here is the syntax for accessing elements of an array, // access array elements array[index] Let's see an example of accessing array elements using index numbers....
🌐
Medium
medium.com › @pies052022 › how-to-declare-array-in-java-use-creating-types-and-class-d2d52b4c49d3
How to Declare Array in Java — Use, Creating, Types, and Class | by JOKEN VILLANUEVA | Medium
October 3, 2025 - How to Declare Array in Java — Use, Creating, Types, and Class The Java Arrays are used to store more than one value in a single variable, instead of declaring a separate variable for each …
🌐
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
{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 for three ints.
🌐
Runestone Academy
runestone.academy › ns › books › published › apcsareview › ArrayBasics › abasics.html
8.1. Arrays in Java — AP CSA Java Review - Obsolete
To declare an array specify the type of elements that will be stored in the array, then ([ ]) to show that it is an array of that type, then at least one space, and then a name for the array. Note that the declarations below just name the variable and say what type of array it will reference.
🌐
W3Schools
w3schools.com › java › java_ref_arrays.asp
Java Arrays Reference
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... The Java Arrays class (found in java.util), has methods that allow you to manipulate arrays.
🌐
PW Skills
pwskills.com › blog › java › array-java
Array Java: Declare, Define, and Access Array
However, knowing the exact index is important to yield correct output. The syntax of an array in Java is data_type [ ] variable_name = new int [ ]. Check out the article to know how to declare a multidimensional array in Java.
🌐
The Knowledge Academy
theknowledgeacademy.com › blog › java-array
Java Array: A Complete Guide With Examples
April 9, 2026 - Syntax: There is no direct method to split Arrays in Java, but one can achieve this by using System.arraycopy() or manual iteration.
🌐
Codecademy
codecademy.com › docs › java › arrays
Java | Arrays | Codecademy
October 23, 2023 - Beginner Friendly.Beginner Friendly17 hours17 hours · Array variables are defined with a type followed by square brackets []. Next, the actual elements are comma-separated and surrounded by curly brackets {}: