Table of Contents
💡 Outline
You can use below code to initialize empty array in java.
1234 // Initialize empty arrayint arr[] = {};Or
1234 // Initialize empty arrayint array[] = new int[0];Or
1234 // Initialize empty arrayint arr[] = new int[] {};
1. Introduction
In this post, we take a look on How to Initialize empty array in Java
. We will look at different ways to Initialize arrays in Java with dummy values or with prompted user inputs. Arrays in Java follow a different declaration paradigm than other programming languages like C, C++, Python, etc.
In Java, array is by default regarded as an object and they are allocated memory dynamically. Moreover, in java at the time of creation an array is initialized with a default value. For Example: If the array is of type int(integer) all the elements will have the default value 0.
Hence, we will outline different methods to initialize an empty array with examples, to make it easy for a beginner in Java to use appropriate example under apt cases.
2. Initialize an empty array in Java
Considering all cases a user must face, these are the different methods to initialize an array:
Let us look at these methods in detail.
2.1 Using Array Initializer
To declare empty array, we can use empty array initializer with {}
. Java
1 2 3 |
int arr[] = {}; |
Here is sample program:
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Arrays; public class EmptyArrayMain { public static void main(String[] args) { int[] intArr= {}; System.out.println(Arrays.toString(intArr)); } } |
1 2 3 |
[] |
Length of the array will be number of elements enclosed in {}
. Since we have put nothing in {}
, length of Array will be 0
.
Please note that once you create empty array, you can’t change the length. In case, you need to change the size, you should use ArrayList instead.
You can also initialize empty array with Array creation expression as below:
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Arrays; public class EmptyArrayMain { public static void main(String[] args) { int[] intArr= new int[] {}; System.out.println(Arrays.toString(intArr)); } } |
1 2 3 |
[] |
2.2 Using new keyword with 0 size
You can use new keyword with 0 size.
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Arrays; public class EmptyArrayMain { public static void main(String[] args) { int[] intArr= new int[0]; System.out.println(Arrays.toString(intArr)); } } |
1 2 3 |
[] |
3. Initialize empty 2D Array in Java
You can initialize empty 2D array similar to empty Array.
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Arrays; public class Empty2DArrayMain { public static void main(String[] args) { int[][] intArr= new int[0][]; System.out.println(Arrays.deepToString(intArr)); } } |
1 2 3 |
[] |
We can also create empty 2D array of non empty Arrays.
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Arrays; public class Empty2DArrayMain { public static void main(String[] args) { int[][] intArr= new int[0][1]; System.out.println(Arrays.deepToString(intArr)); } } |
1 2 3 |
[] |
As Java allows to create empty array with size 0 for int and String.
1 2 3 4 |
int[] arr = new int[0]; String[] arr = new String[0]; |
Similarly it allows empty of int[1] here.
1 2 3 |
int[][] intArr= new int[0][1]; |
That’s all about how to initialize empty array in Java.