May 23 marks the 25th anniversary of Java. To celebrate it properly we invited one of our Java gurus Stefan Dragnev to share his thoughts about Java’s evolution over the years, the importance of the Java Virtual Machine in it, and what to expect from Java in the future. Stefan is a Senior Software Engineer with more than 20 years of experience. His main languages are Java and C++. Stefan is interested in performance optimization, concurrency, compilers and low-latency applications. Aspiring to be JVM and OpenJDK developer. Read what he shared with us:
Today is a big milestone for Java. After all these years, the language is more popular than ever. Statistics differ, but it is estimated that around 10 million people use it as their main programming language.
Java started as a language for interactive TV. As often happens, it was ahead of its time and didn’t achieve much success in this area. Regardless, as a programming language Java had a unique blend of features that turned out to be vital for its future success.
Rise to fame
The most notable is its portability. Write once, run everywhere (WORE), was the 1995 slogan used and created by Sun Microsystems (creators of Java) to illustrate the cross-platform benefits of the Java. Portability is achieved by a combination of bytecode and Java Virtual Machine (JVM).
Java got a significant boost from being a language with formal specification from its very inception. That combined with fanatical support for backwards compatibility – never did a new version of Java break programs written in a previous version of the language, or required recompilation - solidified Java position as the go-to language for big corporations.
Java syntax is quite similar to C and C++ making it easy to learn by people already familiar with these languages. It comes with an extensive collection of libraries ranging from collections to networking and GUIs.
Why learn to program in Java?
- Java Memory Model
Java had built-in threading support right from the very beginning, but what absolutely set it apart from all other languages was the creation of Java Memory Model (JMM) in 2005. In essence, it is a set of a small number of rules that allow programmers to reason how a program behaves when threads are executing on multiple cores. The importance of JMM cannot be underestimated. It gives programmers an easy way to reason about concurrent programs. It was the first of its kind and solidified Java as the first choice for concurrent programming. It also served as a basis for the memory model that was added to C++ with the C++11 standard.
- Rich standard library
Programming language status among programmers is based not only on technical merits but also on how fast and how efficiently a task can be accomplished with it. Java comes with a very rich standard library, that makes it easier to and faster to solve practical problems than other statically typed languages.
- Java is open source
Shortly after the acquisition of Java from Sun, Oracle open-sourced both Java and the JVM, allowing for developers to contribute features and improvements. This move gave a huge boost to the entire Java ecosystem.
- Plenty of jobs with Java
Sometimes programmers get too obsessed with technical merits and advantages of one language vs another, and while this is important, ultimately the language is just a tool. Often the choice of what to learn is dictated by the number of available jobs with this technology. And there is an abundance of jobs with Java.
- A Huge number of libraries
Whatever library one can think of, there is a good chance that someone already created one for Java.
It was never hard to install JVM whether it is JDK or JRE. Yet recently, with the creation of SdkMan project (not part of Java or JVM), installing Java or switching between multiple versions of JDK on the same machine became even easier. One no longer has to fiddle with changing environmental variables and paths.
Importance of the JVM
As important as Java is as a language, it wouldn’t be where it is today if not for the Java Virtual Machine. The JVM not only gave portability to the language but also provided a host of other services that made programmers more productive. It freed them from some of the most tedious and error-prone tasks.
There are numerous benefits JVM brought to the Java language. These are the three most prominent ones that set Java apart from other languages:
- Garbage collector (GC) – since the very first release of Java, it came with automatic memory management that is part of the JVM. It freed the programmers from the burden to manage memory directly and helped them avoid the most common errors that account for most of the vulnerabilities found in modern software.
- Just in time compiler (JIT) – originally the JVM executed each bytecode individually. It was easy to implement, but the performance wasn’t great. That changed with the introduction of the HotSpot Just In Time compiler (JIT) in the early 2000s. It was designed to analyse/profile the code as it runs on the JVM and then, using the collected profiling information to compile the most heavily used (a.k.a hot) parts of the program to native code. The performance achieved from JIT-ed code was comparable to one of the statically compiled languages like C and C++.
- Bytecode – Java bytecode is fairly unique among modern languages. It greatly simplifies the implementation of features as Aspect-Oriented Programming (AOP) and implementation of a multitude of dynamic languages running on the JVM as JRuby and Jython. It also makes compiled Java class file compact in size.
Current and Future Improvements to Java and JVM
In recent years the enhancements and improvements to Java language and the JVM keep pouring in and further solidify Java position as one the best modern programming languages.
- Lambdas were added to the language in Java 8, bringing it a step closer to some popular functional languages. This allows more succinct and expressive code.
- New garbage collectors – in most recent version of the JVM two new garbage collectors ZGC and Shenandoah were added. Significant improvements were made to the default G1 garbage collector as well. The result of all this work is that developers can set the maximum amount of time the garbage collector is allowed to spend in Stop-The-World (STW) pause and the collector will do its best adhere to this constraint.
- Project Loom – it is aimed at adding support for Fibers (also called green or non-native threads) to JVM to solve the issue when a blocked thread in Java will block a native thread in the OS and slow it down. Work on the project is still ongoing but one can test the second version of the project in the latest JDK 15 developer builds.
- Project Valhalla – aims at developing and adding support of value types to the language. These are objects that are immutable and contain only data. They are extremely important for performance in areas like streaming, low latency applications and data processing. These are already available with the addition of records to Java 14.
- Java Mission Control (JMC) and Java Flight Recorder (JFR) – are the preferred state of the art monitoring tools for modern JVM. JFR defines a huge and extensible set of events for the JVM that the developer can choose from to monitor its behaviour. It is very low overhead and can be safely used to monitor JVM in production deployments. As of JVM 14, a streaming mode was added to JFR where the events no longer need to be stored locally on the host but can be streamed to another host for processing. Regardless if events are stored locally or remotely JMC then can be used to visualize and analyse the collected performance data.
- Foreign-memory Access API – aims at replacing the legacy Unsafe API for requesting and working with native memory that is not part of the JVM heap. It is based on the previous work in the JVM that implements Var and Method handles. The new memory API tries to avoid the dangers that stem from the usage of internal and undocumented Unsafe API.
The Future of Java
With contributions of hundreds and thousands of developers to OpenJDK and the new six-month release cycle, the amount of innovation and new features added both to the language and the JVM has never been greater. After a quarter of a century, the future of Java today is brighter than ever.