Java 9-11 Features
Explore the Java Module System, Local Variable Type Inference (var), and HTTP Client.
Explore the Java Module System, Local Variable Type Inference (var), and HTTP Client. This hands-on tutorial focuses on practical implementation of java 9-11 features concepts.
Java 9-11 Features
Java 9: The Module System (JPMS)
The Java Platform Module System allows you to modularize applications, encapsulating packages and managing dependencies explicitly.
module-info.java
module com.example.myapp {
requires java.sql; // Depends on java.sql module
exports com.example.api; // Exposes com.example.api package
}
Java 10: Local Variable Type Inference (var)
You can allow the compiler to infer the type of a local variable.
var message = "Hello, Java 10"; // Inferred as String
var list = new ArrayList<String>(); // Inferred as ArrayList<String>
var stream = list.stream(); // Inferred as Stream<String>
[!NOTE]
varcan only be used for local variables with initializers. It cannot be used for method parameters, return types, or fields.
Java 11: HTTP Client API
A modern, standardized way to make HTTP requests, replacing HttpURLConnection.
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.github.com/"))
.GET()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
Other Notable Features
- String Methods (Java 11):
isBlank(),lines(),strip(),repeat(n). - Collection Factory Methods (Java 9):
List.of(),Set.of(),Map.of(). - Single Source File Execution (Java 11): Run
java HelloWorld.javadirectly withoutjavac.
AI Mentor
Confused about "Java 9, 10 and 11 key features including var and collection factories"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 3What does JPMS stand for?