Project Coin is a collection of minor features introduced in Java7. The list of features are given below.
a. Strings in switch
b. Binary integral literals and underscores in numeric literals
c. Multi-catch and more precise rethrow
d. Improved type inference for generic instance creation (diamond)
e. try-with-resources statement
f. Simplified varargs method invocation
Java9 provide some more amendments to this list. Following are the amendments.
a. Allow @SafeVarargs on private methods
SafeVarargs annotation assert that the body of the annotated method or constructor does not perform potentially unsafe operations on its varargs parameter. Prior to Java9, this annotation can only be applied to methods which cannot be overridden, including static methods and final instance methods. From Java9 onwards, this annotation can be used on private instance methods also.
b. Allow effectively-final variables to be used as resources in the try-with-resources statement
Prior to Java9, try-with-resources statement requires a fresh variable to be declared for each resource being managed by the statement.
From Java9 onwards, if the resource is referenced by a final or effectively final variable, a try-with-resources statement can manage the resource without a new variable being declared.
App.java
package com.sample.app;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class App {
public static void main(String args[]) throws FileNotFoundException {
File file = new File("/Users/Shared/hello.txt");
Scanner scnr = new Scanner(file);
int lineNumber = 1;
try (scnr) {
{
while (scnr.hasNext()) {
String line = scnr.nextLine();
System.out.println("line " + lineNumber + " :" + line);
lineNumber++;
}
}
}
}
}
c. Allow diamond with anonymous classes if the argument type of the inferred type is denotable.
From Java9, onwards, we can use diamond operator with anonymous classes if the inferred datatype is denotable.
For example, Example interface is defined like below.
public static interface Example<T> {
public void doSomeWork();
}
From Java9 onwards below snippet compile without any errors.
Example example = new Example<>() {
@Override
public void doSomeWork() {
// TODO Auto-generated method stub
}
};
App.java
package com.sample.app;
public class App {
public static interface Example<T> {
public void doSomeWork();
}
public static void main(String args[]) {
Example example = new Example<>() {
@Override
public void doSomeWork() {
// TODO Auto-generated method stub
}
};
}
}
d. _ is not used as a legal identifier.
Prior to Java9, you can use _ as a variable name. Java compiler throws warnings but the program will run.
public class App {
public static void main(String args[]) {
int _ = 20;
System.out.println("_ " + _);
}
}
From Java9 onwards, you are not supposed to use _ as a variable name. Above program will not compile.
e. Support for private methods in interfaces
ArithmeticUtil.java
public interface ArithmeticUtil {
public default int sum(int a, int b) {
print(a, b);
return a + b;
}
private void print(int a, int b) {
System.out.println("Received arguments a : " + a + ", b : " + b);
}
}
Above snippet is valid in Java9. But when you try to compile same snippet in Java8, you will get following error.
ArithmeticUtil.java:8: error: modifier private not allowed here
private void print(int a, int b) {
^
ArithmeticUtil.java:8: error: interface abstract methods cannot have body
private void print(int a, int b) {
^
2 errors
Reference
https://openjdk.java.net/jeps/213
http://openjdk.java.net/projects/coin/
Previous Next Home
No comments:
Post a Comment