Java9 Features – Part I | JSHELL | Interface | Examples

Java9 is ready to roll on the minds of developer after Java8 brings revolution by introducing lambda. Most of the developers start compiling code with higher version of Java while using same old coding standards.

The fact is we do not have implementation knowledge of new Java version features. I got an opportunity to attend a seminar on Java9 and I am sharing my learning and earning as a developer.



Few of its features are :-

  1. Java + REPL = JShell.
  2. Jigsaw Module.
  3. Enhancement in Process API.
  4. Try with Resources update.
  5. Diamond Operator update.
  6. Deprecation of Underscore.
  7. Private Methods in Interface.

I. JShell :-
ShellJava Shell –  Sole aim is to make it more efficient and to offer rivalry to “JavaScript”. Much like JavaScript, you can just write a single line of code and get it executed.

You need not to compose class and main method to test small piece of code.

Get into JShell mode by writing jshell and you are all ready to go.

D:>jshell
|  Welcome to JShell -- Version 9-ea
|  For an introduction type: /help intro

jshell> System.out.println("Say Hello to JShell")
Say Hello to JShell

jshell>int sum=10;
sum ==> 10

jshell> System.out.println("sum value = " + sum )
sum value = 10

 

II. Try with Resource updation :-
Try with Resources was first introduced in Java7 and since then there were lot of improvements. Below is difference between establishing JDBC connection with Java8 and Java9:-

******************Before JAVA 9*******************
try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
	Statement stmt = conn.createStatement()) {
//some code inside
}
		

****************In JAVA 9*************************
Connection conn=null;
Statement stmt=null;
		
try (conn = DriverManager.getConnection(DB_URL, USER, PASS);
	stmt = conn.createStatement()) {
//some code inside
}

 

III. Error while using Underscore
Java Community make it clear in Java8 by deprecating underscore and now in Java9 underscore will throw  an error.

UPDATE:- Alone underscore will throw an error and not names containing underscore.

int count_sum=0; //valid in Java9
String java_new_features; //valid
String _="Java9"; //error in java9-only underscore

 



 

IV. Interface can have Private Methods – 
Interface assumes a noteworthy part in programming and also it is used to design  API and to implement Remote Method Invocation. Interface has seen many changes in JAVA7 , JAVA8 and now JAVA9. Below is the difference:-

interface example{
	int sum=0; // public static final int sum=0;
	private void fun(){
		//some code allowed
	}
}

Read More :- But what is the need of private method in interface?

 

9 thoughts on “Java9 Features – Part I | JSHELL | Interface | Examples

  1. I believe item III is incorrect. As far as I know, using underscore (_) as part of a variable name will not throw an error. An error will be thrown only if the underscore character is used as the only character in the variable NAME.

    For example:

    String my_String = “Hello World!” // valid in Java 9.
    String _ = “Goodbye!” // Error in Java 9.

    https://bugs.openjdk.java.net/browse/JDK-8065599

    The link above explains this issue. It states “The use of the variable name _ in any context is discouraged. Future versions of the Java programming language may reserve this name as a keyword and/or give it special semantics. ” – This statement extracted from the bug description clearly states that it is the use of the variable NAME _ and not “the use of _ in the variable name.” as you explained here.

  2. Number III is wrong.

    int count_sum=0; //not illegal in Java 9
    int _ = 0; //this is the one that is now illegal in Java 9

  3. May I know what is the use of private methods inside an interface? Is this feature just to facilitate improved coding of default methods of the interfaces or something else?

    1. I think it is not for improving coding style. you can see clear difference here . Here I described the use of private method inside interface in detail.

Leave a Reply

Your email address will not be published. Required fields are marked *