Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Tuesday, November 23, 2021

Java - How to return empty array?

1. Overview

In this tutorial, we'll learn how to return an empty array in java in different ways.

An empty array means the array is not having any values and the length of the array is zero. Sometimes we need to return an empty array rather than returning null values.

Creating an empty array can be done in 3 ways.


All examples work for any type of array ex. int, double, custom type such as Emp, Student etc.

Java - How to return empty array?



2. Return Empty Array - With new int[0] or new Emp[0]


When you want to return an array with a size of 0 then you just need to return new int[0]. Here, new int[0] returns an integer type array with zero values and size is 0.

Example 1:

In the below example, we added a utility method that returns an empty array. So that this method can be accessed from any other similar scenarios.
package com.javaprogramto.arrays.empty;

public class ReturnEmptyArrayExample1 {

	public static void main(String[] args) {

		if (true) {
			int[] array1 = emptyArray();
			System.out.println("Length of int array 1 : " + array1.length);
		}
		
		if( 40 % 10 == 0) {
			int[] array2 = emptyArray();
			System.out.println("Length of int array 2 : " + array2.length);
		}

	}

	public static int[] emptyArray() {

		return new int[0];
	}
}

Output:
Length of int array 1 : 0
Length of int array 2 : 0

We could see the length of returned array size is 0 from method emtpyArray().

3. Return Empty Array - With {} Empty curly braces


Another approach is to create the empty array is using empty curly braces like {}. We no need of using new keyword and type of array with size 0.

This is the simplest one in forming the blank array. You can simply return the empty braces from the method with the type.

Example 2:

In the below example, line int[] emptyArray = {}; creates the empty integer array with size 0.
package com.javaprogramto.arrays.empty;

public class ReturnEmptyArrayExample2 {

	public static void main(String[] args) {

		if (true) {
			int[] array1 = emptyArrayWIthCurlyBraces();
			System.out.println("Length of int array 1 : " + array1.length);
		}

		if (100 > 0) {
			int[] array2 = emptyArrayWIthCurlyBraces();
			System.out.println("Length of int array 2 : " + array2.length);
		}

	}

	public static int[] emptyArrayWIthCurlyBraces() {

		int[] emptyArray = {};
		
		return emptyArray;
	}
}

Output:
Length of int array 1 : 0
Length of int array 2 : 0

4. Return Empty Array - With Apache Commons API


Apache commons API has been added with a utility class ArrayUtils. This class has several useful methods.

ArrayUtils has a constant EMPTY_INT_ARRAY which holds an empty array of int.

in the same way, there are many empty arrays default available.

Samples:
ArrayUtils.EMPTY_BOOLEAN_ARRAY;
ArrayUtils.EMPTY_BOOLEAN_OBJECT_ARRAY;
ArrayUtils.EMPTY_STRING_ARRAY;

All of these are initialised with new boolean[0] syntax as below.
public static final float[] EMPTY_FLOAT_ARRAY = new float[0];

public static final Float[] EMPTY_FLOAT_OBJECT_ARRAY = new Float[0];

/**
 * An empty immutable {@code int} array.
 */
public static final int[] EMPTY_INT_ARRAY = new int[0];

/**
 * An empty immutable {@code Integer} array.
 */
public static final Integer[] EMPTY_INTEGER_OBJECT_ARRAY = new Integer[0];

/**
 * An empty immutable {@code long} array.
 */
public static final long[] EMPTY_LONG_ARRAY = new long[0];

/**
 * An empty immutable {@code Long} array.
 */
public static final Long[] EMPTY_LONG_OBJECT_ARRAY = new Long[0];

/**
 * An empty immutable {@code Object} array.
 */
public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];

Example 3:
package com.javaprogramto.arrays.empty;

import org.apache.commons.lang3.ArrayUtils;

public class ReturnEmptyArrayExample3 {

	public static void main(String[] args) {

		if (true) {
			int[] array1 = emptyArrayWIthCurlyBraces();
			System.out.println("Length of int array 1 : " + array1.length);
		}

		if (100 > 0) {
			int[] array2 = emptyArrayWIthCurlyBraces();
			System.out.println("Length of int array 2 : " + array2.length);
		}

	}

	public static int[] emptyArrayWIthCurlyBraces() {

		return ArrayUtils.EMPTY_INT_ARRAY;
	}
}

This code also generates the same output as the above section.

5. Conclusion


In this article, We've seen how to return an empty array in 3 ways with examples.

The concepts shown will work for any type of empty array creation.




No comments:

Post a Comment

Please do not add any spam links in the comments section.