TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

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

167 pointsby ternaryoperatoralmost 2 years ago

9 comments

awelxtralmost 2 years ago
&gt; (Note that the comma after the last value is accepted in Java and won’t cause an error.)<p>Why have I been 15 years programming in Java and I discovered this today?<p>On a more serious note, I prefer this kinds of posts to the traditional &quot;Look at this shiny new thing&quot; because without fundamental stuff the next big thing can&#x27;t be built and normally this information makes you a slightly better programmer.
评论 #37248634 未加载
评论 #37250667 未加载
评论 #37246273 未加载
alex3305almost 2 years ago
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.<p>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.
评论 #37246656 未加载
评论 #37246375 未加载
评论 #37246252 未加载
评论 #37248285 未加载
评论 #37252835 未加载
eternalvoyagealmost 2 years ago
Things I learned by reading this post<p>* Java arrays can have 0 dimensions<p>* When declaring arrays, trailing comma is allowed after the last element<p>* In multi-dimensional arrays, only the last dimension contains actual values. Other dimensions are just pointers to arrays.<p>Good read.
评论 #37246279 未加载
评论 #37246265 未加载
xdavidliualmost 2 years ago
&gt; (It’s somewhat counterintuitive that the zero dimension is not the first one in the array.)<p>I must&#x27;ve read this sentence at least 7 times, but don&#x27;t understand what this means. Can anyone illuminate?
评论 #37248344 未加载
评论 #37252641 未加载
评论 #37249035 未加载
评论 #37248362 未加载
Pannoniaealmost 2 years ago
What are the advantages of representing multidimensional arrays with pointers to arrays instead of a &quot;flat&quot; version where everything is stored contiguously and access is simply pointer arithmetic?<p>EDIT: For the JVM, not manually. I&#x27;m asking about the internal representation, not a manual flattening by the user.
评论 #37246715 未加载
评论 #37246284 未加载
评论 #37246404 未加载
评论 #37250811 未加载
评论 #37246248 未加载
TOGoSalmost 2 years ago
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&#x27;t wrap his head around the concept of zero and thinks it should be special somehow.<p>And nobody can give a good reason why String#length(), Array#length, and Collection#size() are all spelled differently, but if pressed, they&#x27;ll use &#x27;special bytecodes!&#x27; as an excuse.
评论 #37253424 未加载
SeanLukealmost 2 years ago
&gt; Another curiosity of Java arrays is that they can have a size of zero.<p>&gt; 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.<p>What? How can someone at Oracle have written this?<p>Zero-length arrays are used all the time when you call a function asking for an array of &quot;the latest stuff&quot; and it needs to be an array, not an ArrayList say (maybe it&#x27;s an array of bytes). If there&#x27;s no stuff, you get a zero-length array of course. The myriad foo.toArray(...) functions in Java&#x27;s library do this for example.
评论 #37246258 未加载
评论 #37253045 未加载
评论 #37259936 未加载
评论 #37246490 未加载
评论 #37246818 未加载
SeanLukealmost 2 years ago
I was pretty disappointed that, for a blog called &quot;Inside the JVM&quot;, 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?
评论 #37246553 未加载
评论 #37246356 未加载
评论 #37252979 未加载
评论 #37246820 未加载
Izmakialmost 2 years ago
I was honestly hoping for a little more considering the title is &quot;Inside the JVM&quot; and not &quot;Basic data structures in Java&quot;. Oh well...
评论 #37248319 未加载
评论 #37248299 未加载
评论 #37247714 未加载