Arrays in Java are very useful. They store many values of the same type in one place. If you are learning Java, you must learn arrays. One of the first things to understand is how to initialize array Java.
This article explains 5 easy ways to do it. Each method comes with a simple example. Let’s get started.
1. Initialize Array Java Using Literal Syntax
This is the fastest and easiest way. You can create and fill the array in one line.
Syntax:
javaCopyEditint[] numbers = {1, 2, 3, 4, 5};
This creates an array of five integers. The array has values right from the start.
How it works:
- You define the type (
int[]
). - You use curly braces
{}
for values. - Values are stored in order:
1
at index 0,2
at index 1, and so on.
Example:
javaCopyEditpublic class Main {
public static void main(String[] args) {
String[] fruits = {"Apple", "Banana", "Mango"};
System.out.println(fruits[0]); // Output: Apple
}
}
This method works well when values are known ahead of time.
2. Initialize Array Java Using the ‘new’ Keyword
Use this method when you know the size but not the values.
Syntax:
javaCopyEditint[] numbers = new int[5];
This creates an array of size 5. All values will be zero by default.
Default values:
int
,long
,short
,byte
→ 0double
,float
→ 0.0char
→\u0000
(null character)boolean
→ false- Object types → null
Example:
javaCopyEditpublic class Main {
public static void main(String[] args) {
boolean[] flags = new boolean[3];
System.out.println(flags[0]); // Output: false
}
}
You can later assign values using indexes.
3. Initialize Array Java Using a Loop
Loops are useful when you want to fill an array step by step. You can use a for
loop.
Syntax:
javaCopyEditint[] numbers = new int[5];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = i + 1;
}
This fills the array with values from 1 to 5.
Example:
javaCopyEditpublic class Main {
public static void main(String[] args) {
int[] squares = new int[5];
for (int i = 0; i < squares.length; i++) {
squares[i] = (i + 1) * (i + 1);
}
System.out.println(squares[2]); // Output: 9
}
}
Use loops when values depend on logic or a formula.
4. Initialize Array Java Using Arrays.fill() Method
Java provides a utility class called Arrays
. It has a method called fill()
to set all elements.
Syntax:
javaCopyEditimport java.util.Arrays;
int[] numbers = new int[5];
Arrays.fill(numbers, 100);
This sets all values in the array to 100.
Example:
javaCopyEditimport java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] grades = new int[4];
Arrays.fill(grades, 70);
System.out.println(grades[1]); // Output: 70
}
}
This method is quick and saves time.
You can also fill only part of the array like this:
javaCopyEditArrays.fill(grades, 1, 3, 90);
This fills from index 1 to 2 with value 90.
5. Initialize Array Java Using Streams (Java 8 and Later)
If you use Java 8 or later, you can use streams. Streams make your code shorter.
Syntax:
javaCopyEditint[] numbers = java.util.stream.IntStream.range(1, 6).toArray();
This creates an array with values from 1 to 5.
Example:
javaCopyEditimport java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int[] numbers = IntStream.range(0, 10).toArray();
System.out.println(numbers[5]); // Output: 5
}
}
Streams are useful for creating patterns or ranges.
Bonus: Initializing Multi-Dimensional Arrays
Sometimes, you need more than one row or column. That’s when multi-dimensional arrays help.
Syntax:
javaCopyEditint[][] matrix = new int[2][3];
This creates a 2×3 array.
Filling Values:
javaCopyEditmatrix[0][0] = 10;
matrix[0][1] = 20;
Example:
javaCopyEditpublic class Main {
public static void main(String[] args) {
int[][] grid = {
{1, 2, 3},
{4, 5, 6}
};
System.out.println(grid[1][2]); // Output: 6
}
}
You can also use nested loops to fill them.
Which Method Should You Use?
- Use literal syntax when values are fixed and known.
- Use ‘new’ keyword when size is known, values are not.
- Use loops when values follow logic.
- Use Arrays.fill() to set all values quickly.
- Use streams for modern, short code.
Each method has its own use. Choose based on your need.
Final Thoughts
It is important to learn how to initialize array Java in different ways. Arrays are used often in coding. They store data, organize it, and make your programs efficient.
This article covered five easy ways to do it:
- Literal syntax
- ‘new’ keyword
- Loops
- Arrays.fill()
- Streams
Now you know how to initialize array Java in various ways. Try these in your own code. Keep practicing and you’ll get better with time.