> (Note that the comma after the last value is accepted in Java and won’t cause an error.)<p>Why have I been 15 years programming in Java and I discovered this today?<p>On a more serious note, I prefer this kinds of posts to the traditional "Look at this shiny new thing" because without fundamental stuff the next big thing can't be built and normally this information makes you a slightly better programmer.
In my experience, just a little bit of insider knowledge goes a long ways to making better code. Arrays are fun things, especially when you do a deep dive into the System.arraycopy() function. But the same goes for all Collections in Java. For instance, most of them have a default size (mostly 10), and growing them is a costly operation. So knowing beforehand how large your collection can or may be, can benefit code. I could use this effectively when working with large document XML parsing.<p>I recommend everyone that uses a managed language (Java, C# or others) to at least get a basic understanding of these fundamentals. And also know which collection type to use when.
Things I learned by reading this post<p>* Java arrays can have 0 dimensions<p>* When declaring arrays, trailing comma is allowed after the last element<p>* In multi-dimensional arrays, only the last dimension contains actual values. Other dimensions are just pointers to arrays.<p>Good read.
> (It’s somewhat counterintuitive that the zero dimension is not the first one in the array.)<p>I must've read this sentence at least 7 times, but don't understand what this means. Can anyone illuminate?
What are the advantages of representing multidimensional arrays with pointers to arrays instead of a "flat" version where everything is stored contiguously and access is simply pointer arithmetic?<p>EDIT: For the JVM, not manually. I'm asking about the internal representation, not a manual flattening by the user.
tl;dr: Arrays have some special opcodes dedicated to them, and otherwise are completely unsurprising, unless you are an ancient Roman, or Andrew Brinstock, who can't wrap his head around the concept of zero and thinks it should be special somehow.<p>And nobody can give a good reason why String#length(), Array#length, and Collection#size() are all spelled differently, but if pressed, they'll use 'special bytecodes!' as an excuse.
> Another curiosity of Java arrays is that they can have a size of zero.<p>> 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.<p>What? How can someone at Oracle have written this?<p>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.
I was pretty disappointed that, for a blog called "Inside the JVM", very little in the blog entry discussed goings on inside the JVM. For example, when does the JVM typically optimize away bounds or null checks? How are arrays of booleans packed and what is their efficiency compared to arrays of bytes or words?
I was honestly hoping for a little more considering the title is "Inside the JVM" and not "Basic data structures in Java". Oh well...