Some interesting features inroduced in in JAVA 9
A) Interface Private Method
Interfaces in the upcoming JVM version can have private methods, which can be used to split lengthy default methods:
B)JShell Command Line Tool
JShell is read–eval–print loop – REPL for short.
Simply put, it’s an interactive tool to evaluate declarations, statements, and expressions of Java, together with an API. It is very convenient for testing small code snippets, which otherwise require creating a new class with the main method.
The jshell executable itself can be found in <JAVA_HOME>/bin folder:
jdk-9\bin>jshell.exe
Welcome to JShell -- Version 9
For an introduction type: /help intro
jshell> "This is my long string. I want a part of it".substring(8,19);
$5 ==> "my long string"
The interactive shell comes with history and auto-completion; it also provides functionality like saving to and loading from files, all or some of the written statements:
C) Immutable Set
java.util.Set.of() – creates an immutable set of a given elements. In Java 8 creating a Set of several elements would require several lines of code. Now we can do it as simple as:
The Set returned by this method is JVM internal class: java.util.ImmutableCollections.SetN, which extends public java.util.AbstractSet. It is immutable – if we try to add or remove elements, an UnsupportedOperationException will be thrown.
D)Optional To Stream
A) Interface Private Method
Interfaces in the upcoming JVM version can have private methods, which can be used to split lengthy default methods:
interface MyInterfacePrivateMethods {
private static String privateMethod1() {
return " static private method";
}
private String privateMethod2() {
return "instance private method";
}
default void printPrivate() {
String returnValue = privateMethod1();
InterfaceWithPrivateMethods privateMethod = new InterfaceWithPrivateMethods() {
//Any class which implements this interface.
};
returnValue = privateMethod.privateMethod2();
}
}}
B)JShell Command Line Tool
JShell is read–eval–print loop – REPL for short.
Simply put, it’s an interactive tool to evaluate declarations, statements, and expressions of Java, together with an API. It is very convenient for testing small code snippets, which otherwise require creating a new class with the main method.
The jshell executable itself can be found in <JAVA_HOME>/bin folder:
jdk-9\bin>jshell.exe
Welcome to JShell -- Version 9
For an introduction type: /help intro
jshell> "This is my long string. I want a part of it".substring(8,19);
$5 ==> "my long string"
The interactive shell comes with history and auto-completion; it also provides functionality like saving to and loading from files, all or some of the written statements:
C) Immutable Set
java.util.Set.of() – creates an immutable set of a given elements. In Java 8 creating a Set of several elements would require several lines of code. Now we can do it as simple as:
Set<String> strKeySet = Set.of("String1", "String2", "String3");
The Set returned by this method is JVM internal class: java.util.ImmutableCollections.SetN, which extends public java.util.AbstractSet. It is immutable – if we try to add or remove elements, an UnsupportedOperationException will be thrown.
D)Optional To Stream
java.util.Optional.stream() gives us an easy way to you use the power of Streams on Optional elements: List<String> filteredList = listOfOptionals.stream() .flatMap(Optional::stream) .collect(Collectors.toList());