178

I have an int array which has no elements and I'm trying to check whether it's empty.

For example, why is the condition of the if-statement in the code below never true?

int[] k = new int[3];
if (k == null) {
 System.out.println(k.length);
}
Lii
12.1k9 gold badges68 silver badges92 bronze badges
asked Mar 3, 2010 at 9:20
3
  • Could you post a bit more of the code please? The bit where the array is initialised would be useful to see. Commented Mar 3, 2010 at 9:23
  • I am not sure what your asking. Surelly to check if an array is null one would say (array == null) Commented Mar 3, 2010 at 9:34
  • 7
    Do you not want if (k != null) Commented Mar 3, 2010 at 10:51

16 Answers 16

269

There's a key difference between a null array and an empty array. This is a test for null.

int arr[] = null;
if (arr == null) {
 System.out.println("array is null");
}

"Empty" here has no official meaning. I'm choosing to define empty as having 0 elements:

arr = new int[0];
if (arr.length == 0) {
 System.out.println("array is empty");
}

An alternative definition of "empty" is if all the elements are null:

Object arr[] = new Object[10];
boolean empty = true;
for (int i=0; i<arr.length; i++) {
 if (arr[i] != null) {
 empty = false;
 break;
 }
}

or

Object arr[] = new Object[10];
boolean empty = true;
for (Object ob : arr) {
 if (ob != null) {
 empty = false;
 break;
 }
}
Miles
32.6k7 gold badges69 silver badges77 bronze badges
answered Mar 3, 2010 at 9:22
4
  • 1
    ups, the last snippet has obj !- null, probably meant to be obj != null Commented Mar 3, 2010 at 9:58
  • 8
    Don't forget about: org.apache.commons.lang3.ArrayUtils.isEmpty(k) Commented Sep 21, 2012 at 13:29
  • Remember, === would not work. You must use == because null is of a different type. Commented Dec 14, 2019 at 18:16
  • Why someone would assign null to int array, if it's initialised with zeros by default? Only reference types are initialised with nulls. Commented Apr 25, 2022 at 2:10
150

ArrayUtils.isNotEmpty(testArrayName) from the package org.apache.commons.lang3 ensures Array is not null or empty

Misa Lazovic
2,82110 gold badges34 silver badges39 bronze badges
answered Sep 3, 2015 at 19:18
0
33

Method to check array for null or empty also is present on org.apache.commons.lang:

import org.apache.commons.lang.ArrayUtils;
ArrayUtils.isEmpty(array);
answered Jul 16, 2019 at 13:56
24

Look at its length:

int[] i = ...;
if (i.length == 0) { } // no elements in the array

Though it's safer to check for null at the same time:

if (i == null || i.length == 0) { }
answered Mar 3, 2010 at 9:23
1
  • 3
    if you need to check for both null and length 0, it's important to check for null first as you have done, to avoid a possible null pointer error Commented Jan 15, 2020 at 5:31
4

I am from .net background. However, java/c# are more/less same.

If you instantiate a non-primitive type (array in your case), it won't be null.
e.g. int[] numbers = new int[3];
In this case, the space is allocated & each of the element has a default value of 0.

It will be null, when you don't new it up.
e.g.

int[] numbers = null; // changed as per @Joachim's suggestion.
if (numbers == null)
{
 System.out.println("yes, it is null. Please new it up");
}
answered Mar 3, 2010 at 9:23
2
  • 1
    In Java that won't compile, because it will tell you that numbers has not been initialized yet. "Uninitialized" and null are not the same thing. Commented Mar 3, 2010 at 9:30
  • Thanks Joachim. I will edit the post to have int[] numbers changed to int[] numbers == null; In c#, it is not the case. Commented Mar 3, 2010 at 10:17
4

In Java 8+ you achieve this with the help of streams allMatch method.

For primitive:

int[] k = new int[3];
Arrays.stream(k).allMatch(element -> element != 0)

For Object:

Objects[] k = new Objects[3];
Arrays.stream(k).allMatch(Objects::nonNull)
answered Apr 21, 2020 at 21:18
3

An int array is initialised with zero so it won't actually ever contain nulls. Only arrays of Object's will contain null initially.

