Java 10 and 11 Features

Author: Shazin Sadakath


We talked about features of Java 10 during previous article, But since Oracle took over there has been some changes to the Java JDK release cycle. One of the most notable ones are the 6 months long releases. Which means Java 11 is not that far away (in fact can expect to be released in early 2019). So it is time that we look at some of the features of both Java 10 and Java 11.

Features of Java 10

Local Variable Syntax 

String name = "Shazin”;
var name = "Shazin";

List list = List.of(1, 2, 3);
var list = List.of(1, 2, 3);

The var is a placeholder data type which can be used replace actual data types when declaring local variables which makes the code look concise. This is however is just syntactical sugar. All four lines above compile to generate same byte code. These declarations are not immutable (as opposed to Kotlin's val). Now you can make code even more readable by moving the generic data type information to the initializing part like below:

var map = new HashMap();

However these are stricly allowed to define local variable so not allowed to declare instance variables not even inside of nested/inner classes.

Unmodifiable Collections

Unmodifiable collections can be created out of existing collections like List, Set and Map and Stream.collect terminal method can also be used to create unmodifiable collections. 

Copying Stream Terminal operations
List.copyOf Collectors.toUnmodifiableList
Set.copyOf Collectors.toUnmodifiableSet
Map.copyOf Collectors.toUnmodifiableMap

Faster Default Garbage Collection

The default Garbage Collector for Java 10 is G1 GC with parallel implementation. Can learn more about this from this article.

Unicode 9 and 10 support

This means alot of letters from big languages like Chinese and Emojis are also supported.

Features of Java 11

var in Lambda

When defining Lambdas you can ommit the datatype altogether as long as you don't use annotations for the Lambda arguments like following:

Consumer c = s -> System.out.println(s);

But when you use annotation you will be forced to define the datatype of s. Now with this var in Lambda feature you can concisely write code with annotations as below:

Consumer c = (@NotNull var s) -> System.out.println(s);

The only catch is that when there is are multple parameters to a Lambda they must all have the var datatype or non. It is not possible have for some parameters and ignore others like in the following case which is invalid:

(var s, l) -> l.contains(s);

Introducing Eplison Garbage Collection

This is GC never reclaims memory and will run until the heap space runs out. The GC never runs. This is ideal for permormance/stress testing and very small running programs. Can be triggered with either of following JVM arguments:

-XX:+UseEpsilonGC
-XX:+UseNoGC

Introducing Z Garbage Collection

This is an experimental GC which is only supported in Linux. This has 10ms pause times. Can be triggered with either of following JVM arguments:

-XX:+UnlockExperimentalVMOptions
-XX:+UseZGC

HttpClient is no longer in Incubation 

HttpClient will be packaged and released as part of the JDK to be used to make HTTP calls like below:

var uri = "http://www.techtalks.lk"; 
var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder()
                         .uri(URI.create(uri))
                         .build();
var response = client.send(request, BodyHandlers.ofString());
System.out.println(response.body());

New Java Launcher

Newbies of Java when learning it have to use the following commands to compile and run their initial learning programs:

javac Program.java
java Program

These commands have some pain points for learning where the first command expects the .java extenstion to be specified where as the second command expects only name not .class extension. This can cause confusions and problem for students. For this reason the following command is valid from Java 11 onwards:

java Program.java

Which will compile and run the java program in memory but can only be used for a single file programs. 

Answer

Since var is kept as a placeholder datatype instead of a reserved keyword it provides backward compatibility meaning anyone can still have older code which has variable names as var in their code without a problem. Also following code compiles without a problem.

var var = "This is a var";

References



Tags: Java11 Java10 Features
Views: 821
Register for more exciting articles

Comments

Please login or register to post a comment.


There are currently no comments.