Java 11 Features: What Every Developer Should Know
Java 11, released in September 2018, marked the second Long-Term Support (LTS) version after Java 8. It brought several key improvements and changes that every Java developer should be aware of. Whether you're upgrading from Java 8 or just curious, here’s a rundown of the most impactful features.
1. var in Lambda Parameters
Java 11 allows the use of var in lambda expressions:
(list) -> list.size() // Old (var list) -> list.size() // Java 11
This is particularly useful when annotations are required:
(@Nonnull var item) -> System.out.println(item);
2. HTTP Client (Standard)
Java 11 standardized the HTTP Client API introduced in Java 9. It supports HTTP/1.1 and HTTP/2 and replaces the older HttpURLConnection.
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.example.com/data"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
3. Removed Deprecated APIs and Features
Several older modules were removed:
- Java EE modules (like
java.xml.ws,java.activation) java.se.eeaggregate moduleJAXB,JAX-WS,CORBA, and others
If your code depends on them, you'll need to add external libraries.
4. String API Enhancements
Java 11 adds several useful methods to the String class:
" ".isBlank(); // true " abc ".strip(); // "abc" "abc\n".stripTrailing();// "abc" "line1\nline2".lines(); // Stream of lines
5. File Reading Made Simple
A new method Files.readString(Path) simplifies file reading:
Path path = Path.of("file.txt");
String content = Files.readString(path);
6. Local-Variable Syntax for Lambda Parameters
This allows a consistent style for type declarations and helps in adding annotations:
(var x, var y) -> x + y
Previously, you couldn't mix var and explicit types in lambdas.
7. Launch Single-File Java Source Code
You can now run a single .java file directly from the command line without compiling:
java HelloWorld.java
This makes Java much more beginner-friendly and scripting-like.
8. ZGC (Z Garbage Collector)
Introduced as an experimental feature, ZGC is a scalable, low-latency garbage collector ideal for applications requiring large heap sizes and short pause times.
Enable it using:
-XX:+UseZGC
🔚 Final Thoughts
Java 11 is a powerful, stable upgrade from Java 8, bringing modern features, better performance, and a more streamlined API. With long-term support, it’s a great choice for both enterprise and personal projects.
Are you still on Java 8? Now's the time to upgrade.
Comments
Post a Comment