top of page
Writer's picturemargi patel

Java 11 Features

In this tutorial, we learn about the new features introduced in Java 11.

To run a Java file, we first compile it using the javac command. But with Java 11, we can directly run a Java file by using single java command.

javac MyFirstJavaProgram.java

java MyFirstJavaProgram


We no longer need to use the above process. Instead, we can use the following command and it will give the same results

java MyFirstJavaProgram.java


Java 11 introduced a few new methods to the String class. The following list explains these new methods.

isBlank() - this method is used to check whether a string is blank or not. Empty strings and strings with just whitespace are considered blank.


public class Demo

{

public static void main(String[] args)

{

String s1 = "";

String s2 = " ";

String s3 = "String";

System.out.println("s1 is blank: " + s1.isBlank());

System.out.println("s2 is blank: " + s2.isBlank());

System.out.print("s3 is blank: " + s3.isBlank());

}

}


output:

s1 is blank: true

s2 is blank: true

s3 is blank: false


lines() - this method splits a string using line terminators and returns a stream.


import java.util.List;

import java.util.stream.Collectors;


public class Demo

{

public static void main(String[] args)

{

String s = "This\n is\n a\n String";

List<String> listOfLines = s.lines().collect(Collectors.toList());

System.out.print(listOfLines);

}

}

output:

[This, is, a, String]


repeat() - this method is used to duplicate or repeat a string

public class Demo

{

public static void main(String[] args)

{

String s = "String";

System.out.println("String: " + s);

System.out.println("String repeated twice: " + s.repeat(2));

System.out.print("String repeated five times: " + s.repeat(5));

}

}

output:

String: String

String repeated twice: StringString

String repeated five times: StringStringStringStringString


strip(), stripLeading(), stripTrailing() - These methods are used to remove whitespace from the strings. They are very similar to the existing trim() method, but provide Unicode support.


public class Demo

{

public static void main(String[] args)

{

String s = " string ";

System.out.println("$" + s + "$");

System.out.println("$" + s.strip() + "$");

System.out.println("$" + s.stripLeading() + "$");

System.out.println("$" + s.stripTrailing() + "$");

}

}

Output:

$ string $

$string$

$string $

$ string$


Previous Java versions allowed access of private members to nested classes(nestmates), but we cannot use them with the Reflection API. Java 11 no longer uses bridge methods and provides the getNestHost(), getNestMembers(), and isNestmatOf() methods for the Reflection API.


public class Demo {

private void privateMethod() {

System.out.print("Private Method");

}

class NestedClass {

public void callPrivateMethod() {

privateMethod();

}

}

public static void main(String[] args) {

System.out.println(Demo.class.isNestmateOf(Demo.NestedClass.class)); //Demo class is nestmate of NestedClass

System.out.println(Demo.NestedClass.class.isNestmateOf(Demo.class)); //NestedClass is nestmate of Demo class

System.out.println(Demo.NestedClass.class.getNestHost()); //Nest host of NestedClass

System.out.println(Demo.class.getNestMembers()); //Nest host of Demo class

}

}

output:

true

true

class Demo

[Ljava.lang.Class;@36baf30c


Java 11 makes it a lot easier to read and write strings. The readString() and writeString() static methods are added to the Files class for this purpose.


import java.io.IOException;

import java.nio.file.Files;

import java.nio.file.Path;


public class Demo

{

public static void main(String[] args) throws IOException

{

Path path = Files.createTempFile("temporaryFile", ".txt");

//Writing to the file

Files.writeString(path, "Hello World");

//Reading from the file

String s = Files.readString(path);

System.out.print(s);

}

}


output:

Hello World


Collection to an Array

The new default toArray() method is used to easily convert a collection to an array of the correct type.


import java.util.ArrayList;

import java.util.Arrays;


public class Demo

{

public static void main(String[] args) {

ArrayList<Integer> list = new ArrayList<>();

list.add(5);

list.add(10);

list.add(15);

Integer[] intArr = list.toArray(Integer[]::new);

System.out.print(Arrays.toString(intArr));

}

}

output:

[5, 10, 15]


The HTTP Client API was first introduced in Java 9 and was updated in Java 10. It is offered as a standard feature in Java 11 version. The new API has better performance and is compatible with both HTTP/1.1 and HTTP/2. The API also provides support for WebSockets.


Local-Variable Syntax for Lambda

Java 11 adds the support for Local-Variable syntax for lambda expressions. Lambdas can infer the type, but using the var keyword allows us to use annotations like @NotNull or @Nullable with the parameters.


(@NotNull var str) -> "$" + str


Dynamic Class-File Constants

In Java 11, the Java Class-File format supports a new constant pool form called the CONSTANT_Dynamic. This will delegate creation to a bootstrap method. This was introduced to reduce the cost of creating new forms of materializable class-file constants by creating a single new constant-pool form that will be parameterized with appropriate user-defined behavior. This feature greatly enhances the performance.


Removed JMC and JavaFX

JDK will no longer include JDK Mission Control(JMC) and JavaFX modules. They are available for download separately.

Deprecated Modules

The Nashorn JavaScript engine and JJS were deprecated in Java 11. Future versions will probably remove them completely.

The pack200 and unpack200 tools and the Pack200 compression scheme API of java.util.jar are also deprecated.


Summary

Java 11 introduced a lot of new changes and features. A few existing features were deprecated or removed completely.

19 views0 comments

Recent Posts

See All

Comments


bottom of page