img

Top Java 9 Features

Late on July 2017, Java 9 (Open JDK) is released. With Java, there is some of the best important features is also released. In this article, we’re going to look some of them best features of Java.

  1. JShell: Java 9 REPL (Read Eval Print Loop)

JShell is the command line tool which included Java in Java with REPL. A Read-Eval-Print Loop (REPL) is an interactive programming tool which loops, continually reading user input, evaluating the input, and printing the value of the input or a description of the state change the input caused.

Previously in Java, if you need to create a Java program i.e. Hello World!”, you need to create the public class and method to print it.  But JShell is saving your time here. JShell is useful to learn the Java for the beginner. JShell doesn’t require any IDEs or any notepad Editors to run your basic programs. It is very useful for Beginners and Experts to use it to learn and evaluate new features.

Scala, Ruby, JavaScript, Haskell, Clojure, and Python all have REPLs and all allow small initial programs. JShell adds this REPL functionality to the Java platform.

  1. New Factory Methods for Collections

Often you want to create a collection (e.g., a List or Set) in your code and directly populate it with some elements. That leads to the repetitive code where you instantiate the collection, followed by several ‘add’ calls. To overcome this in Java 9, static methods have been introduced on ListSet, and Map interfaces which take the elements as arguments and return an instance of List,  Set and  Map respectively The method is named of(…) for all the three interfaces.


Set<Integer> ints = Set.of(1, 2, 3);

List<String> strings = List.of("first", "second");

  1. Private Methods in Interfaces

In Java 7 or in earlier versions, interfaces were simple. They could only contain public abstract methods. But from Java 8, you can have public static methods and public default methods and now Java 9 is adding private methods on interfaces.

Thus, methods can be public or private (with the default being public if not specified). Private methods can be static or instance. In both cases, the private method is not inherited by sub-interfaces or implementations.


public interface MyInterface {

       void normalInterfaceMethod();

       default void interfaceMethodWithDefault() { init(); }

       default void anotherDefaultMethod() { init(); }

       private void init() { 

           System.out.println(“Initializing”); 

       }

}

In Java 9, there are two new features are added in Interfaces which are Private Methods and Private static methods.

Sometimes, when we want to introduce several defaults and static methods in the interface (like in Java8), they may share some common code base. For Example, Multiple Default methods have redundant code for connecting to the database. It would be good if we can provide some common method for the database connection. But if we make such method public it might expose to the outside of the code and someone can easily use and can connect to the database and make illegal operation as well, and that’s the reason those methods made private only.

Click here to download Java 9 and enjoy its features.

 

 

 

  • Facebook
  • Twitter
  • Google+
  • Linkedin
  • Pinterest

Leave a Comment

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

It is main inner container footer text
Accessibility