Java 11 Features: Embrace the Power of the Latest Java Release

Naveen Kumar Ravi
2 min readJul 24, 2023

--

Introduction:

Java, one of the most popular programming languages, has evolved significantly over the years. With each new version, it brings exciting features and enhancements, making developers’ lives more comfortable and code more efficient. Java 11, released in September 2018, is no exception. In this post, we’ll explore some of the most intriguing features of Java 11, along with practical code examples and references, to help you stay up-to-date with the latest Java release.

Local-Variable Syntax for Lambda Parameters:

One of the most anticipated features in Java 11 is the enhancement to the lambda expressions. Now, you can use the “var” keyword to define lambda parameters implicitly. This reduces boilerplate code and makes your code more concise and readable.

Example:

// Before Java 11
list.forEach((String s) -> System.out.println(s));

// Java 11
list.forEach((var s) -> System.out.println(s));

String Methods:

Java 11 introduces several useful methods to the String class that enhance String manipulation capabilities.

a. isBlank(): The isBlank() method checks whether a string is empty or contains only white spaces.

String str = "   ";
boolean result = str.isBlank(); // true

b. lines(): The lines() method returns a stream of lines extracted from the given string.

String text = "Line 1\nLine 2\nLine 3";
text.lines().forEach(System.out::println);
// Output:
// Line 1
// Line 2
// Line 3

HTTP Client (Standard):

Java 11 introduces a new and more efficient HTTP client API to replace the deprecated HttpURLConnection. The new HTTP client is asynchronous, supports HTTP/2, and has a more straightforward API.

Example:

// Create an HttpClient
HttpClient httpClient = HttpClient.newHttpClient();

// Create a GET request
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.example.com/data"))
.build();

// Send the request and asynchronously process the response
httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();

Local-Variable Syntax for Try-With-Resources:

Java 11 enhances the try-with-resources block by allowing the “var” keyword in resource declarations. This makes the code more concise and eliminates the need to repeat the resource type.

Example:

// Before Java 11
try (BufferedReader reader = new BufferedReader(new FileReader("data.txt"))) {
// ...
}

// Java 11
try (var reader = new BufferedReader(new FileReader("data.txt"))) {
// ...
}

Epsilon Garbage Collector:

Java 11 introduces the Epsilon garbage collector, a “no-op” garbage collector. It’s designed for use cases where memory allocation is not a concern, and you want to skip the overhead of garbage collection.

Example:

java -XX:+UseEpsilonGC MyApp

Conclusion:

Java 11 brings several exciting features and improvements that can enhance your development experience and make your code more efficient. From improved lambda expressions to a new HTTP client and more, Java 11 is undoubtedly worth exploring. I hope this post has provided you with valuable insights into the latest Java release.

If you enjoyed this post and would like to stay updated with more Java-related content, don’t forget to follow me on Medium. Happy coding with Java 11!

References:

  1. Java Platform, Standard Edition 11 Documentation — https://docs.oracle.com/en/java/javase/11/
  2. Java 11 Features You Should Know About — https://www.baeldung.com/java-11-new-features
  3. What’s New in Java 11 — https://www.javatpoint.com/java-11

--

--

Naveen Kumar Ravi
Naveen Kumar Ravi

Written by Naveen Kumar Ravi

Technical Architect | Java Full stack Developer with 9+ years of hands-on experience designing, developing, and implementing applications.

No responses yet