Java
11 features with examples can be a valuable resource for developers who want to
stay up-to-date with the latest improvements in the language.
Java 11 is a significant release in the Java programming language. It was
released in September 2018 as a long-term support (LTS) version, which means it
receives updates and security fixes for an extended period, making it a stable
choice for businesses and developers. Java 11 brings several important features
and enhancements that improve performance, security, and developer
productivity. Here below list of Java 11 features with examples.
List of Java 11 Features with Examples:
1. Local
Variable Syntax for Lambda Parameters (var):
Java 11 allows the use of the var
keyword for lambda parameters, making code more concise.
(x, y) -> x + y;
// With Java 11 (var x, var y) -> x + y;
|
2. HTTP Client
(standard):
Java 11 introduces a modern,
asynchronous HTTP client in the java.net.http package for making HTTP
requests.
import java.net.http.*; import java.net.*;
public class HttpClientExample {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder() .uri(new
URI("https://example.com")) .build();
HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
} } |
3. Nest-Based
Access Control:
Nestmates improve the efficiency of
inner classes' access to private members of their containing class.
class Outer {
private int x = 10;
class Inner {
void printX() { System.out.println(x); // Accessing private member of Outer
}
} } |
4. Dynamic
Class-File Constants:
This feature allows the creation of a
new constant pool entry that references a dynamically computed value.
class DynamicConstantsExample {
static final int MAGIC_NUMBER = 42;
static final int COMPUTED_VALUE = MAGIC_NUMBER * 2; // Dynamic class-file constant
public static void main(String[] args) {
System.out.println(COMPUTED_VALUE); // Prints 84
} } |
5. Improved var
Handling:
Java 11 enhances the var
keyword to support more use cases, such as in lambda expressions, method
parameters, and local variables.
var list = List.of(1, 2, 3); var square = (int x) -> x * x; |
6. Epsilon
Garbage Collector:
Epsilon is a no-op garbage collector
useful for performance testing and diagnosing memory allocation behavior.
# Enable Epsilon GC java -XX:+UseEpsilonGC MyApp.java |
7. New Methods
in String Class:
Java 11 added several useful methods
to the String class, including isBlank(), lines(), strip(),
stripLeading(), and stripTrailing().
String text = " Hello, World! "; boolean isBlank = text.isBlank(); // true String stripped = text.strip(); // "Hello, World!" |
These Java 11 features with examples make a powerful and developer-friendly version,
offering improved readability, performance, and support for modern web
development. Developers can leverage these features to write cleaner, more
efficient code.
0 Comments