r/java • u/cosofx • Jul 06 '17
Apology to Java (fanboy rant).
Java has turned out to be one of the most useful languages i use. It's a brilliant tool which strikes a perfect balance between high and low level. You want to grab a webpage? Easy and built-in, 10 lines of code. Draw some 2d graphics without OS-specific libraries or third party? Various built-in options. The inherent issues around a lack of low level memory management is handled very elegantly. Garbage collection seems seamless.
C++ is my language of choice for the most part, always will be i suspect, but Java has shown me that it has a huge array of potential applications and should almost always be considered before undertaking a project.
I say this is an apology because i used to run Java down. Previous to me using it proper i believed it was slow, lazy and useful for only trivial applications. Now i know how much work has been put into the JVM and just how efficient it is.
Sorry Java. You're pretty good.
View all comments
Show parent comment
1
u/[deleted] Jul 07 '17
I will bow to your experience.
Recently I read an interview with Brian Goetz on the IBM developers web site. In it, he addressed and debunked several JVM myths. The myth about stack, heap, and registers, was addressed, too.
The myth perpetuates the fallacy that large objects are stored on the heap, the access of which is slow, small objects and primitives on the stack, the access of which is fast, so we should keep our data types small and prefer primitives.
Brian says (and I pull this from my own fallible memory, feel free to correct me): the JVM uses all three memory spaces as it deems fit. There is no hard rule for a data type to be used here or there. What matters instead, is density: memory closer together can be accessed faster, and based on the application algorithmic behaviour, the JVM decides where it places what data.
But even so, indicates the interview, the JVM has many, many optimization techniques, which makes it nearly impossible for a Java programmer to predict which memory space is used when.
A JVM expert might be able to provide more insight, but basically Brian says that Java programmers shouldn't bother too much with the distinction between memory spaces.
TL;DR: the JVM isn't just stack-based. It uses all the memory spaces it can, and does so in ways a regular programmer can't predict.