Hacker News new | past | comments | ask | show | jobs | submit login

> Another curiosity of Java arrays is that they can have a size of zero.

> This code will not result in an error message. This surprising feature is used primarily by code generators, which might create an array and then discover there are no values to place in it.

What? How can someone at Oracle have written this?

Zero-length arrays are used all the time when you call a function asking for an array of "the latest stuff" and it needs to be an array, not an ArrayList say (maybe it's an array of bytes). If there's no stuff, you get a zero-length array of course. The myriad foo.toArray(...) functions in Java's library do this for example.




Right, and

> create an array and then discover there are no values to place in it.

I mean you can't change the size of an array after you've created it, so if you create an array with a certain size intending to put values in it, then discover there aren't any values, you've still got a non-zero size array...


Author here. Zero-length arrays are used in the narrow domain you mention. but they are rare in bread-and-butter Java programming. Given some of the other comments on this page, you can see that this aspect is new to multiple readers. And in my experience speaking to Java devs, the reaction of surprise is far more common than "of course, I use them."


Thing is, practically every modern programming language has zero-length arrays or lists. This is derived from Lisp, which had zero-length arrays (and of course zero-length lists). It's not at all surprising. What ought to be surprising is that among languages developed in the last 40 years, C++ almost uniquely does not have them. I use zero-length arrays all the time in my coding, as does everyone I know. I think they're probably much more common than you imagine.


In Common Lisp I might want to push elements to an array, and start with zero elements:

  CL-USER 9 > (make-array 0 :adjustable t :fill-pointer 0)
  #()

  CL-USER 10 > (vector-push-extend 'foobar *)
  0

  CL-USER 11 > **
  #(FOOBAR)


An adjustable vector probably doesn't qualify as an "array" in the Java sense of the term: it's closer to an ArrayList. However Lisp is perfectly comfortable making zero-length simple-vectors, which are arrays in the Java sense.


An ArrayList is not an Array?

> comfortable making zero-length simple-vectors

Btw., CL allows also zero-dimensional arrays:

  CL-USER 3 > (make-array '())
  #0ANIL


> An ArrayList is not an Array?

Not in Java, no.

An ArrayList is an object which represents a variable length random-access list and does not support basic types (int, double etc.) An array is fixed-length and supports basic types. It is not an object per se.

The closest analog to an ArrayList in Lisp is an extensible vector, and the closest analog to an array is a simple-vector.

> Btw., CL allows also zero-dimensional arrays:

As any decent language would!


This choice probably pre-dates Oracle?


I think he's saying "how could he write that 0 length array is a surprising feature".


And the use of "primarily", when there is a much more common use case, so common that it's baked right into the standard libraries.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: