Live data from Hacker News

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

blogs.oracle.com

31–40 of 133 posts

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

#31

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.

They're not the same thing. A flat array is fixed in dimension (modulo sum each size = total size). An array of arrays can have its sub-arrays replaced.

        long[][] foo = new long[2][];
        foo[0] = new long[5];
        foo[1] = new long[3];
        // ...
        foo[0] = new long[7];
Although I think as a developer you probably almost always want to index a single flat flat array if the dimensions are fixed. This is much faster.

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

#32
post #7

Earlier quoted context omitted.

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

No, I mean for the JVM.... the interface could remain the same but the pointer indirections could be avoided. I don't see the downsides, that's why I'm asking.

If you keep the interface the exact same, you run into some pretty big complications, as with the current interface you can change one row out with another object with a single statement, or similarly get a row object as such (which you do implicitly in "arr[a][b]"!). So you'd have to still store an object per row in addition to the data to keep their identity the same each time they're gotten, and have some way to transform out of this representation if the last reference to the 2D array is via a single row of it, GCing away the rest of the rows.

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

#33
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…

[deleted]

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

#34
post #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?

[deleted]

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

#35
post #18
post #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?

For that you need an Inside Hotspot, Inside OpenJ9, Inside GraalVM, Inside Azul, Inside ART, Inside microEJ, Inside PTC, Inside JamaicaVM, Inside.... Otherwise is like trying to discuss what does a C compiler do, when only looking through the lens of the C abstract machine in ISO C.

As far as I know, all those distributions use OpenJDK for that kind of stuff and don't really do much more than apply a few patches here and there, not change stuff like how the JVM packs bytes in memory.

Would be happy to be proven wrong.

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

#36
post #24
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…

This choice probably pre-dates Oracle?

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

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

#38
post #7

Earlier quoted context omitted.

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

No, I mean for the JVM.... the interface could remain the same but the pointer indirections could be avoided. I don't see the downsides, that's why I'm asking.

[deleted]

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

#39
post #18

Earlier quoted context omitted.

For that you need an Inside Hotspot, Inside OpenJ9, Inside GraalVM, Inside Azul, Inside ART, Inside microEJ, Inside PTC, Inside JamaicaVM, Inside.... Otherwise is like trying to discuss what does a C compiler do, when only looking through the lens of the C abstract machine in ISO C.

This is an Oracle blog, and it's called "Inside the JVM". What VMs do Oracle build besides Hotspot and its ilk?

GraalVM, the JVM inside various database products, historically the embedded VMs, and maybe a few more I’ve forgotten. :-)

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

#40
post #29
post #19

Earlier quoted context omitted.

I cringe every single time I see a for loop for what System.arraycopy () has been providing since early days. For better or worse, it shows me that the author isn't that into Java.

I cannot for my life remember the argument order, so I write the manual code and let IntelliJ convert it.

Doesn't autocomplete show the arguments? I usually use Netbeans when I write Java, so no idea if InelliJ is just that bad.
Post reply on HN