If you have a function that takes an array, and you want to give it an array with nothing in it, you pass an zero-length array.

If you read an array from an external source, and it happens to not have any items, you'll get an zero-length array.

Answer from SLaks on Stack Overflow
๐ŸŒ
Quora
quora.com โ€บ Is-it-possible-to-create-arrays-of-length-zero-in-Java
Is it possible to create arrays of length zero in Java? - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
Discussions

java - Use of Array of length 0? - Stack Overflow
If null were returned you would ... return 0 length array - then you might only check the if range is correct - ie. in for loop. ... Save this answer. ... Show activity on this post. Zero-length arrays are rarely "required," but are more often used simply to show that there is no data being passed to a method. in java the memory ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How can I initialize a String array with length 0 in Java? - Stack Overflow
It's clearer to me that it means "I want a string array with 0 elements" rather than "I want an array with this content - which is empty". Just personal preference I guess. 2009-11-03T08:40:19.48Z+00:00 ... @Tony - I have to use the few places where Java can infer a type. More on stackoverflow.com
๐ŸŒ stackoverflow.com
November 3, 2009
Why are zero-length arrays allowed?
String[] strings = new String[0]; This creates a string array with zero elements. It's useful if you're going to increase the size of an array one element at a time. strings[0] This doesn't call the zero-th element of an array, it calls the first element of an array. strings[1] calls the second. "strings" has zero elements, hence the exception. More on reddit.com
๐ŸŒ r/learnjava
14
11
August 28, 2018
java - Use of array of zero length - Stack Overflow
The variant with 0-sized array is better, because your caller doesn't need to check for NULL and can process the array in a consistent way - say, in a loop (which would be empty in this case). There's a chapter on this in Effective Java, Item 27 More on stackoverflow.com
๐ŸŒ stackoverflow.com
June 24, 2009
๐ŸŒ
Medium
theonlylight.medium.com โ€บ java-101-the-difference-between-array-0-length-and-array-length-36231e4e846c
Java 101: The Difference Between array[0].length and array.length | by Light | Medium
December 12, 2023 - Therefore, array.length returns the number of 1D arrays in the 2D array, which is also the number of rows in the 2D array. On the other hand, array[0].length returns the number of elements in the first 1D array, which is also the number of columns in the first row of the 2D array.
๐ŸŒ
Quora
quora.com โ€บ Why-is-there-zero-length-array-in-Java-and-C
Why is there zero length array in Java and C? - Quora
Answer (1 of 8): Having an array with a length of 0 allows you to create the array with no elements, which can be especially helpful if you donโ€™t know in advance how many elements it will have, or if the number of elements will vary over time (and could temporarily be โ€œnoneโ€). For a ...
Find elsewhere
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 264957 โ€บ certification โ€บ array
array (OCPJP forum at Coderanch)
September 4, 2007 - Select the one correct answer. ... a zero-length array of String references when no program arguments are specified. e.No, it is not possible to create arrays of length zero in Java....
๐ŸŒ
JetBrains
jetbrains.com โ€บ help โ€บ inspectopedia โ€บ ZeroLengthArrayAllocation.html
Zero-length array allocation | Inspectopedia Documentation
Since array lengths in Java are non-modifiable, it is almost always possible to share zero-length arrays, rather than repeatedly allocate new ones.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 401628 โ€บ java โ€บ Array-length
Array of zero length (Beginning Java forum at Coderanch)
November 28, 2005 - The array of length zero can't hold data, so as a source of data it is not very helpful. What it's better for is as a replacement to sending null pointers. For example, consider the following loop (in java 1.4 format): Now, if the array is empty or null, you would expect no work to be done ...
Top answer
1 of 6
11

The issue I would wager is that C arrays are just pointers to the beginning of an allocated chunk of memory. Having a 0 size would mean that you have a pointer to... nothing? You can't have nothing, so there would have had to be some arbitrary thing chosen. You can't use null, because then your 0 length arrays would look like null pointers. And at that point every different implementation is going to pick different arbitrary behaviors, leading to chaos.

2 of 6
6

Let's look at how an array is typically laid out in memory:

         +----+
arr[0] : |    |
         +----+
arr[1] : |    |
         +----+
arr[2] : |    |
         +----+
          ...
         +----+
arr[n] : |    |
         +----+

Note that there isn't a separate object named arr that stores the address of the first element; when an array appears in an expression, C computes the address of the first element as needed.

So, let's think about this: a 0-element array would have no storage set aside for it, meaning there's nothing to compute the array address from (put another way, there's no object mapping for the identifier). It's like saying, "I want to create an int variable that takes up no memory." It's a nonsensical operation.

Edit

Java arrays are completely different animals from C and C++ arrays; they're not a primitive type, but a reference type derived from Object.

Edit2

A point brought up in the comments below - the "greater than 0" constraint only applies to arrays where the size is specified through a constant expression; a VLA is allowed to have a 0 length Declaring a VLA with a 0-valued non-constant expression is not a constraint violation, but it does invoke undefined behavior.

It's clear that VLAs are different animals from regular arrays, and their implementation can allow for a 0 size. They cannot be declared static or at file scope, because the size of such objects must be known before the program starts.

It's also worth nothing that as of C11, implementations are not required to support VLAs.

๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 629154 โ€บ java โ€บ length-null-array-object
what is the length of a null array object? (Beginning Java forum at Coderanch)
February 21, 2014 - There can be no length, because there is no array. ... Winston Liek wrote:I tried the following and it gives me null pointer exception. What else would you expect? If o is an object (and arrays in Java are objects) and is null, then 'o.anything' will always throw a NullPointerException.
๐ŸŒ
Edureka
edureka.co โ€บ blog โ€บ array-length-in-java
Array Length In Java | Java Array Examples | Edureka
December 4, 2023 - It must be noted, that Java Array Object does not have a method to get its length.
๐ŸŒ
JetBrains
jetbrains.com โ€บ help โ€บ inspectopedia โ€บ ConstantForZeroLengthArrayAllocation.html
Unnecessary zero length array usage | Inspectopedia Documentation
class Item { // Public zero-length array constant that can be reused public static final Item[] EMPTY_ARRAY = new Item[0]; } class EmptyNode { Item[] getChildren() { // Unnecessary zero-length array creation return new Item[0]; } } ... Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings. ... Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE. Settings or Preferences | Editor | Inspections | Java | Memory
๐ŸŒ
Javatpoint
javatpoint.com โ€บ how-to-find-array-length-in-java
How to Find Array Length in Java - Javatpoint
How to Find Array Length in Java - How to Find Array Length in Java with java tutorial, features, history, variables, object, class, programs, operators, for-loop, oops concept, array, string, map, math, methods, examples etc.