Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java 9 Features (JShell, Immutable List, Set, Map, Private methods in Interfaces etc)

In this post let's see all upcoming exciting features of Java 9, including most awaited REPL (JShell), Immutable List, Set, Map, Private methods in Interfaces and much more.


How to Download, Install and Use Java 9 


Official announcement is yet to come, but here is a small post on what is expected and other exciting stuff wrapped inside; Lets get started:

1) Download Java 9 Beta

To Download, Java 9 early release go to the following Download Link, accept agreement, choose appropriate version and download. (I did it for Ubuntu 64bit)

2) Extract and Use

Move the downloaded version to a directory of your choice, extract it, and sneak into "jdk-9/bin", one can set JAVA_HOME as well to get direct access to these executable.


Java 9 features


1) Java 9 REPL (JShell)

To open JShell go to bin folder of downloaded package and start shell, as shown below:

 cd /opt/jdk-9/bin/
./jshell

May 02, 2017 4:46:52 PM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.
| Welcome to JShell -- Version 9-ea
| For an introduction type: /help intro

jshell>


Now lets test few small statements, looks like Java is now a scripting language too, what else is left ? :)

jshell> String s = "hello world";
s ==> "hello world"

jshell> System.out.print(s);
hello world
jshell>


2) Immutable List, Set, Map

Before Java 9 developers used to get immutable objects of Collections using "Collections.unmodifiableList" kind of methods, these methods are very tedious and verbose approach.

Immutable List In Java8
  // Empty immutable list not possible
List immutableListJava8 = Collections.unmodifiableList(); // compilation error

// Non empty immutable list possible
List immutableListJava8 = Collections.unmodifiableList(Arrays.asList(1,2,3));

Now on with Java 9 about to come, one can use provided new factory methods "of()" to do so, and even empty immutable objects can be created.

Immutable List In Java9
  // Empty immutable list possible
List immutableEmptyListJava9 = List.of();

// Non empty immutable list possible
List immutableEmptyListJava9 = List.of(1,2,3);
Same is the case with Set and Map

Immutable Set in Java 9
  // Empty immutable list possible
Set immutableEmptySetJava9 = Set.of();

// Non empty immutable Set possible
Set immutableEmptySetJava9 = Set.of(1,2,3);
Immutable Map in Java 9
  / Empty immutable map possible
Map immutableEmptyMapJava9 = Map.of();

// Non empty immutable map possible
Map immutableEmptyMapJava9 = Map.of(1,"Apple",2,"Orange");


3) Private methods in Interfaces

Before Java 9 developers were able to add methods in interface with implementation while the methods are either default or static, private methods were not allowed.

In Java 8
 interface Java8{
default void defaultMethod(){
System.out.println("I will compile, just fine");
}

static void staticMethod(){
System.out.println("I will compile, just fine");
}

// will not compile, error
private void privateMethod(){
System.out.println("I will not compile, just fine");
}
}
Now on with Java 9 about to come, one can add even private method body to an interface along with default and private methods.

In Java 9
 interface Java8{
default void defaultMethod(){
System.out.println("I will compile, just fine");
}

static void staticMethod(){
System.out.println("I will compile, just fine");
}

// Introduced in Java 9
private void privateMethod(){
System.out.println("I will compile, just fin");
}
}
Apart from these changes, a number of other exciting changes are proposed some of them are listed below:

1) GC (Garbage Collector) Improvements
2) Stack-Walking API
3) Module System
4) Reactive Streams
5) Process API Improvements
6) Filter Incoming Serialization Data
7) Deprecate the Applet API
8) Indify String Concatenation
9) Enhanced Method Handles
10) Java Platform Logging API and Service
11) Compact Strings
12) Parser API for Nashorn
13) Javadoc Search
14) HTML5 Javadoc


We have seen few important Java 9 features in detail, will cover remaining soon.
Thanks :)



This post first appeared on Java, Struts 2, Spring, Hibernate, Solr, Mahout An, please read the originial post: here

Share the post

Java 9 Features (JShell, Immutable List, Set, Map, Private methods in Interfaces etc)

×

Subscribe to Java, Struts 2, Spring, Hibernate, Solr, Mahout An

Get updates delivered right to your inbox!

Thank you for your subscription

×