NashTech Blog

Java 11: New Features and Enhancements with Examples

Table of Contents
artificial intelligence, brain, think-3382507.jpg

 

Exploring Java 11: New Features and Enhancements with Examples

Java 11, released in September 2018, brought several new features and enhancements to the Java programming language. This release is significant because it’s a Long-Term Support (LTS) version, meaning it will receive extended support and updates from Oracle. Let’s dive into some of the notable features of Java 11, complete with examples.

1. Local-Variable Syntax for Lambda Parameters

Java 11 allows the use of var in lambda expressions, making the code more concise and readable.

Example:

import java.util.List;

public class LambdaExample {
public static void main(String[] args) {
List<String> list = List.of("a", "b", "c");
list.forEach((var item) -> System.out.println(item));
}
}

2. New String Methods

Java 11 introduced several new methods for the String class, making string manipulation more convenient.

  • isBlank(): Checks if a string is empty or contains only whitespace.
  • lines(): Converts a string into a stream of lines.
  • strip(): Removes leading and trailing whitespace.
  • repeat(int times): Repeats the string a specified number of times.

Example:

public class StringMethodsExample {
public static void main(String[] args) {
String str = " Hello, Java 11! ";

// isBlank
System.out.println(str.isBlank()); // false

// lines
String multilineStr = "Hello\nWorld\nJava 11";
multilineStr.lines().forEach(System.out::println);

// strip
System.out.println(str.strip()); // "Hello, Java 11!"

// repeat
System.out.println("Java ".repeat(3)); // "Java Java Java "
}
}

3. The Files.writeString and Files.readString Methods

Java 11 added new methods to the Files class for reading and writing strings to files, simplifying file I/O operations.

Example:

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;

public class FilesExample {
public static void main(String[] args) {
Path path = Paths.get("sample.txt");

try {
// Write to a file
Files.writeString(path, "Hello, Java 11!");

// Read from a file
String content = Files.readString(path);
System.out.println(content); // "Hello, Java 11!"
} catch (IOException e) {
e.printStackTrace();
}}}

4. The HttpClient API

Java 11 standardizes the HttpClient API introduced in Java 9. It supports both synchronous and asynchronous HTTP requests.

Example:

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.io.IOException;

public class HttpClientExample {
public static void main(String[] args) {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://jsonplaceholder.typicode.com/posts/1"))
.build();

try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}}}

5. Nest-Based Access Control

Java 11 improves access control for nested classes, allowing them to access each other’s private members.

Example:

public class NestBasedAccessControlExample {
private String message = "Hello from the outer class!";

class NestedClass {
void printMessage() {
System.out.println(message);
}
}

public static void main(String[] args) {
NestBasedAccessControlExample outer = new NestBasedAccessControlExample();
NestBasedAccessControlExample.NestedClass nested = outer.new NestedClass();
nested.printMessage();
}}

6. Dynamic Class-File Constants (JEP 309)

This feature introduces a new constant pool form, making the JVM more efficient when handling dynamically-computed constants.

Example: This feature is more JVM-centric and doesn’t have a straightforward code example but contributes to performance improvements under the hood.

Conclusion

Java 11 brought a host of new features and improvements, making the language more modern and efficient. Whether it’s enhancing syntax with var in lambda expressions, simplifying file I/O, or introducing new methods to the String class, Java 11 continues to evolve to meet the needs of developers. As an LTS release, it’s a solid choice for projects that require long-term stability and support. Embracing these new features can significantly boost productivity and code quality.

Picture of Akshit Kumar

Akshit Kumar

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

Scroll to Top