The Java 9 project coin language changes include diamond with anonymous classes, private methods in interfaces, removing ‘_’ as a single character identifier, @SafeVargs on private instance methods and effectively final variables as resources in try-wth-resources statement.
1. Diamond with anonymous classes
Prior to Java 1.6, constructors did not have inferred type inference. This code would work in java 1.6
List<String> a = new ArrayList<String>();
but, omitting type on the constructor would not work.
List<String> a = new ArrayList<>();// does not compile.
java7 fixed that and we could use the diamond. However, the diamond can’t be used with anonymous classes. For example, lets create an anonymous classes
List<String> c = new ArrayList<>() {}
This is an anonymous class that extends an ArrayList. This would not compile in Java 7 and Java 8, however, Java 9 allows this as part of project coin changes.
2. private methods in interfaces
Java 8 introduced the concept of default methods. These are methods in interfaces that have implementation.
package com.st.java9; public interface PrivateMethods { int methodA(); default void methodB() {} private void methodC() {} }
In the example above methodB has an implementation and is a default method. methodC has an implementation but is a private method. They can be used by
other private methods and default methods in the interface.
3. Removing ‘_’ as a single character identifier
Versions prior to 9 allowed a variable called ‘_’ (underscore). Java 9 disallows that. Java 1.8 started using the underscore as a keyword and the compiler gave a warning. Java 9 gives a compile error.
4. @SafeVargs on private instance methods
When a method declares a method with parameterized variable argument, the compiler gives a warning . For example
public <T> void method1(T... a) {}
For this method the compiler gives a warning on a that says ‘Type safety: Potential heap pollution via varargs parameter a’. This is because java internally converts T… a to T[], but because java does not allow parameterized arrays the type is erased the it ends up as Object[]. This can potentially cause heap pollution. If you are sure that your method handles the vararg safely, you can annotate the method with @SafeVargs
annotation and this will remove the compiler warning. Java 9 allows using the @SafeVargs annotation on private instance methods.
5. effectively final variables as resources in try-wth-resources statement
try-with-resources allows using auto-closeable objects inside the try block, so that they are automatically closed. For example, prior to Java9, this is how you would use a BufferedReader
BufferedReader reader = new BufferedReader(new FileReader(new File(""))); try (BufferedReader r1 = reader) { }
However, with Java 9, you don’t need to redeclare a variable if it is final or effectively final.
This works with Java 9
BufferedReader reader = new BufferedReader(new FileReader(new File(""))); try (reader) { }
However, note that the varialbe has to be effectively final or final, so this does not work.
BufferedReader reader = new BufferedReader(new FileReader(new File(""))); reader = new BufferedReader(new FileReader(new File(""))); try (reader) { }
This completes our discussion on the language changes in Java 9. In the next article we will look at JLink tool.