Java 9 Features

Unicode 8.0

Java supports Unicode 8.0 in its new Java 9 version, previously Unicode 6.2 was used.
Java 9 supports, Unicode 6.3, 7.0 and 8.0 standards that combined introduced 10,555 characters, 29 scripts, and 42 blocks.

Compact Strings

In new version, Java uses more space-efficient internal representation for strings. In previous versions, the String was stored in char array and takes two bytes for each character. Now, the new internal presentation of the string is a byte class.

Interface Private Methods

In Java 9, we can create private methods inside an interface. An 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();
}
}



Java 9 try-with-resource Example
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);
}
}
}

Anonymous Inner Classes Improvement

abstract class ABCD
abstract T show(T a, T b);
}
public class TypeInferExample {
public static void main(String[] args) {
ABCD a = new ABCD<>() { // diamond operator is empty, compiler infer type
String show(String a, String b) {
return a+b;
}
};
String result = a.show("Java","9");
System.out.println(result);
}
}




Comments