Code Pumpkin

Java 9 Features

April 17, 2017
Posted by Pumpkin
Subscribe

Java 9 has been released on 21st Sept, 2017. In this article we will look into some of the most awaiting java 9 features with examples.

You can download JDK 1.9 from here based on your OS Platform requirements.

Some of the important java 9 features are:

  1. JShell: The Java Shell (Read-Eval-Print Loop)
  2. Convenience Factory Methods for Collections
  3. Private methods in Interfaces


1. JShell: The Java Shell (Read-Eval-Print Loop)

JShell is command line tool to include REPL (Read-Eval-Print Loop) into Java programming language i.e. 

                     Java + REPL = JShell

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.

Earlier in Java, if you want to print "Hello, world!", you need to create public class and public static main method to test it.

JShell is useful to learn Java very easily. It does not require any IDEs or Editors to execute simple Java 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.

Example


C:\Users\codePumpkin>jshell -v
|  Welcome to JShell -- Version 9-ea
|  For an introduction type: /help intro


jshell> int a = 10
a ==> 10

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


If you want to know more about JShell and REPL tool, Please go through our post JShell: The Java Shell And REPL in Java 9 
 


2. Convenience Factory Methods for Collections

Java is often criticized for its verbosity. Creating a small, unmodifiable collection (say, a set) involves lots of code. For Example,


Set<String> set = new HashSet<>();
set.add("a");
set.add("b");
set.add("c");
set = Collections.unmodifiableSet(set);

Also we are not saying this is the only way to create an unmodifiable collection. We can also use Using copy constructor of List interface, Double Brace Technique  or Java Stram API  to intialize collections. But all of these ways are still verboss and tedious.

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 ListSet and Map respectively. The method is named of(…) for all the three interfaces.

Example

The signature and characteristics of ListSet and Map factory methods are same:


static <E> List<E> of(E e1, E e2, E e3)
static <E> Set<E>  of(E e1, E e2, E e3)
static <E> Map<E> of(K k1, V v1, K k2, V v2, K k3, V v3)

usage of the methods:


List<String> list = List.of("a", "b", "c");
Set<String> set = Set.of("a", "b", "c");
Map<String, Integer> cities = Map.of("a", 1, "b", 2,"c",3);

For detailed understanding on this topic, Please go through our post Convenience Factory Methods for Collections


3. Private Methods in Interfaces

In Java 7 and all earlier versions, interfaces were simple. They could only contain public abstract methods.

Java 8 changed this. From Java 8, you can have public static methods and public default methods.

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.

In java 9, additional two members can be included in interface:

  • Private methods
  • Private Static methods


Why private methods?

Sometimes, when we want to introduce several default and static methods in interface (in Java8) , they may share some common code base.

For Example, Multiple Default methods have redundent code for connecting to database. It would be good if we can provide some common method for database connection.

What if we will declare this common method public?

If we make such method public, it is exposed to outside world. anybody can use that method to connect to database and perform some illegal db operations. To prevent such action we need to make this method private.

In short, if multiple default methods need to share code, a private interface method would allow them to do so, but without exposing that private method and all its "implementation details" via the interface.

For detailed understanding on this topic, Please go through our post Private Methods in Interfaces.

That's it for Java 9 Features .

Happy Learning smiley

Tags: , , , ,


Comments and Queries

If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code class="java"> 
String foo = "bar";
</code></pre>
For more information on supported HTML tags in disqus comment, click here.

Total Post : 82
Subscribe
Contribute Your Articles

Interview Experiences

Related Books

Like Us On Facebook

Alexa Page Rank