Several new features have been added to Java 11 to assist developers in avoiding boilerplate code and improving readability. Java 11 is the first Long Term Support (LTS) version following Java 8. Oracle discontinued supporting Java 8 in January 2019. Oracle, as we all know, releases a new Java version every six months.
Java is now the most widely used technology for application development. A web application or a desktop application can be used. However, the application owner must upgrade the application in order to maintain it up to date with the newest Java version.
Oracle no longer offers a free LTS for Java 11. The last free Oracle JDK release was Java 10. You can use OpenJDK and upgrade it whenever you want for free. If you want to use it on a enterprise scale, you’ll have to pay for support.
In this article we will know what are the important new features added in Java 11 or JDK 11?
Table of Contents
Running Java File with single command (Compile Free Run)
Earlier versions of Java required you to compile the source code with the javac command before running it with the java command.
However, with Java 11, you do not need to use the javac tool to compile the code. The capability to launch a file directly using java tool is included in Java 11.
If you don’t compile the code, Java 11 will explicitly compile it.
package com.geekcer;
public class CompileFreeLaunch {
public static void main(String[] args) {
System.out.println("Welcome to Java 11 Features!");
}
}
>> java CompileFreeLaunch.java Welcome to Java 11 Features!
Local-Variable Syntax for Lambda (Var in Lambda)
The var keyword is available in Java 11 and can be used with lambda arguments. To put it another way, we may use the var keyword in a lambda local variable.
package com.geekcer;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class LambdaVarKeyword {
public static void main(String[] args) {
List list = Arrays.asList("Java", "C#", "C++", "HTML");
String str = list.stream().map((var t) -> t).collect(Collectors.joining(", "));
System.out.println(str);
}
}
Output: Java, C#, C++, HTML
New methods in String class
isBlank() in Java
The isBlank() method examines a string to see if it is empty or contains white space. This method returns a boolean value. The isBlank() method returns true if the string contains empty or white space otherwise returns false.
package com.geekcer;
public class JavaIsBlankDemo {
public static void main(String[] args) throws Exception {
String data = "";
System.out.println(data.isBlank()); // Returns true
String input = " ";
System.out.println(input.isBlank()); // Returns true
String value = "Java 11";
System.out.println(value.isBlank()); // Returns false
}
}
Output: true true false
lines() in Java
The lines() method returns a collection of all line-splitting substrings. A stream of strings is what a collection is. When you have a multiline string, the lines() method is appropriate.
package com.geekcer;
import java.util.stream.Collectors;
public class JavaLinesDemo {
public static void main(String[] args) {
String multiLineString = "Program line1\nProgram line2\nProgram line3\nProgram line4";
System.out.println(multiLineString);
System.out.println(multiLineString.lines().collect(Collectors.toList()));
}
}
Output: Program line1 Program line2 Program line3 Program line4 [Program line1, Program line2, Program line3, Program line4]
strip() in java
In Java, the strip() method eliminates white space from the start and end of a string. In Java, the stripLeading() method eliminates white space from the start of a string. The stripTrailing() method eliminates white space from a string’s end.
package com.geekcer;
public class JavaStripDemo {
public static void main(String[] args) {
String inputString = " Java for Geek ";
System.out.print("Before");
System.out.print(inputString.strip());
System.out.println("After");
System.out.print("Before");
System.out.print(inputString.stripLeading());
System.out.println("After");
System.out.print("Before");
System.out.print(inputString.stripTrailing());
System.out.println("After");
}
}
Output: BeforeJava for GeekAfter BeforeJava for Geek After Before Java for GeekAfter
repeat(int) in java
String.repeat(int) can be used to repeatedly concatenate strings. The repeat method repeats the string for the number of time specified.
package com.geekcer;
public class JavaRepeatDemo {
public static void main(String[] args) throws Exception {
String inputSting = "Java 11 for Geek | ".repeat(4);
System.out.println(inputSting);
}
}
Output: Java 11 for Geek | Java 11 for Geek | Java 11 for Geek | Java 11 for Geek |
File readString() and writeString() methods in Java
The ReadString() and WriteString() methods in Java 11 introduce a new concept for reading and writing files. These techniques save a lot of boilerplate code while also improves readability.
package com.geekcer;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
public class Java11FilesMethods {
public static void main(String[] args) {
try {
Path filePath = Files.writeString(Path.of(File.createTempFile("file", ".txt").toURI()), "Java 11 Features",
Charset.defaultCharset(), StandardOpenOption.WRITE);
String fileData = Files.readString(filePath);
System.out.println(fileData);
} catch (IOException exception) {
exception.printStackTrace();
}
}
}
Output: Java 11 Features
Convert Collections to Array in Java 11
As we all know, there are several techniques to convert a collection to an array. However, with Java 11, there is a new mechanism to convert a Collection to an Array.
We’ll learn how to convert a Collection to an Array in Java 11 by using an example.
package com.geekcer;
import java.util.Arrays;
import java.util.List;
public class CollectionsToArrayInJava {
public static void main(String[] args) {
List namesList = Arrays.asList("Java", "C#", "C++");
String[] names = namesList.toArray(String[]::new);
System.out.println(Arrays.toString(names));
}
}
Output: [Java, C#, C++]
Predicate.not() Method in Java
We can use the not() in Java 11 to negate an existing predicate. This works in the same way as the negate method.
package com.geekcer;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class PredicateNegateDemo {
public static void main(String[] args) {
List countryList = Arrays.asList("INDIA", "UK", "SA", "USA");
List countries = countryList.stream().filter(Predicate.not(String::isBlank))
.collect(Collectors.toList());
countries.forEach(country -> System.out.print(country + " "));
}
}
Output: INDIA UK SA USA
Java 11 Removed JMC and JavaFX features(module)
JMC (JDK Mission Control) and JavaFX modules are no longer accessible in Java 11. To use these modules, you must first download them.
Epsilon garbage collector as Java 11 new Features
Epsilon (no-operations(No-Op) garbage collector) is a new experimental feature in Java 11. It does not do collect garbage but does allocate memory. Out-of-memory faults are handled using a no-op garbage collector. The JVM will shut down when the Java heap is full.
To enable the Epsilon GC, type the following command.
-XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC
Some of its uses are listed below.
- Performance evaluations
- Pressure testing of memory
- Extremely short-lived tasks and VM interface testing
- Improvements in last-drop delay and throughput