answered Mar 3, 2010 at 9:26
3
  • what if I have to check null for integer Commented Mar 3, 2010 at 9:30
  • 1
    You can't check for null with primitives such as int. Commented Mar 3, 2010 at 9:35
  • 2
    depends where you declared it, if as a class member, then yes it's get initialized with zeroes. but when declared locally inside a method, i believe it's another case... you have to assign an initial value yourself. i suppose. just a thought! Commented Mar 3, 2010 at 9:43
3

if you are trying to check that in spring framework then isEmpty(Object[]) method in ObjectUtils class helps,

public static boolean isEmpty(@Nullable Object[] array) {
 return (array == null || array.length == 0);
 }
Datz
3,9913 gold badges31 silver badges64 bronze badges
answered Nov 30, 2020 at 13:11
2

The point here very simply is that the variable k isn't null because it points to the array. It doesn't matter that the array itself is empty. The null test in your post would only evaluate to true if the variable k didn't point to anything.

answered Jan 12, 2016 at 16:45
2

I tested as below. Hope it helps.

Integer[] integers1 = new Integer[10];
 System.out.println(integers1.length); //it has length 10 but it is empty. It is not null array
 for (Integer integer : integers1) {
 System.out.println(integer); //prints all 0s
 }
//But if I manually add 0 to any index, now even though array has all 0s elements
//still it is not empty
// integers1[2] = 0;
 for (Integer integer : integers1) {
 System.out.println(integer); //Still it prints all 0s but it is not empty
 //but that manually added 0 is different
 }
//Even we manually add 0, still we need to treat it as null. This is semantic logic.
 Integer[] integers2 = new Integer[20];
 integers2 = null; //array is nullified
// integers2[3] = null; //If I had int[] -- because it is priitive -- then I can't write this line. 
 if (integers2 == null) {
 System.out.println("null Array");
 } 
answered Sep 19, 2017 at 3:25
1

By 'empty array' most probably you meant length is 0.

if( array.length == 0 ) {
 // do something ...
}

But it's also worth to check first if the array ref is null or not before accessing it's property (e.g. length).

if( array != null ) {
 if( array.length == 0 ) {
 // do something ...
 }
}

And this become necessary if the array is a parameter to a method.

public boolean isEmpty( Object[] array ) throws Exception {
 if( array != null ) {
 if( array.length == 0 )
 return true;
 } else {
 throw new Exception("isEmpty(): Array ref is null");
 }
 return false;
}

The checking should be done in cascading way (not using && i.e. array != null && array.length == 0) since the order of evaluation of the conditions depends on implementation of the compiler. If it is evaluating the array.length == 0 condition first, it will ends with runtime error if array is null.

answered Jan 21, 2024 at 6:19
0
 public boolean empty() {
 boolean isEmpty = true;
 int i = 0;
 for (int j = 0; j < array.length; j++) {
 if (array[j] != 0) {
 i++;
 }
 }
 if (i != 0) {
 isEmpty = false;
 }
 return isEmpty;
}

This is as close as I got to checking if an int array is empty. Although this will not work when the ints in the array are actually zero. It'll work for {1,2,3}, and it'll still return false if {2,0} but {0} will return true

answered Apr 27, 2018 at 3:35
0

Assuming column is "array" type.

size(col(x))===0 will filter out all empty arrays (arrays with size/length=0).

answered Apr 22, 2024 at 12:17
-1

An int array without elements is not necessarily null. It will only be null if it hasn't been allocated yet. See this tutorial for more information about Java arrays.

You can test the array's length:

void foo(int[] data)
{
 if(data.length == 0)
 return;
}
answered Mar 3, 2010 at 9:22
-1

I believe that what you want is

int[] k = new int[3];
if (k != null) { // Note, != and not == as above
 System.out.println(k.length);
}

You newed it up so it was never going to be null.

answered Mar 4, 2010 at 17:55
-1

You can also check whether there is any elements in the array by finding out its length, then put it into if-else statement to check whether it is null.

int[] k = new int[3];
if(k.length == 0)
{
//do something
}
answered Nov 14, 2017 at 5:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.