Return Empty Array in Java

Rupam Yadav Jan 30, 2023 Jan 20, 2021
  1. Return an Empty Array Using new int[0] in Java
  2. Return an Empty Array Using Empty Curly Braces in Java
  3. Return an Empty Array Using org.apache.commons.lang3.ArrayUtils
Return Empty Array in Java

In this article, we will discuss how we can return an empty array in Java. We sometimes have to return an empty array for a few reasons, like when the array is coming from an API, and it returns null; in this case, we might want to return an array without any element, instead of null.

Return an Empty Array Using new int[0] in Java

Every array has a fixed size that we can specify when we create the array. If the array has a length of zero, then it does not contain any element. To return an empty array from a function, we can create a new array with a zero size.

In the example below, we create a function returnEmptyArray() that returns an array of int. We return new int[0] that is an empty array of int. In the output, we can get the length of the array getEmptyArray.

public class EmptyArray {
    public static void main(String[] args) {

        int[] getEmptyArray = returnEmptyArray();

        System.out.println(getEmptyArray.length);
    }

    private static int[] returnEmptyArray() {
        return new int[0];
    }
}

Output:

0

Return an Empty Array Using Empty Curly Braces in Java

In Java, we can instantiate an array using { } with the elements inside, and the array size is the number of elements in the array. We can return empty curly braces without any item that will make the array size to zero.

In the example, we create a function returnEmptyArray that returns an int array. We initialize an empty array using int[] emptyArr = {} and then return emptyArr. The length of the array will be zero.

public class EmptyArray {
    public static void main(String[] args) {

        int[] getEmptyArray = returnEmptyArray();

        System.out.println(getEmptyArray.length);
    }

    private static int[] returnEmptyArray() {

        int[] emptyArr = {};

        return emptyArr;
    }
}

Output:

0

Return an Empty Array Using org.apache.commons.lang3.ArrayUtils

In this example, we use the ArrayUtils class of the Apache Commons Library. To use this library, we have to import it using the following dependency.

 <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.11</version>
 </dependency>

ArrayUtils class has several static fields to return empty arrays of different types like boolean , char, String etc. For this example, we use EMPTY_STRING_ARRAY that returns an empty array of the String type. The output shows the getEmptyArray array length is zero.

import org.apache.commons.lang3.ArrayUtils;

public class EmptyArray {
    public static void main(String[] args) {

        String[] getEmptyArray = returnEmptyArray();

        System.out.println(getEmptyArray.length);
    }

    private static String[] returnEmptyArray() {

        return ArrayUtils.EMPTY_STRING_ARRAY;
    }
}

Output:

0
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java Array