Java 11: LTS Release with Lambda in var and HTTP Client

Java 11: Long-Term Support with Modern APIs
Java 11 (September 2018) is a Long-Term Support (LTS) release with support until September 2026. It introduced a modern HTTP client, enhanced string methods, and improved developer experience.
1. New HTTP Client API
The new HTTP/2 capable client replaces the legacy URLConnection.
Before Java 11:
// Using deprecated URLConnection
URL url = new URL("https://api.example.com/users");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream())
);
String line;
StringBuilder response = new StringBuilder();
while ((line = br.readLine()) != null) {
response.append(line);
}After Java 11:
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
// Simple GET request
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.example.com/users"))
.build();
HttpResponse<String> response = client.send(
request,
HttpResponse.BodyHandlers.ofString()
);
System.out.println("Status: " + response.statusCode());
System.out.println("Body: " + response.body());POST Request Example:
HttpRequest postRequest = HttpRequest.newBuilder()
.uri(URI.create("https://api.example.com/users"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(
"{\"name\":\"John\",\"email\":\"john@example.com\"}"
))
.build();
HttpResponse<String> postResponse = client.send(
postRequest,
HttpResponse.BodyHandlers.ofString()
);
JsonObject result = JsonParser.parseString(postResponse.body())
.getAsJsonObject();
System.out.println("Created user: " + result.get("id"));Async HTTP Requests:
// Non-blocking async requests
HttpRequest asyncRequest = HttpRequest.newBuilder()
.uri(URI.create("https://api.example.com/data"))
.build();
client.sendAsync(asyncRequest, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenApply(JsonParser::parseString)
.thenAccept(System.out::println)
.join();2. Enhanced String Methods
New utility methods for common string operations.
// isBlank(): Check if string is null, empty, or whitespace-only
" ".isBlank(); // true
"".isBlank(); // true
" Hello ".isBlank(); // false
// lines(): Split by line breaks into Stream
String multiline = "Line 1\nLine 2\nLine 3";
multiline.lines()
.filter(line -> !line.isEmpty())
.forEach(System.out::println);
// strip(): Remove leading/trailing whitespace
" Hello World ".strip(); // "Hello World"
" Hello World ".stripLeading(); // "Hello World "
" Hello World ".stripTrailing(); // " Hello World"
// repeat(): Repeat string n times
"ab".repeat(3); // "ababab"
"*".repeat(10); // "**********"
// indent(): Add indentation for multi-line strings
String code = "for (int i = 0; i < 10; i++) {\n count++;\n}";
String indented = code.indent(4);
// Adds 4 spaces to each line3. Local Variable Syntax for Lambda Parameters
Enhancement to Java 10's var keyword for lambda expressions.
// Before Java 11
stream.map((String str) -> str.toUpperCase());
stream.forEach((String name) -> System.out.println(name));
// After Java 11: var in lambda parameters
stream.map((var str) -> str.toUpperCase());
stream.forEach((var name) -> System.out.println(name));
// With annotations (powerful combination)
// Requires modern compiler
stream.filter((var user) -> user.isActive());
// Multiple parameters
BiFunction<Integer, Integer, Integer> add =
(var a, var b) -> a + b;4. New File Methods
Enhanced file I/O operations.
import java.nio.file.Files;
import java.nio.file.Path;
Path filePath = Path.of("example.txt");
// readString(): Read entire file as String
String content = Files.readString(filePath);
// writeString(): Write string to file
Files.writeString(filePath, "Hello, World!");
// Simpler than old approach
// Old: Files.write(filePath, "content".getBytes())
// Chain with streams
String processed = Files.readString(filePath)
.lines()
.map(String::toUpperCase)
.collect(Collectors.joining("\n"));5. Nest-based Access Control
Better encapsulation for nested classes.
public class OuterClass {
private String secret = "Private Data";
class InnerClass {
void accessSecret() {
// Direct access to outer class private members
System.out.println(secret);
}
}
static class StaticNestedClass {
// Also works with static nested classes
}
}
// Before Java 11: Synthetic methods were generated
// After Java 11: Direct access with nest-based control6. Removal of Java EE Modules
Several modules were removed in preparation for future changes.
// REMOVED modules:
// java.corba - CORBA support
// java.xml.bind - JAXB
// java.activation - Activation Framework
// javax.xml.ws - Web Services
// These should be added as external dependencies now
// <dependency>
// <groupId>jakarta.xml.bind</groupId>
// <artifactId>jakarta.xml.bind-api</artifactId>
// </dependency>Developer Impact
Positive:
- Modern HTTP Client: No need for external libraries like OkHttp
- String Operations: Simplified common string manipulations
- Long-term Support: Stable platform for enterprise projects
- Performance: Significant improvements in GC and startup time
Challenges:
- LTS Lock-in: Risk of staying on older version too long
- External Dependencies: Removed Java EE modules must be added separately
- Module System: Full module migration optional but recommended
Pros and Cons
Pros ✅
- LTS Stability: 8 years of support (Sept 2018 - Sept 2026)
- Modern HTTP Client: Built-in async HTTP support without external libs
- Enhanced Strings: Cleaner string operations
- Better Performance: Significant GC and startup improvements
- Lambda Enhancement: var in parameter lists
- File I/O: Simpler file operations
- Flight Recorder: Built-in JVM profiling (previously commercial)
Cons ❌
- Removed Features: Java EE modules require external dependencies
- Module Complexity: Still learning curve for full module adoption
- Learning Curve: Multiple new features to master
- Backward Compatibility: Some legacy code patterns no longer work
Use Cases
Perfect for:
- Enterprise applications needing stable, long-term support
- Microservices with REST APIs (HTTP client)
- Large teams where stability > cutting-edge features
Code Example:
// Typical Java 11 use case - REST API client
public class UserServiceClient {
private static final HttpClient client = HttpClient.newHttpClient();
public String getUser(String id) throws IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.example.com/users/" + id))
.GET()
.build();
return client.send(request, HttpResponse.BodyHandlers.ofString())
.body();
}
}Conclusion
Java 11 is a milestone LTS release that modernizes Java's standard library with practical APIs like the HTTP client and enhanced string methods. It provides a stable platform for enterprise applications while introducing quality-of-life improvements. Many organizations remain on Java 11 due to its stability and long support window.
Recommendation:
- Adopt Java 11 for new projects requiring LTS support
- Use the HTTP client for modern REST API integration
- Leverage enhanced string methods for cleaner code