• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us

How to Initialize an Array in Java

June 25, 2025 //  by Anees Asghar

An array in Java is a collection of elements having the same data type and occupying contiguous memory locations. An array in Java can be declared similar to a variable, but with a couple of square brackets “dataType[] arrayName”. Once an array is declared, we can initialize it using different methods. 

In Java, arrays are indexed with a numeric value, by default, starting from 0, the second element stored at the 1st index, and so on. These index numbers can be used to access the elements of an array. 

In this write-up, we’ll show you how to initialize an array in Java using square brackets, curly braces, and the Stream interface methods.

How to Initialize an Array in Java Using Square Brackets []

In Java, we can initialize an array with square brackets, as shown in the following syntax:

dataType [] arrayName = new dataType [arraySize];

By default, this syntax will create an array of the specified length and initialize it with the default value. The default values depend on the specified data type, as illustrated in the following table:

Data Type Default Value
Integer0
Float0.0
Double0.0
Byte 0
Short0
Long0
BooleanFalse
Char‘\u0000′ (i.e., Null)
ObjectNull 

Example 1: Initializing an Array With Default Values

In this code, we create arrays of different data types and initialize them with their respective default values:

public class InitializeArrays {
    public static void main(String[] args) {
Integer [] intArray = new Integer [3];
Boolean [] boolArray = new Boolean [2];
String [] strArray = new String [3];
  }
}

In this code, we create three arrays of type Integer, Boolean, and String, respectively. Each array will be initialized according to the specified data type and array length, like this:

[0, 0, 0] //output of intArray
[false, false] //output of boolArray
[null, null, null] //Output of strArray

Example 2: Initializing an Array With Specific Values

We can use the square bracket syntax to initialize an array with specific values. To do that, we need to initialize each array index one by one. This approach works fine if the array size is small; however, this approach is not recommended for large arrays. Let’s learn how this approach works in Java:

public class InitializeArrays {
  public static void main(String[] args) {
      Integer [] intArray = new Integer [3];      String [] strArray = new String [2]; intArray[0] = -1;
intArray[1] = 3;
intArray[2] = 2;
System.out.println("The Elements of intArray: ");
  for(int i=0; i<intArray.length; i++)
  {
    System.out.println(intArray[i]);
  }
strArray[0] = "hi";
strArray[1] = "hello";
System.out.println("The Elements of strArray: ");
  for(int j=0; j<strArray.length; j++)
    {
System.out.println(strArray[j]);
    }
}
}

In this code,

  • First, we create an integer array of size 3 and a string array of size 2 using the square bracket syntax. 
  • After this, we initialize both arrays by specifying the respective array indexes in the square brackets.
  • In the end, we employ the for loop to iterate and print elements of each array.
Image

Example 3: How to Initialize an Array in Java Using a Loop

We can create an array of a specific size in Java and initialize it using a loop, as shown below:

public class InitializeArrays {
  public static void main(String[] args) {
Integer[] intArray = new Integer[4];
for (int i =0; i< intArray.length; i++)
{
        intArray[i] = i-3;
}
System.out.println("Array Elements: ");
for (int i = 0; i < intArray.length; i++) {
  System.out.println(intArray[i]);
}
  }
}

In this code, we create an integer array of size 4 and initialize it using a for loop. The array will be initialized with the following values:

Image

How to Initialize an Array in Java Using Curly Braces

We can use the curly braces syntax to create and initialize an array in one step. For this purpose, use the curly braces with the comma-separated syntax to specify the array values, as shown below:

datType[] arrName  = {val1, val2, val3, ...};

Here, 

  • datType indicates the array’s type.
  • arrName shows the array’s name
  • “val1, val2, val3, …” represents the array elements/values. 

Example: Creating and Initializing an Array Using the Curly Braces

Let’s learn how to create an array of any specific data type and initialize it using the curly braces syntax:

