Live data from Hacker News

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

blogs.oracle.com

101–110 of 133 posts

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

#101
post #69

Earlier quoted context omitted.

Unfortunately the article author uses the exact same terminology: > (It’s somewhat counterintuitive that the zero dimension is not the first one in the array.)

No, this is a quirk of the English language that I'm not sure how to describe generally. Best I can do: "Dimensions" plural agrees with "zero" as a count, so that reads as "the count of dimensions is zero". But they used singular "dimension" with "the" which treats "zero" as an adjective. It means "the dimension that is zero".

> "Dimensions" plural agrees with "zero" as a count, so that [always?] reads as "the count of dimensions is zero".

Out of curiosity, how do you rate each of:

"Arrays can have a zero dimension."

"Arrays can have two zero dimensions."

"Arrays can have some zero dimensions."

"Arrays can have zero dimensions."

in this regard?

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

#102

Earlier quoted context omitted.

Although be careful with that in Python because there are other situations where (x) and (x,) are both legal but mean very different things.

In Python the comma acts as a tuple creation operator inside parentheses with no other context like function invocation. Also, last I remember in JS a trailing comma meant the creation of an extra array element that’s null so I became conditioned to only use trailing commas for separators in languages where it’s consistent and specified very explicitly like in HCL

Trailing commas in JS don't do that, but having more commas than values does. (eg. [1,,].length === 2)

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

#103
post #91

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

Array is a fixed size memory, therefore it’s unnecessary to invoke a method A string has a length, because it counts the number of characters, which can have different sizes. Both arrays and strings represent a 1-dimensional string or away of things. Collections have sizes, because collections are more generic. Would a binary tree have a length? Maybe.. but it’s ambiguous and in most cases not correct

Uh-huh. And yet they can (according to the article) turn .length into a special opcode, even though it looks like any other property access. So the Java compiler could just as easily see size() being called on what it knows is an array and turn that into a special opcode. We could argue about the semantic differences between length-as-in-number-of-characters and size-as-in-bytes, but for most purposes, i.e. when any of String, char[], or List would be logically equivalent, there's no technical reason for having different APIs for what are essentially different implementations of the same thing.

Like when Rasmus couldn't stick to a naming convention for PHP functions, and it turned out to be because he couldn't be bothered to write a decent hash function. So everyone who uses the language from now to forever has to memorize this inconsistent naming scheme because the language designers couldn't get their shit together. Yeah, I get it; sometimes it's hard to foresee these things from the beginning. But it annoys me when people make up excuses for these inconsistencies that don't hold water instead of just admitting that someone messed up.

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

#104
post #99

Earlier quoted context omitted.

Author here. Your "correction" is wrong. All dimensions after the zero dimension are ignored by the JVM. This is explicitly stated in the JVM spec[0].The typecheck you're leaning on is a purely syntactical construct. Inside the JVM, the arrays function and are sized just as I described. > Object having no size() method and arrays having therefore a length field is also confused. Arrays have distinct types (and classe…

> All dimensions after the zero dimension are ignored by the JVM. This is explicitly stated in the JVM spec[0]. What is stated is: “If any count value is zero, no subsequent dimensions are allocated.” This is mostly just for clarification, because what could the alternative possibly be? It is equivalent to saying that count subarrays are allocated, which when count is zero, of course means that none are allocated. Ag…

> What is stated is: “If any count value is zero, no subsequent dimensions are allocated.” This is mostly just for clarification, because what could the alternative possibly be?

The key part comes a bit later:

> The components of the last !!allocated!! dimension of the array are initialized to the default initial value (§2.3, §2.4) for the element type of the array type.

Since the last allocated dimension is the one that has zero count... well, that means each component (of which there are none) is initialized to a value of type `int`, so technically speaking, the type of each component (of which there are none) is `int`. Due to the rest of the spec, that means the type of the dimension before that is `int[]`; and that's how you arrive at the conclusion of TFA.

It's quirky, and a semantic argument at best, and certainly not worth fighting over here on HN.

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

#105
post #45

Earlier quoted context omitted.

Given that I have programed dozen of languages since 1986, and have to jump between C#, Java, C++, Typescript, Transact SQL and PL/SQL for work, plus whatever is needed to keep the customer happy, isn't an argument I would sympathise with in code reviews.

