Java 11, which was released in September 2018, brought several significant features and improvements to the Java platform. It enhanced language features, introduced new libraries, and provided tools for improved application performance and diagnostics. Below are some key features of Java 11:
๐น 1. Local-Variable Syntax for Lambda Parameters
- Java 11 allows the use of
var
in lambda expressions, improving readability and consistency in your code. - This feature enables you to use
var
for parameters in lambda expressions, allowing you to omit the explicit type declaration while keeping the code compact.
Example:
java
(var x, var y) -> x + y
๐น 2. HTTP Client (Standard)
- The HttpClient class, which was introduced in Java 9 as an incubator module, became a standard feature in Java 11.
- It provides a more modern, flexible, and asynchronous API for making HTTP requests.
Example:
java
HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://example.com"))
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
๐น 3. Nest-Based Access Control
- Java 11 introduces nest-based access control, which allows classes that are logically part of the same code entity (such as inner classes) to access each otherโs private members without needing the compiler to insert accessibility-broadening bridge methods.
- This improves performance and reduces unnecessary methods.
๐น 4. Dynamic Class-File Constants
- Java 11 introduced a new constant type,
CONSTANT_Dynamic
, enabling constants to be created dynamically at runtime. - This allows more flexibility in generating constants based on runtime conditions, offering performance improvements in certain scenarios.
๐น 5. Epsilon: A No-Op Garbage Collector
- Java 11 introduced the Epsilon garbage collector, which is a no-op (no operation) garbage collector.
- Epsilon is useful in scenarios where garbage collection is not required, such as performance testing, short-lived jobs, or applications where memory management is not a concern.
Example:
bash
-XX:+UseEpsilonGC
๐น 6. Java Flight Recorder (JEP 328)
- Java Flight Recorder (JFR), previously a commercial feature, was made available in OpenJDK starting with Java 11.
- JFR provides a low-overhead data collection framework for profiling and troubleshooting Java applications, making it easier to gather detailed runtime diagnostics.
๐น 7. Enhanced Deprecation
- The
@Deprecated
annotation was enhanced in Java 11, providing more detailed information about deprecated methods. - You can specify the version in which the feature was deprecated and indicate whether it will be removed in future releases.
Example:
java
@Deprecated(since = "11", forRemoval = true)
๐น 8. New File Methods
- Several new methods were added to the
java.nio.file.Files
class to simplify file handling in Java 11: readString()
: Reads the contents of a file as a string.writeString()
: Writes a string to a file.isSameFile()
: Checks if two paths point to the same file.mismatch()
: Compares two files and returns the first byte position where they differ.
Example:
java
String content = Files.readString(Path.of("example.txt"));
System.out.println(content);
๐น 9. Improvements in String Handling
Java 11 introduced several useful methods to the String
class, enhancing string manipulation capabilities:
isBlank()
: Checks if a string is empty or contains only whitespace.lines()
: Splits a string into a stream of lines.strip()
: Removes leading and trailing whitespace.stripLeading()
: Removes leading whitespace.stripTrailing()
: Removes trailing whitespace.
Example:
java
String str = " Example String ";
System.out.println(str.strip()); // Output: "Example String"
๐ง Conclusion
Java 11 introduces a range of features that significantly enhance the language and platform. From modernizing HTTP handling with the new HttpClient, to simplifying string operations, and providing performance improvements through features like the Epsilon garbage collector, Java 11 offers improvements that benefit both developers and application performance. Keep in mind that Java 11 is also a Long-Term Support (LTS) version, which makes it a stable choice for production environments.