Java 11 features

Java 11 features

1. New HTTP Client API (Standardized)
Replaces `HttpURLConnection` with a modern, fluent API.
```java
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("https://api.github.com"))
    .build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
```

2. String API Enhancements
```java
String str = "  Hello Java 11  ";
System.out.println(str.isBlank());              // false
System.out.println(str.strip());                // "Hello Java 11"
System.out.println(str.stripLeading());         // "Hello Java 11  "
System.out.println(str.stripTrailing());        // "  Hello Java 11"
System.out.println("Java".repeat(3));           // "JavaJavaJava"
System.out.println("Line1\nLine2\nLine3".lines().count()); // 3
```

3. Local Variable Syntax for Lambda Parameters
```java
BiFunction<Integer, Integer, Integer> add = (var x, var y) -> x + y;
System.out.println(add.apply(5, 3)); // 8
```

4. Single-File Source-Code Execution
Run `.java` files directly without compiling:
```bash
java HelloWorld.java
```
Where `HelloWorld.java` contains:
```java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello from Java 11!");
    }
}
```

5. Nest-Based Access Control
Improves access between nested classes and their enclosing classes. No visible syntax change, but better reflection and JVM handling.

6. Epsilon Garbage Collector (JEP 318)
A no-op GC for performance testing:
```bash
java -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC -Xmx1G MyApp
```

7. Removed Features
- JavaFX and WebStart removed from JDK.
- Deprecated APIs cleaned up.
- No more `pack200` tools.

> Would you like a mini-project using Java 11’s HTTP client and string utilities—maybe a GitHub repo analyzer or a weather fetcher? 

Comments

Popular posts from this blog

Java 25 Features

Java Version Features

Java 8 Programs