Live data from Hacker News

Inside the JVM: Arrays and how they differ from other objects

blogs.oracle.com

1–10 of 133 posts

Re: Inside the JVM: Arrays and how they differ from other objects

#2
> (Note that the comma after the last value is accepted in Java and won’t cause an error.)

Why have I been 15 years programming in Java and I discovered this today?

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.

Re: Inside the JVM: Arrays and how they differ from other objects

#3
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?

EDIT: For the JVM, not manually. I'm asking about the internal representation, not a manual flattening by the user.

Re: Inside the JVM: Arrays and how they differ from other objects

#4
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.

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.

Re: Inside the JVM: Arrays and how they differ from other objects

#5
> 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.

Re: Inside the JVM: Arrays and how they differ from other objects

#6
Things I learned by reading this post

* Java arrays can have 0 dimensions

* When declaring arrays, trailing comma is allowed after the last element

* In multi-dimensional arrays, only the last dimension contains actual values. Other dimensions are just pointers to arrays.

Good read.

Re: Inside the JVM: Arrays and how they differ from other objects

#7

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? EDIT: For the JVM, not manually. I'm asking about the internal representation, not a manual flattening by the user.

Maintenance, probably.

Puting data in a structure that mirrors the real thing normally helps when trying to understand it

Re: Inside the JVM: Arrays and how they differ from other objects

#8
post #4

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 agree. Understanding the inner working of languages and their runtimes is IMHO what gets you one step closer to a senior. Luckily, I had in my young career few seniors in the team who knew a lot about Java and shared their knowledge about the behavior.

Re: Inside the JVM: Arrays and how they differ from other objects

#9
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?

Re: Inside the JVM: Arrays and how they differ from other objects

#10
post #5

> 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 st…

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...

Post reply on HN