A comprehensive guide to all Java 11 concepts with practical examples for interview preparation.
- HTTP Client (Standardized)
- Files Methods Enhancements
- String Methods Enhancements
- Optional.isEmpty()
- Predicate.not()
- Local-Variable Syntax for Lambda Parameters
- Nest-Based Access Control
- Java Flight Recorder (JFR)
- Epsilon Garbage Collector
- ZGC (Experimental)
- Removed Java EE and CORBA Modules
- Common Interview Questions
The HTTP Client API is now a standard feature in Java 11 (previously in Java 9 as incubator).
The HTTP Client provides a modern, easy-to-use API for HTTP requests with support for HTTP/2 and WebSocket.
import java.net.http.*;
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());- HTTP/1.1 and HTTP/2 support
- Asynchronous and synchronous requests
- WebSocket support
- Better than HttpURLConnection
- Modern API design
See HttpClientStandard.java for complete example.
New convenience methods for reading and writing files.
Java 11 adds readString() and writeString() methods to the Files class for simplified file I/O.
import java.nio.file.*;
// Read entire file as string
String content = Files.readString(Paths.get("file.txt"));
// Write string to file
Files.writeString(Paths.get("output.txt"), "Hello, Java 11!");
// With charset
String content = Files.readString(Paths.get("file.txt"),
StandardCharsets.UTF_8);- Simplified file reading
- Simplified file writing
- Automatic charset handling
- Less boilerplate code
See FilesMethods.java for complete example.
New methods for string manipulation and validation.
Java 11 adds several useful methods to the String class: isBlank(), lines(), strip(), stripLeading(), stripTrailing(), and repeat().
// isBlank() - checks if string is blank (whitespace only)
String str = " ";
boolean blank = str.isBlank(); // true
// lines() - returns stream of lines
String text = "Line 1\nLine 2\nLine 3";
text.lines().forEach(System.out::println);
// strip() - removes leading and trailing whitespace
String s = " hello ";
String stripped = s.strip(); // "hello"
// repeat() - repeats string
String repeated = "Java ".repeat(3); // "Java Java Java "isBlank(): Better than checkingtrim().isEmpty()lines(): Stream of lines from multiline stringstrip(): Unicode-aware trimrepeat(): Repeat string N times
See StringMethods.java for complete example.
A more readable way to check if an Optional is empty.
isEmpty() provides a clearer alternative to !isPresent().
Optional<String> optional = Optional.empty();
// Before Java 11
if (!optional.isPresent()) {
// handle empty
}
// Java 11+
if (optional.isEmpty()) {
// handle empty - more readable
}- More readable code
- Clearer intent
- Better than
!isPresent()
See OptionalIsEmpty.java for complete example.
A static method to negate predicates.
Predicate.not() provides a cleaner way to negate predicates in streams.
List<String> names = Arrays.asList("Alice", "", "Bob", " ");
// Filter non-blank strings
List<String> nonBlank = names.stream()
.filter(Predicate.not(String::isBlank))
.collect(Collectors.toList());- Cleaner than
filter(s -> !s.isBlank()) - Method reference support
- More readable
See PredicateNot.java for complete example.
The var keyword can now be used in lambda parameters.
Java 11 allows using var in lambda parameters for consistency with local variables.
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
// Using var in lambda
names.forEach((var name) -> System.out.println(name));
// With annotations
names.forEach((@NonNull var name) -> System.out.println(name));- Consistency with local variables
- Allows annotations on lambda parameters
- Type inference still works
See VarInLambda.java for complete example.
Improved access control for nested classes.
Nest-based access control allows classes in the same nest to access each other's private members without synthetic bridge methods.
- Better performance (no synthetic methods)
- Cleaner bytecode
- More efficient reflection
- Better encapsulation
See NestBasedAccessControl.java for complete example.
JFR is now open-source and available in OpenJDK.
Java Flight Recorder provides low-overhead profiling and monitoring capabilities.
- Low overhead profiling
- Production-ready monitoring
- Event collection
- Performance analysis
See JavaFlightRecorderDemo.java for complete example.
A no-op garbage collector for testing and performance analysis.
Epsilon GC does not perform any garbage collection, useful for testing memory allocation patterns.
java -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC MyApp- Performance testing
- Memory allocation analysis
- Short-lived applications
A low-latency garbage collector (experimental in Java 11).
ZGC is designed for applications requiring low latency and large heaps.
java -XX:+UnlockExperimentalVMOptions -XX:+UseZGC MyApp- Low latency
- Large heap support
- Concurrent collection
- Experimental in Java 11
Java EE and CORBA modules were removed from JDK.
These modules are now available as separate dependencies if needed.
java.xml.ws(JAX-WS)java.xml.bind(JAXB)java.activation(JAF)java.corba(CORBA)java.transaction(JTA)java.se.ee(Java SE EE)
- Smaller JDK size
- Use external dependencies if needed
- Better modularity
A: Key features:
- HTTP Client (standardized)
- Files methods (readString, writeString)
- String methods (isBlank, lines, strip, repeat)
- Optional.isEmpty()
- Predicate.not()
- Var in lambda parameters
- Nest-based access control
- JFR open-sourced
A:
- trim(): Removes only ASCII whitespace (≤ U+0020)
- strip(): Removes all Unicode whitespace characters
- stripLeading(): Removes leading whitespace
- stripTrailing(): Removes trailing whitespace
A: Modern HTTP API:
- HTTP/1.1 and HTTP/2 support
- Asynchronous and synchronous requests
- Better than HttpURLConnection
- Standardized in Java 11
A: Improved access control:
- Classes in same nest can access private members
- No synthetic bridge methods
- Better performance
- Cleaner bytecode
A: Removed modules:
- Java EE modules (JAX-WS, JAXB, etc.)
- CORBA module
- Available as external dependencies
Last Updated: 2025
Version: 1.0
This guide covers the features of Java 11 (LTS), released on September 25, 2018.