Revising current Java features (JAVA-9 to JAVA-14)

Java 9 Features

Creates a immutable list (read-only), cannot modify the list once it is created.

  • Cannot add null value, it will throw NullPointerException.
  • Cannot add more values to the list, it will throw UnsupportedOperationException.
  • Can add values only at the time of initialization.
  • Cannot add duplicate values like Set.of(1,1,2), it will throw IllegalArgumentException.
  • Cannot add null value, it will throw NullPointerException.
  • Cannot add more values to the set, it will throw UnsupportedOperationException.
  • Can add values only at the time of initialization.

we can create immutable map in 2 ways:


  • Cannot add more values to the map, it will throw UnsupportedOperationException.

Cannot use it as a user defined variable name. _ will be treated as any other keyword.

1) ofNullable()

To wrap a value which may be null, it prevents from null pointer exception.

2) takeWhile()

Takes a predicate and decides till when to process the data.

In this example it will create a stream of only 1 element because only 1 element satisfies the takewhile condition. Even if we change the values to (1,2,3,4,1) it will still create a stream of 1 element because once the predicate fails it stops its processing, it will not compute the further elements.

3) dropWhile()

Takes a predicate and decides from which element to start processing the data.

It will create a stream of 4 elements (2,3,4,5) as they satisfies the dropwhile() condition. Now, if we change the values to (1,2,3,4,5,1) then it will create a stream of 5 elements because once the predicate condition passes it will take all the remaining elements without computing it with predicate present in dropwhile() method.

4) iterate()

Till Java-8 iterate method comes with seed and operator only. From Java-9 iterate comes with predicate.

till Java 8

from Java 9

1) or()

It is used to return an Optional default value. Till Java-8 we had orElse() and orElseGet() methods, these methods returns the object value but if we need the result in an optional format, we did not had any method for that, so in Java-9 then introduced or().

If the value of ‘‘val’’ is null then it will return 3 as an optional value.

2) ifPresentOrElse()

It takes 2 arguments, 1st is a Consumer and second is a Runnable. Consumer gets executed if the given optional value computes to a not null value otherwise Runnable method will get executed.

Here if the optional value is not null then it will print the value otherwise, it will execute the error() method.

3) stream()

Now we can create a stream on a optional value like:

It will first check if value of val variable is null or not, if it is not null then it will creates a stream on that, applies the upper case method and collects the response in a list.

From Java-9 we can have private methods in interface. It is used to remove the duplicate code from the default methods present in the interface.

Try with resource was introduced in Java-7 but we have to declare the object in try statement itself, we could not use the objects which are defined outside the try statement.

Let say we have a class name Person which implements Closable interface and we want to use it in try with resource. So, till Java-8 we have to use it like:

we have to declare the class object in the try statement.

So in Java-9, we can use objects which are defined outside try statement. Object must implement either Autocloseable or closeable interface and objects must be final of effective final.

Here we can see we defined the person object outside the try statement.

We can use multiple objects in try block using ‘;’ as delimiter like:

Java 10 Features

Till Java-9 we have to mention the type of the local variable explicitly and ensure it was compatible with the initializer used to initialize it:

now from Java-10 we don't have to specify the data type of variable, compiler itself infers the type of message from the type of the initializer present on the right-hand side. we can just define it with var keyword

Compiler will understand that it is a integer using the value on the right side. It can be used only for the local variables with initializer, it cannot be used as a member variable, parameter and return value.

In Java-9 we get List.of() method which creates a unmodifiable list from the elements but if we want to make a list from other collection we did not had any method. So, in Java-10 we got this copyOf() method which takes a collection and makes a unmodifiable list from it.

If we try to alter this unmodifiable list then we will get ‘java.lang.UnsupportedOperationException’. In this example we used list as a argument, here we can use any collection.

It is similar to the List.copyOf() method and used for achieving the same goal.

Map also has the same method in Java-10.

In Java-10 there is a new method introduced in Collectors to make collection unmodifiable, any try to make a change in this unmodifiable collection will result in UnsupportedOperationException.

Similarly we have toUnmodifiableMap() and toUnmodifiableSet() to create read-only map and set respectively.

