Java 1.9
Java 1.9
Oracle has released Java 9 with rich set of new features. It includes various upgrades to the Java programming,
JVM, Tools and libraries. In this tutorial, we will discuss all the main features that are given below.
o Platform Module System (Project Jigsaw)
o Interface Private Methods
o Try-With Resources
o Anonymous Classes
o @SafeVarargs Annotation
o Collection Factory Methods
o Process API Improvement
o New Version-String Scheme
o JShell: The Java Shell (REPL)
o Process API Improvement
o Control Panel
o Stream API Improvement
o Installer Enhancement for Microsoft windows and many more
Java 1.9
In Java 9, we can create private methods inside an interface. Interface allows us to declare private methods that help
to share common code between non-abstract methods.
Before Java 9, creating private methods inside an interface cause a compile time error. The following example is
compiled using Java 8 compiler and throws a compile time error.
Java 9 Private Interface Methods Example
interface Sayable{
default void say() {
saySomething();
}
// Private method inside interface
private void saySomething() {
System.out.println("Hello... I'm private method");
}
}
public class PrivateInterface implements Sayable {
public static void main(String[] args) {
Sayable s = new PrivateInterface();
s.say();
}
}
Example:2
interface Sayable{
default void say() {
saySomething(); // Calling private method
sayPolitely(); // Calling private static method
}
// Private method inside interface
private void saySomething() {
System.out.println("Hello... I'm private method");
}
// Private static method inside interface
private static void sayPolitely() {
System.out.println("I'm private static method");
}
}
public class PrivateInterface implements Sayable {
public static void main(String[] args) {
Sayable s = new PrivateInterface();
s.say();
}
}
================================================================
Java 9 Try With Resource:
Java introduced try-with-resource feature in Java 7 that helps to close resource automatically after being used.
In other words, we can say that we don't need to close resources (file, connection, network etc) explicitly, try-with-
resource close that automatically by using AutoClosable interface.
In Java 7, try-with-resources has a limitation that requires resource to declare locally within its block.
Example Java 7 Resource Declared within resource block
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class FinalVariable {
public static void main(String[] args) throws FileNotFoundException {
try(FileOutputStream fileStream=new FileOutputStream("javatpoint.txt");){
String greeting = "Welcome to javaTpoint.";
byte b[] = greeting.getBytes();
fileStream.write(b);
System.out.println("File written");
}catch(Exception e) {
System.out.println(e);
}
}
}
This code executes fine with Java 7 and even with Java 9 because Java maintains it's legacy.
But the below program would not work with Java 7 because we can't put resource declared outside the try-with-
resource.
To deal with this error, try-with-resource is improved in Java 9 and now we can use reference of the resource that is
not declared locally.
In this case, if we execute the above program using Java 9 compiler, it will execute nicely without any compile
error.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class FinalVariable {
public static void main(String[] args) throws FileNotFoundException {
FileOutputStream fileStream=new FileOutputStream("javatpoint.txt");
try(fileStream){
String greeting = "Welcome to javaTpoint.";
byte b[] = greeting.getBytes();
fileStream.write(b);
System.out.println("File written");
}catch(Exception e) {
System.out.println(e);
}
}
}
========================================================================
Java 9 Anonymous Inner Classes Improvement
Java 9 introduced a new feature that allows us to use diamond operator with anonymous classes. Using the diamond
with anonymous classes was not allowed in Java 7.
In Java 9, as long as the inferred type is denotable, we can use the diamond operator when we create an anonymous
inner class.
Data types that can be written in Java program like int, String etc are called denotable types. Java 9 compiler is
enough smart and now can infer type.
==========================================================
SafeVarargs Annotation
It is an annotation which applies on a method or constructor that takes varargs parameters. It is used to ensure that
the method does not perform unsafe operations on its varargs parameters.
It was included in Java7 and can only be applied on
Final methods
Static methods
Constructors
From Java 9, it can also be used with private instance methods.
Note: The @SafeVarargs annotation can be applied only to methods that cannot be overridden. Applying to the
other methods will throw a compile time error.
Q: What happens? If we compile the following code by using older versions of Java.
Output: SafeVar.java:6: error: Invalid SafeVarargs annotation. Instance method display(List<String>...) is not
final.
private void display(List<String>... products) {
^
Note: SafeVar.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
====================================
Java 9 Factory Methods
Java 9 Collection library includes static factory methods for List, Set and Map interface. These methods are useful to
create small number of collection.
Suppose, if we want to create a list of 5 elements, we need to write the following code.