Java Transformation from Java 7 to Java 18
A lot has changed in Java from its beginnings in 1995 until today. Java 8 was a revolutionary release that put Java back on the pedestal of the best programming languages.
We will go through most of the changes in the Java language that happened from Java 8 in 2014 until today. I will try to be as brief as possible on every feature. The intention is to have a reference for all features between Java 8 and Java 18 inclusively.
Java 8
Java 8 was a massive release, and you can find a list of all features at the Oracle website. There are two main feature sets I’d like to mention here:
1. Language Features: Lambdas
Before Java 8, whenever you wanted to instantiate, for example, a new Runnable, you had to write an anonymous inner class like so:
With lambdas, the same code looks like this:
You also got method references, repeating annotations, default methods for interfaces and a few other language features.
2. Collections & Streams
In Java 8 you also got functional-style operations for collections, also known as the Stream API. A quick example:
Now pre-Java 8 you basically had to write for-loops to do something with that list.
With the Streams API, you can do the following:
Java 9
Collections
Collections got a couple of new helper methods to easily construct Lists, Sets, and Maps.
Streams
Streams got a couple of additions, in the form of takeWhile,dropWhile,iterate methods.
Optionals
Optionals got the sorely missed ifPresentOrElse method.
Interfaces
Interfaces got private methods:
Java 10
Local-Variable Type Inference: var-keyword
P.S. It is still strongly typed, and only applies to variables inside methods.
Java 11
New String Methods
Adds a few new methods to the String class: isBlank, lines, strip, stripLeading, stripTrailing, and repeat.
New File Methods
it’s now easier to read and write Strings from files
Java 12
Switch Expressions
Firstly, let’s look and at the old syntax:
And now, let’s see the same logic with switch expressions:
It’s also possible to execute code in switch expressions without returning any value:
Java 13
Multiline Strings
We can finally do this in Java:
Java 14
Switch Expressions
Records
There are now record classes, which help alleviate the pain of writing a lot of boilerplate with Java.
Have a look at this pre Java 14 class, which only contains data, (potentially) getters/setters, equals/hashcode, toString.
With records, it can now be written like this:
P.S. this is a preview feature and subject to change in future releases.
Helpful NullPointerExceptions
Finally NullPointerExceptions describe exactly which variable was null.
Pattern Matching For InstanceOf
Previously we had to (cast) our objects inside an instanceof like this
We can now do this, effectively dropping the cast.
Garbage Collectors
The Concurrent Mark Sweep (CMS) Garbage Collector has been removed, and the experimental Z Garbage Collector has been added.
Java 15
Text-Blocks / Multiline Strings
Sealed Classes
This means that while the class is public, the only classes allowed to subclass Shape are Circle, Rectangle and Square.
Java 16
Vector API Incubator
The idea of this API is to provide a means of vector computations that will ultimately be able to perform more optimally (on supporting CPU architectures) than the traditional scalar method of computations.
Let’s look at how we multiply two arrays using vector:
Java 17
Java 17 is the new long-term support (LTS) release of Java, after Java 11.
Passing Objects as switch functions and check for a particular type.
Java 18
@snippet Tag
@snippet tag, specifically designed to display source code. With the @snippet tag, we can write the comment as follows:
Pattern Matching Improvements:
Pattern matching simplifies the implementation of the common pattern of matching an object against a pattern. For example, you may want to check if a given object is an instance of a particular type and then access it in a type-safe manner. This feature improves readability and increases code safety. Pattern matching can be used in switch statements.