- Java 8 -
Java 8 was a massive release and you can find a list of all features at the Oracle website. There’s two main feature sets I’d like to mention here, though:
Language Features: Lambdas etc.
Before Java 8, whenever you wanted to instantiate, for example, a new Runnable, you had to write an anonymous inner class like so:
Runnable runnable = new Runnable(){
@Override
public void run(){
System.out.println("Hello world !");
}
};
With lambdas, the same code looks like this:
Runnable runnable = () -> System.out.println("Hello world two!");
You also got method references, repeating annotations, default methods for interfaces and a few other language features.
Collections & Streams
In Java 8 you also got functional-style operations for collections, also known as the Stream API. A quick example:
List<String> list = Arrays.asList("franz", "ferdinand", "fiel", "vom", "pferd");
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:
list.stream()
.filter(name -> name.startsWith("f"))
.map(String::toUpperCase)
.sorted()
.forEach(System.out::println);
- Java 9 -
Java 9 also was a fairly big release, with a couple of additions:
Collections
Collections got a couple of new helper methods, to easily construct Lists, Sets and Maps.
List<String> list = List.of("one", "two", "three");
Set<String> set = Set.of("one", "two", "three");
Map<String, String> map = Map.of("foo", "one", "bar", "two");
Streams
Streams got a couple of additions, in the form of takeWhile,dropWhile,iterate methods.
Stream<String> stream = Stream.iterate("", s -> s + "s")
.takeWhile(s -> s.length() < 10);
Optionals
Optionals got the sorely missed ifPresentOrElse method.
user.ifPresentOrElse(this::displayAccount, this::displayLogin);
Interfaces
Interfaces got private methods:
public interface MyInterface {
private static void myPrivateMethod(){
System.out.println("Yay, I am private!");
}
}
Other Language Features
And a couple of other improvements, like an improved try-with-resources statement or diamond operator extensions.
JShell
Finally, Java got a shell where you can try out simple commands and get immediate results.
% jshell
| Welcome to JShell -- Version 9
| For an introduction type: /help intro
jshell> int x = 10
x ==> 10
HTTPClient
Java 9 brought the initial preview version of a new HttpClient. Up until then, Java’s built-in Http support was rather low-level, and you had to fall back on using third-party libraries like Apache HttpClient or OkHttp (which are great libraries, btw!).
With Java 9, Java got its own, modern client - although in preview mode, which means subject to change in later Java versions.
Project Jigsaw: Java Modules and Multi-Release Jar Files
Java 9 got the Jigsaw Module System, which somewhat resembles the good old OSGI specification. It is not in the scope of this guide to go into full detail on Jigsaw, but have a look at the previous links to learn more.
Multi-Release .jar files made it possible to have one .jar file which contains different classes for different JVM versions. So your program can behave differently/have different classes used when run on Java 8 vs. Java 10, for example.
If you want more Java 9 practice
Again, this is just a quick overview of Java 9 features and if you want more thorough explanations and exercises, have a look at the Java 9 core features course.
- Java 10 -
There have been a few changes to Java 10, like Garbage Collection etc. But the only real change you as a developer will likely see is the introduction of the "var"-keyword, also called local-variable type inference.
Local-Variable Type Inference: var-keyword
// Pre-Java 10
String myName = "Marco";
// With Java 10
var myName = "Marco"
Feels Javascript-y, doesn’t it? It is still strongly typed, though, and only applies to variables inside methods (thanks, dpash, for pointing that out again).
- Java 11 -
Java 11 was also a somewhat smaller release, from a developer perspective.
Strings & Files
Strings and Files got a couple new methods (not all listed here):
"Marco".isBlank();
"Mar\nco".lines();
"Marco ".strip();
Path path = Files.writeString(Files.createTempFile("helloworld", ".txt"), "Hi, my name is!");
String s = Files.readString(path);
Run Source Files
Starting with Java 10, you can run Java source files without having to compile them first. A step towards scripting.
ubuntu@DESKTOP-168M0IF:~$ java MyScript.java
Local-Variable Type Inference (var) for lambda parameters
The header says it all:
(var firstName, var lastName) -> firstName + lastName
HttpClient
The HttpClient from Java 9 in its final, non-preview version.
Other stuff
Flight Recorder, No-Op Garbage Collector, Nashorn-Javascript-Engine deprecated etc.
- Java 12 -
Java 12 got a couple new features and clean-ups, but the only ones worth mentioning here are Unicode 11 support and a preview of the new switch expression, which you will see covered in the next section.
- Java 13 -
You can find a complete feature list here, but essentially you are getting Unicode 12.1 support, as well as two new or improved preview features (subject to change in the future):
No comments:
Post a Comment