After the release of Java 9, a lot has changed in Java World. Starting with the new release train. We, as Java Developers, used to think in a new version of Java as a huge event, with a lot of new features to explore. Unfortunately (or fortunately?) today this event doesn’t have the same prestige.
Now we have a new version of Java every 6 months, meaning that we can have access to new features much faster than before, but on the other hand, became more difficult to keep updated.
The picture below shows an update road-map.
Java 11 was released in September 2018 and is the first LTS (Long-Term Support) version of the new Java era. Starting with Java 11, Oracle will provide JDK releases under the open source GPLv2, and under a commercial license for those using the JDK as part of an Oracle product or service, or who don’t wish to use open source software. Of course, you still have the option to use OpenJDK in your project, which I believe will be the choice of the vast majority of the companies who don’t have enough budget to pay the commercial license.
Most useful features
Despite all the abovementioned changes, for a developer, the most important thing is to know the new features of the new version. If you didn’t have enough time to try any version after java 8, I’ll summarize the most useful features, IMHO.
Modules: Everything is now modular, which means that you can run your applications with just the needed modules, increasing the performance and decreasing the memory consumption, comparing with previous versions.
Interface Private and Static Methods: Java 9 introduced private methods and private static method in interfaces. Improves code re-usability inside interfaces and provide choice to expose only our intended methods implementations to users.
JShell: Interactive tool for learning the Java programming language and prototyping Java code. JShell is a Read-Evaluate-Print Loop (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results. Very useful to test code snippets.
Local-Variable Type Inference: With Java 10, you no longer need to explicitly state the type of a local-variable. You can switch the type to a simple var. This treatment would be restricted to local variables with initializers, indexes in the enhanced for-loop, and locals declared in a traditional for-loop; it would not be available for method formals, constructor formals, method return types, fields, catch formals, or any other kind of variable declaration. Examples:
var list = new ArrayList<String>(); //infers ArrayList<String>var stream = list.stream(); //infers Stream<String>
New String Methods:
- boolean isBlank(): Returns true if the string is empty or contains only white space, otherwise false.
- Stream lines(): Returns a stream of lines extracted from this string, separated by line terminators.
- String repeat(int): Returns a string whose value is the concatenation of this string repeated count times.
- String strip(): same as the well-known trim() but differs in how it differs whitespace.
- String stripLeading(): remove all leading whitespace.
- String stripTrainling(): remove all trailing whitespace.
New Files Method:
- String readString(Path): Reads all content from a file into a string.
- Path writeString(Path, CharSequence, java.nio.file.OpenOption):Write a CharSequence to a file.
What’s coming in Java 12?
Switch Expressions: Today, we need to write a lot of unnecessary code to make a switch statement. The following switch case
Will be written as:
Raw String Literals: Another problem today, is to escape string sequences, making very difficult to read and maintain. With the new Raw String Literals, we can rewrite it like the following example:
Of course, there are a lot of new changes and features. If you want to know more, access OpenJDK for all JEPs (JDK enhancement Proposals).