Live data from Hacker News

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

blogs.oracle.com

11–20 of 133 posts

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

#11

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.

Arrays cannot have zero dimensions. They can have zero-sized dimensions.

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

#12
post #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

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.

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

#13
post #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.

I believe that comma thing was added recently.

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

#14

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.

> Java arrays can have 0 dimensions

The way you've phrased it is a bit ambiguous.

Having 0 dimension sounds like

    x = new int[7][5]; // 2 dimensions
    y = new int[9];    // 1 dimension
    z = new int;       // no dimensions, not an array, not allowed
The phrasing you mean is "dimensions can have zero size".

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

#15

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.

Ragged arrays. You can do this:

    int[][] x = new int[2][];
    x[0] = new int[] { 1, 2, 3 };
    x[1] = new int[] { 1, 2, 3, 4 };

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

#16
post #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.

I believe that comma thing was added recently.

Yup, as recent as 1996:

"A trailing comma may appear after the last expression in an array initializer and is ignored"

https://titanium.cs.berkeley.edu/doc/java-langspec-1.0/10.do...

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

#17

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.

> Java arrays can have 0 dimensions The way you've phrased it is a bit ambiguous. Having 0 dimension sounds like x = new int[7][5]; // 2 dimensions y = new int[9]; // 1 dimension z = new int; // no dimensions, not an array, not allowed The phrasing you mean is "dimensions can have zero size".

Thanks for the clarification.

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

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

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

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

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

#20

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.

The JVM doesn’t really have multidirectional arrays, it just has arrays with a type, and that type may itself be an array.

Sometimes that can be useful, and sometimes not.

Post reply on HN