All types of optional (OptionalInt, OptionalDouble, OptionalLong and Optional) got a new method in Java-10 which throws NoSuchElementException when no element is present in optional.

JAVA 11 Features

Till Java-10 we first need to compile the code with javac command then we can run it with java command but from Java-11 we can directly run our java class using a java command without compiling it with javac command.

Java-11 added new string methods:

1) isBlank()

Checks if the current string value is blank or not, empty string or the string with white spaces only will be considered as blank string. This method will throw NullPointerException in case the string value is null.

2) lines()

This method returns a stream of strings, which is a collection of all sub-strings split by lines.

3) strip(), stripLeading(), stripTrailing()

These methods are used to remove the white spaces, leadingwhite spaces and trailing white spaces from a string. It is like a advanced version of trim, strip is unicode aware. Unicode was not there at the time of trim.

4) repeat()

It repeats the given string that many number of times as mentioned in the given parameter value.

In Java-10 we saw the usage of var keyword for declaring local variables, now in Java-11 we can use it in the lambda expressions.

Second expression is invalid because we need to have parenthesis if we are using var in a lambda expression and third expression is invalid because mixing of any other data type is not allowed with var.

Java 12 Features

Java-12 introduced lambda expression in switch statements. Till Java-11 if we want to use the switch statement then we use to use it like:

Now we can write the same switch expression with the lambda expressions like:

Java-12 introduced a new method to compare 2 files present at different locations, it returns the location of the first mismatch byte between the 2 files or -1L if both the files are identical.

It compares the bytes of the files and returns the first byte which does not match, if all bytes matches then it returns -1L.

This new collectors method takes 2 collector and 1 BiFunction

So the result of first 2 collectors becomes the arguments of the Bifunction, lets understand with an example:

If we have a list of numbers and we want to calculate if the odd numbers are more or even numbers, how can we achieve this with teeing (I know there are much easier methods to solve this issue, here we are using teeing just for understanding this method).

So we can calculate the number of odd numbers in the 1st collector of the teeing method, even numbers in the 2nd collector and can compute which one is more in function.

Java-12 introduced few new string methods

1) indent()

This method is used to add or remove white spaces in a multi line string.

Similarly if we have white space in a multi-line string we can remove it using negative value in the same method.

2) transform()

As the name suggests this method is used to transform a string value using java.util.Function

we can apply any function and transform a string in any format.

3) describeConstable()

It is used to get the optional value of any string


Java-13 introduced a new text block preview feature, preview feature means it will not work by default, you have to enable it. It allows us to create multi-line string easily.

Java-12 introduced switch expression, Java-13 has the same but it replaced break with yield keyword. ‘yield’ is used to return a value from a switch case.

1) formatted(Object… args):
It is similar to the String format() method. It’s added to support formatting of the text blocks.

2) stripIndent():
It is used to remove the incidental white space characters from the beginning and end of every line in the text block. This method is used by the text blocks and it preserves the relative indentation of the content.

3) translateEscapes():
It returns a string whose value is this string, with escape sequences translated as if in a string literal.

Java-14 features

Java-12 introduced the switch expression as a preview feature, Java-13 added some extra functionality in it (yield to return value) but kept it in preview feature list. Now Java-14 officially added it in the feature list.

In java we use pattern matching using ‘instanceof’ keyword, we usually use it for type casting some value.

As we can see in this example first we are checking if the value of ‘v’ is an instance of String, if it is an instance of string then we are assigning a variable to its value. Now from Java-14 we can combine these 2 steps like

The biggest enemy of any developer is a NullPointerException, sometimes it becomes difficult to check which part of the code is causing the NullPointerException like

If we get a NullPointerException here, its hard to tell from logs exactly because of which value it is causing NullPointerException. Now from Java-14 we get very specific logs for an exception

Here we can see where the exact problem is.

JDK 14 introduces records, which are a new kind of type declaration. Like an enum, a record is a restricted form of a class. It’s ideal for “plain data carriers,” classes that contain data not meant to be altered and only the most fundamental methods such as constructors and accessors.

The Java compiler will generate a constructor, private final fields, accessors, equals/hashCode and toString methods automatically.

for more information about record, please refer :



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store