Yet, somehow I doubt you write perfect code in all those languages. Do you cringe at yourself and conclude you just don’t care also?

No I don't, and when someone cringes looking at my code, I shut up, apply the fix and get to improve my skills on the language, instead of excusing myself.

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

#106
post #90
post #43

Earlier quoted context omitted.

IBM OpenJ9 uses a mix of OpenJDK and their J9 toolchain. Azul uses parts of OpenJDK, alongside their JIT Falcon infrastructure. Microsoft OpenJDK based distribution has better escape analysis than regular one, although OpenJDK 22 should have those improvements merged. And no, not all of them use OpenJDk, it is an urban myth, as usual.

That would be much more convincing if you linked to a proper JDK distribution that's not based on the OpenJDK.

Like on street markets, we are having bonus today, and you get two for the price of one.

https://www.ptc.com/en/products/developer-tools/perc

https://www.aicas.com/wp/products-services/jamaicavm/

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

#107
post #69

Earlier quoted context omitted.

No, this is a quirk of the English language that I'm not sure how to describe generally. Best I can do: "Dimensions" plural agrees with "zero" as a count, so that reads as "the count of dimensions is zero". But they used singular "dimension" with "the" which treats "zero" as an adjective. It means "the dimension that is zero".

> "Dimensions" plural agrees with "zero" as a count, so that [always?] reads as "the count of dimensions is zero". Out of curiosity, how do you rate each of: "Arrays can have a zero dimension." "Arrays can have two zero dimensions." "Arrays can have some zero dimensions." "Arrays can have zero dimensions." in this regard?

The first three I read like "__ dimensions that are zero", the last one useless/borderline gibberish - it sounds like "isn't that just an int, not an array of ints?"

So yeah, my description above is a bit off, I guess it's the articles that do it? Is "some" considered an article?

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

#108
post #27

Earlier quoted context omitted.

I believe that comma thing was added recently.

The comma thing is inherited from C and has always been there. Newer bits of array-like syntax, like varargs and array literals in annotations, do not allow trailing commas.

I have this feeling the C comma operator was a accident from making function calls work in the beginning. After doing some hobby languages of my own I have come to realize many features are kinda automatic and accidental when writing languages, in a way other programming almost never is.

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

#109
post #68

Earlier quoted context omitted.

I guess it was just to simplify the original specification of the JVM (if two dimensional arrays are just arrays of arrays, you don't need special instructions for them). I can also imagine that the original JVM designers did not expect JIT compilers to one day become so good that the performance difference becomes relevant.

But there is actually a special bytecode instruction to create multidimensional arrays: MULTIANEWARRAY. My suspicion is that it allows a sufficiently sophisticated JVM to allocate all the memory required at once such that all of it is contiguous. Also, the performance impact has nothing to do with the JIT. It follows from how indexes in contiguous vs. ragged arrays are computed. The JIT can't do much in this case apa…

> MULTIANEWARRAY

Oh, I didn't know that. Weird. IIRC there are no corresponding special instructions for accesses to multidimensional arrays.

> Also, the performance impact has nothing to do with the JIT

What I meant: The authors didn't expect that the JIT compiler would become so good in compiling the bytecode, that the performance difference between contiguous vs. ragged arrays actually becomes relevant.

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

#110
post #107

Earlier quoted context omitted.

> "Dimensions" plural agrees with "zero" as a count, so that [always?] reads as "the count of dimensions is zero". Out of curiosity, how do you rate each of: "Arrays can have a zero dimension." "Arrays can have two zero dimensions." "Arrays can have some zero dimensions." "Arrays can have zero dimensions." in this regard?

The first three I read like "__ dimensions that are zero", the last one useless/borderline gibberish - it sounds like "isn't that just an int, not an array of ints?" So yeah, my description above is a bit off, I guess it's the articles that do it? Is "some" considered an article?

> "isn't that just an int, not an array of ints?"

A two-dimensional (int[x][y] not int[]...[2]...[]) "array of" int isn't really a array of ints either - it's a array of arrays of int. So a zero-dimensional "array of" int is a int.

I'd call the last one ambiguous: it's either a group of (dimensions that are zero), or a (group of dimensions) that is [of group size] zero, and the text doesn't provide enough information to know which without context, just like "I read books." when you don't whether someone's talking about their hobbies or what they did over the summer.

Post reply on HN