Live data from Hacker News

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

blogs.oracle.com

91–100 of 133 posts

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

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

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

#92
post #57
post #54

Earlier quoted context omitted.

The article is very confused in general about multidimensional arrays (which are really just arrays of array references in Java). It’s badly written and IMO doesn’t deserve to be on the HN front page. The author seems to have expectations that Java multidimensional arrays violate, and seems to assume the reader would also have those expectations, but they just seem confused to me. Except for TFA’s mention of the byte…

To elaborate further, consider this quote from TFA: “ For example, strangePoints = new int[3][4][0][2] In this declaration, all dimensions after the zero-size dimension are ignored. So, the result of this declaration is equivalent to a two-dimensional array of ints. ” This is just plain wrong. It’s a four-dimensional array of ints, just one that cannot contain more than zero ints, because one of the dimensions is zer…

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 classes, in the sense of getClass()), and therefore could very well have a size() method. It’s merely a stylistic choice of Java that they opted for the simpler .length syntax.

What's the "confused" part? Again, my description is accurate and you're simply saying that Java could have chosen a different way to do the same thing, but decided not to.

[0] https://docs.oracle.com/javase/specs/jvms/se16/html/jvms-6.h...

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

#93
post #79
post #59

Earlier quoted context omitted.

Likely because it's inconsistent with all other parts of the language where trailing commas are not allowed

Indeed. Whereas JavaScript and Python even allow a trailing comma in function parameters: function foo(x,) {} def foo(x,): pass Before anyone questions why anyone would do this, it's for putting arguments (or array elements) on separate lines: function foo( a, b, c, ) {}

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

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

#94

> (It’s somewhat counterintuitive that the zero dimension is not the first one in the array.) I must've read this sentence at least 7 times, but don't understand what this means. Can anyone illuminate?

Author here. This is simply a comment on the way that Java refers to the subarrays. I believe most developers would think of the first subarray as being referenced with [0], rather than with no index at all. That's all the comment was intended to point out.

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

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

The default size of an ArrayList has been 0 for a while. On the first insertion, it is initialized to 10.

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

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

Author here. Thanks for your comment. It's sometimes a little difficult to know how deeply to go into the innards of the JVM before readers' eyes glaze over and they can't follow. I'll bear in mind for future articles in this series that I can/should go deeper than the present level.

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

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

Author here. Zero-length arrays are used in the narrow domain you mention. but they are rare in bread-and-butter Java programming. Given some of the other comments on this page, you can see that this aspect is new to multiple readers. And in my experience speaking to Java devs, the reaction of surprise is far more common than "of course, I use them."

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

#98
post #79

Earlier quoted context omitted.

Indeed. Whereas JavaScript and Python even allow a trailing comma in function parameters: function foo(x,) {} def foo(x,): pass Before anyone questions why anyone would do this, it's for putting arguments (or array elements) on separate lines: function foo( a, b, c, ) {}

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

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

#99
post #57

Earlier quoted context omitted.

To elaborate further, consider this quote from TFA: “ For example, strangePoints = new int[3][4][0][2] In this declaration, all dimensions after the zero-size dimension are ignored. So, the result of this declaration is equivalent to a two-dimensional array of ints. ” This is just plain wrong. It’s a four-dimensional array of ints, just one that cannot contain more than zero ints, because one of the dimensions is zer…

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. Again, what would the alternative possibly be?

Furthermore, strangePoints.getClass() results in `[[[[I` (so it’s not just syntactical, it’s the actual runtime type), i.e. a four-dimensional array of ints, and not a two-dimensional array of ints as the article claims.

What is true is that what is allocated is a two-dimensional array, but it is an array of empty arrays, not of ints. For example, the expression strangePoints[0][0].length is valid (and yields 0), whereas it wouldn’t be valid for a two-dimensional array of ints. And again that’s on the JVM level, because otherwise the ARRAYLENGTH operation wouldn’t work here. Furthermore, strangePoints[0][0].getClass() of course is valid as well and yields `[[I`, showing that the element type of the two-dimensional array is another two-dimensional array (of ints), and not int.

> What's the "confused" part?

The confused part is this: “Many Java collections have a method called size(), which returns an integer stating the number of elements in the collection. Arrays have no such method. There are several reasons for this, but the principal one is that arrays are simple Object instances—they are not collections. The Object class has no size() method, so arrays don’t either.”

There is no reason why array objects couldn’t have a size() method even though Object doesn’t. Invoking getClass() on an array doesn’t return Object.class, but a subclass of that, and those subclasses could have the additional size() method.

This is also wrong: “Eagle-eyed readers of my earlier statement about length being a field rather than a method call might wonder how a direct subclass of Object would have a field called length to begin with, as Object has no such field.” Arrays don’t have a length field, as calling .getClass().getFields() (or getDeclaredFields()) on an array shows. The .length property is mere syntax, much like .class on an object type. But, again, Java/the JVM could have chosen to imbue array classes with a fully-fledged size() method.

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

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

Post reply on HN