public class InitializeArrays {
  public static void main(String[] args) {
String[] strArray = { "Hi", "Hello", "Welcome" };
int[] intArray = { 100, -10, 50, 101, 256 };
boolean[] boolArray = { true, false, true, true };

System.out.println("The Elements of strArray: ");
for (int j = 0; j < strArray.length; j++) {
System.out.println(strArray[j]);
}

System.out.println("The Elements of intArray: ");
for (int i = 0; i < intArray.length; i++) {
System.out.println(intArray[i]);
}

System.out.println("The Elements of boolArray: ");
for (int i = 0; i < boolArray.length; i++) {
System.out.println(boolArray[i]);
}
  }
}

In this code, 

  • We create and initialize a String, an integer, and a boolean array.
  • To confirm the arrays’ data, we iterate each array using a for loop and print their elements using the println() method.

Output

Image

How to Initialize an Array in Java Using Stream Interface 

We can initialize an array using a stream interface that generates a stream of values and then converts it into an array. For this purpose, we can utilize one of the below-listed methods:

  • Method 1: IntStream.range()
  • Method 2: IntStream.rangeClosed()
  • Method 3: IntStream.of()

Method 1: Initializing an Array Using the IntStream.range()

We can use the range() method of the IntStream interface with the toArray() method to initialize an array of integers between the specified range. The range() method generates a stream of integers between the specified range, while the toArray() method converts this stream into an array:

import java.util.stream.IntStream;
public class InitializeArrays {
public static void main(String[] args) {
int[] intArray = IntStream.range(2, 7).toArray();
System.out.println("Array Elements: ");
for (int i = 0; i < intArray.length; i++) {
System.out.println(intArray[i]);
}
  }
}

In this example, 

  • We import the IntStream interface from the “java.util.stream” package. 
  • Next, we use the “IntStream.range()” method with the “toArray()” method to initialize an array between the integers 2 (inclusive) and 7(exclusive). 
  • After this, we utilize a Java for loop to iterate and display each array element in the output.
Image

Method 2: Initializing an Array Using the IntStream.rangeClosed()

The rangeClosed() method works similarly to the range() method; the only difference is that it also includes the ending range:

import java.util.stream.IntStream;

public class InitializeArrays {
    public static void main(String[] args) {
int[] intArray = IntStream.rangeClosed(2, 7).toArray();
System.out.println("Array Elements: ");
for (int i = 0; i < intArray.length; i++) {
System.out.println(intArray[i]);
}
  }
}

In this code, we replace the range() method with the rangeClosed() method; other than this, all the code remains the same as in the previous example:

Image

The output shows that the last element of the specified range is also included in the array.

Method 3: Initializing an Array Using the IntStream.of()

The of() method of the IntStream interface lets us initialize an array with the values of our choice, as shown below:

import java.util.stream.IntStream;

public class InitializeArrays {
    public static void main(String[] args) {
int[] intArray = IntStream.of(12, 14, 17, 5, 1, -1).toArray();
System.out.println("Array Elements: ");
  for (int i = 0; i < intArray.length; i++) {
System.out.println(intArray[i]);
  }
}
}

In this code, we use the of() method of the IntStream interface to initialize an array with six integers:

Image

That’s all about initializing an array in Java.

Final Thoughts

In Java, arrays let us keep/store multiple values of the same data type in one variable (instead of creating separate variables for each value). We can initialize the arrays using square brackets “[]”, curly braces “{}”, or Stream Interface methods. Using square brackets, we can initialize the arrays with the default or specific values. Using curly braces syntax, we can create and initialize an array in a single line. Likewise, we can use the stream interface methods to initialize an array with specific values or within the specified range.

Category: Java

Image

About Anees Asghar

Anees, a go-to expert of various technologies like PostgreSQL, Java, JS, and Linux. He has been contributing to the community through his words. A passion for serving the people excites him to craft primo content.

Previous Post: « Java Scanner Class | How to Import and Use it in Java

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

Image

Java Scanner Class | How to Import and Use it in Java

Image

Introduction to Java Server Faces (JSF)

Introduction to Java 6.0 New Features, Part–1

Java 6.0 Features Part – 2 : Pluggable Annotation Processing API

Introduction to Java Server Faces(JSF) HTML Tags

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact

Advertisement