How to define an empty array in Java?
In Java, you can define an empty array using the following method:
- Use array initializer:
int[] arr = new int[0];
- Utilize the static methods of the Arrays class:
int[] arr = Arrays.copyOf(new int[0], 0);
- Utilize the toArray() method of ArrayList.
int[] arr = new int[0];
arr = new ArrayList<Integer>().toArray(arr);
No matter how an empty array is defined, it will always create an array with length zero.