15 Java 11 New Features Explained
Java 11, released in September 2018, introduced several new features and enhancements that improve developer productivity, performance, and ease of use. Understanding these features is crucial for modern Java development, especially for those aiming to become Oracle Certified Professional Java SE 11 Developers.
Key Concepts
1. Local-Variable Syntax for Lambda Parameters
Java 11 allows the use of the var
keyword for defining local variables in lambda expressions. This feature simplifies the syntax and reduces redundancy.
Example
(var x, var y) -> x + y
2. HTTP Client API (Standardized)
Java 11 introduces a standardized HTTP Client API that supports both HTTP/1.1 and HTTP/2. This API simplifies making HTTP requests and handling responses.
Example
HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://example.com")) .build(); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
3. Nest-Based Access Control
Java 11 introduces nest-based access control, which allows nested classes to access private members of their enclosing class without requiring additional accessors.
Example
class Outer { private int outerVar = 10; class Inner { void accessOuter() { System.out.println(outerVar); } } }
4. Epsilon: No-Op Garbage Collector
Java 11 introduces Epsilon, a no-op garbage collector that performs no garbage collection. This is useful for performance testing and memory allocation experiments.
Example
java -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC MyApp
5. Flight Recorder
Java 11 includes Flight Recorder, a low-overhead data collection framework for troubleshooting Java applications. It captures detailed information about the application's behavior.
Example
java -XX:StartFlightRecording=filename=recording.jfr MyApp
6. Unicode 10 Support
Java 11 supports Unicode 10, which includes over 16,000 new characters and various scripts, symbols, and emojis.
Example
String unicodeChar = "\uD83E\uDD14"; // Unicode 10 character
7. New String Methods
Java 11 introduces several new methods in the String
class, such as isBlank()
, lines()
, strip()
, stripLeading()
, and stripTrailing()
.
Example
String str = " Hello, World! "; System.out.println(str.strip()); // "Hello, World!"
8. New File Methods
Java 11 introduces new methods in the Files
class, such as readString()
and writeString()
, which simplify reading and writing files.
Example
String content = Files.readString(Paths.get("file.txt")); Files.writeString(Paths.get("output.txt"), content);
9. New Collection Methods
Java 11 introduces a new method in the Collection
interface, toArray(IntFunction<T[]> generator)
, which simplifies converting collections to arrays.
Example
List<String> list = Arrays.asList("a", "b", "c"); String[] array = list.toArray(String[]::new);
10. New Predicate Methods
Java 11 introduces a new method in the Predicate
interface, not()
, which negates a predicate.
Example
Predicate<String> isEmpty = String::isEmpty; Predicate<String> isNotEmpty = Predicate.not(isEmpty);
11. New Optional Methods
Java 11 introduces a new method in the Optional
class, isEmpty()
, which checks if the Optional is empty.
Example
Optional<String> opt = Optional.empty(); System.out.println(opt.isEmpty()); // true
12. New Stream Methods
Java 11 introduces new methods in the Stream
interface, such as ofNullable()
and dropWhile()
, which enhance stream processing capabilities.
Example
Stream<String> stream = Stream.ofNullable("value"); stream.forEach(System.out::println); // "value"
13. New Path Methods
Java 11 introduces new methods in the Path
class, such as isSameFile()
, which checks if two paths point to the same file.
Example
Path path1 = Paths.get("file.txt"); Path path2 = Paths.get("file.txt"); System.out.println(Files.isSameFile(path1, path2)); // true
14. New Process API
Java 11 introduces enhancements to the Process API, such as ProcessHandle
, which provides more control over process management.
Example
ProcessHandle.allProcesses() .forEach(process -> System.out.println(process.info()));
15. Launch Single-File Source-Code Programs
Java 11 allows running single-file source-code programs directly without the need for compilation. This feature simplifies running small Java programs.
Example
java HelloWorld.java
Examples and Analogies
Think of Java 11's new features as tools in a toolbox that make your job as a developer easier and more efficient. Just as a toolbox contains various tools for different tasks, Java 11 provides new methods and APIs to handle different programming challenges. For example, the new HTTP Client API is like a modern drill that simplifies drilling holes, while the new String methods are like precision screwdrivers that make small adjustments easier.
For instance, if you are building a complex application, the new Process API is like having a remote control for managing different parts of the application. The new File methods are like having a smart storage system that makes organizing and accessing files a breeze.
By mastering these new features in Java 11, you can create more efficient, maintainable, and scalable applications, aligning with modern software development practices and enhancing your skills as an Oracle Certified Professional Java SE 11 Developer.