array is empty java code example

Example 1: check if array is empty java

if (myArray == null || myArray.length == 0) { }

Example 2: java empty array

String [] names = new String[0];

// or 

int [] ages = {}

Example 3: empty array java

//where n is the length you want the array to be
// for integer array
int[] intArray = new int[n];
// for String array
String[] strArray = new String[n];

Example 4: isempty for arrays

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

Example 5: isempty for arrays

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

Tags:

Java Example