Java 9 Features and Enhancements
1. Improved Javadoc
2. Factory methods for collections(like List, Map, Set and Map.Entry):
Many a times, you want to create a collection (e.g., a List or Set) in your Java program and fill it with some elements. That leads to repetitive coding where you instantiate the collection, followed by several ‘add’ calls. With Java 9, several so-called collection factory methods have been added.
List and Set interfaces have “of()” methods to create an empty or no-empty Immutable List or Set objects as shown below:
Empty List example:
List and Set interfaces have “of()” methods to create an empty or no-empty Immutable List or Set objects as shown below:
Empty List example:
List immutableList = List.of();
Non-Empty List example:
List immutableList = List.of("one", "two", "three");
Map has two set of methods: of() methods and ofEntries() methods to create an Immutable Map object and an Immutable Map.Entry object respectively.
Empty Map Example:
Empty Map Example:
jshell> Map emptyImmutableMap = Map.of() emptyImmutableMap ==> {}
Non-Empty Map Example:
jshell> Map nonemptyImmutableMap = Map.of(1, "one", 2, "two", 3, "three") nonemptyImmutableMap ==> {2=two, 3=three, 1=one}
3. JShell: the interactive Java REPL
Oracle Corp. has introduced a new tool called “jshell”. It stands for Java Shell and also known as REPL (Read Evaluate Print Loop). Many languages already feature an interactive Read-Eval-Print-Loop, and Java now joins this club. It is used to execute and test any Java Constructs like class, interface, enum, object, statements etc. very easily. You can launch jshell from the console and directly start typing and executing Java code. The immediate feedback of jshell makes it a great tool to explore APIs and try out language features.
4. Stream API Improvements:
In Java SE 9, Oracle Corp. has added four useful new methods to java.util.Stream interface. As Stream is an interface, all those new implemented methods are default methods. It allows you to create declarative pipelines of transformations on collections. There are four new methods added to the Stream interface: dropWhile, takeWhile, ofNullable. The iterate method gets a new overload, allowing you to provide a Predicate on when to stop iterating.
5. Private methods in Interfaces:
In Java 8, we can provide method implementation in Interfaces using Default and Static methods. However we cannot create private methods in Interfaces. To avoid redundant code and more re-usability, Oracle Corp. introduced private methods in Java SE 9 Interfaces. From Java SE 9 on-wards, we can write private and private static methods too in an interface using ‘private’ keyword.
public interface Card{
private Long createCardID(){
// Method implementation goes here.
}
private static void displayCardDetails(){
// Method implementation goes here.
}
Java 10 Features and Enhancements
1. Local Variable Type Inference
Java has now
var
style declarations. It allows you to declare a local variable without specifying its type. The type of variable will be inferred from type of actual object created. It claims to be the only real feature for developers in JDK 10. e.g.var str = "Hello world"; //or String str = "Hello world";
In above example, both statements are equivalent. In first statement, type of
str
is determined by type of assignment which of String
type.2. Heap Allocation on Alternative Memory Devices
The goal of this change is to enable the HotSpot VM to allocate the Java object heap on an alternative memory device, such as an NV-DIMM, specified by the user.
To allocate the heap in such memory we can add a new option,
-XX:AllocateHeapAt=<path>
. This option would take a path to the file system and use memory mapping to achieve the desired result of allocating the object heap on the memory device. The existing heap related flags such as -Xmx
, -Xms
, etc., and garbage-collection related flags would continue to work as before.3. Garbage-Collector Interface
In earlier JDK structure, the components that made up a Garbage Collector (GC) implementation were scattered throughout various parts of the code base. It’s changed in Java 10. Now, it is a clean interface within the JVM source code to allow alternative collectors to be quickly and easily integrated. It will improve source-code isolation of different garbage collectors.
This is purely refactoring. Everything that worked before needs to work afterwards, and performance should not regress.
4. Parallel Full GC for G1
Java 9 introduced G1 (garbage first) garbage collector. The G1 garbage collector is designed to avoid full collections, but when the concurrent collections can’t reclaim memory fast enough. With this change, a fall back full GC will occur.
The current implementation of the full GC for G1 uses a single threaded mark-sweep-compact algorithm. This change will parallelize the mark-sweep-compact algorithm and use the same number of threads. It will be triggered when concurrent threads for collection can’t revive the memory fast enough.
The number of threads can be controlled by the
-XX:ParallelGCThreads
option.4. Application Class-Data Sharing
The goal of this feature is to improve the startup footprint, extends the existing Class-Data Sharing (“CDS”) feature to allow application classes to be placed in the shared archive.
Class-Data Sharing, introduced in JDK 5, allows a set of classes to be pre-processed into a shared archive file that can then be memory-mapped at runtime to reduce startup time. It can also reduce dynamic memory footprint when multiple JVMs share the same archive file.
Currently CDS only allows the bootstrap class loader to load archived classes. Application CDS allows the built-in system class loader, the built-in platform class loader, and custom class loaders to load archived classes.
Specify the
-XX:+UseAppCDS
command-line option to enable class data sharing for the system class loader, the platform class loader, and other user-defined class loaders.5. New Added APIs and Options
73 new API’s has been added in Java 10. Let’s go through few of them:
API | Description |
---|---|
Optional.orElseThrow() | A new method orElseThrow has been added to the Optional class. It is synonymous with and is now the preferred alternative to the existing get method. |
List.copyOf , Set.copyOf , and Map.copyOf | These methods create new collection instances from existing instances. |
Collectors.toUnmodifiableList , Collectors.toUnmodifiableSet , Collectors.toUnmodifiableMap | These methods allow the elements of a Stream to be collected into an unmodifiable collection |
--jdk.disableLastUsageTracking | To disable JRE last usage tracking for a running VM. |
--add-stylesheet | Provides support for the use of multiple stylesheets in the generated documentation. |
--main-stylesheet | To help distinguish the main stylesheet from any additional stylesheets. |
@summary Tag | Added to explicitly specify the text used as the summary of the API description. By default, the summary of an API description is inferred from the first sentence. |
0 comments :
Post a Comment