
On March 13, Azul gathered Java developers, enthusiasts, and Java Champions to celebrate 30 years of Java. An impressive lineup of luminaries gave presentations, including author and Java luminary Venkat Subramaniam. Venkat discussed his appreciation for little-recognized behind-the-scenes Java innovations that have helped keep Java fresh while maintaining backward compatibility.
To mark 30 years of Java, Venkat Subramaniam talked about the Java innovations that happen behind the scenes, that don’t get the attention of other features but provide critical functionality. With more than 200 programming languages to choose from, Java developers have to keep Java moving forward, and Venkat said innovation in Java has actually accelerated over the last decade. Java cannot simply change its code specification like some other languages can, he said, because it has too many active users.
“There are about 10 million users, programmers using Java,” Venkat said. “Well, when 10 million programmers use your language, and out of which about 1 million know where you live, you have to make your decisions very differently.”
Venkat highlighted Java’s backward compatibility, which keeps the language stable while moving the language forward and making it better for programmers. With that, he launched into a few examples of innovations from Java developers behind the scenes.
Invoke Dynamic
Venkat discussed the simple runnable interface, which he demonstrated as
runnable.run()
And passed it to the use method. To implement runnable, he used it as an anonymous inner class, a feature that goes back almost to the beginning of Java.
“Well, we wrote an anonymous inner class,” Venkat said. “You can see the evidence to this by…. Behind the scenes as well, as you can see in this particular example, I’m looking at the bytecode being generated, and you can see in the bytecode there’s a new of an inner class. That’s where you see a sample dollar one, that’s your anonymous inner class.”
Other languages on the JVM introduced lambda expressions as anonymous in their classes because that’s what was available in Java as an interim measure, at the bytecode level. When Java wanted to introduce lambda expressions, they didn’t have the luxury of doing the same thing.
If Java is going to become prevalent in terms of adopting a functional style of programming, and it just implemented anonymous inner classes, Java would create more lambdas, more anonymous classes, more instances of those classes, and more garbage is created and collected.
“Well, this is where I’m absolutely fascinated and take a completely different thread for a minute,” Venkat said, “pun intended.”
He explained that developers were working on making other languages better on the JVM. Other languages were providing more flexibility than Java does. The developers were having a hard time because they were trying to implement those features via reflection, which was not very efficient and produced bloated code. So they implemented a feature called Invoke Dynamic.
Invoke Dynamic allowed them to do a dynamic dispatch to functions in a much more flexible way, but by providing a construct at the bytecode level and at the JVM level. They had done that in the Java 7 timeframe. And as the team was implementing lambdas, which started in Java 7 but branched off to Java 8, they were struggling to implement lambdas without causing so much inefficiency. Somebody suggested, what if we use Invoke Dynamic? Invoke Dynamic was not implemented for that purpose.
Venkat continued his demo and replaced an anonymous inner class with a dispatch to a function, and the lambda sources convert to functions, and then they are invoked via that particular interface, but that’s through an Invoke Dynamic method. This leads to efficiency at the bytecode level because you don’t have a bloat off the bytecode with all these anonymous inner classes. This has an efficiency at runtime as well, because you’re not creating more garbage, so you have a lot less garbage to collect.
“I’m a little curious about these kinds of changes because I often say these are the things you don’t see with your naked eye,” Venkat said. “These are the things that are behind the scenes, but they make a huge impact.”
In fact, Venkat suggested that Java developers don’t really talk about these much because they remove problems so you can focus on enjoying the benefits rather than complaining. “If something is done extremely well, we probably don’t even take note of it, and that’s one of the things I love about what they have done in terms of this,” he said
Venkat presented two other examples of innovations behind the scenes. To see those and his entire presentation, visit the Duke Turns 30 section of our website.