Category : Java | Sub Category : Java 9 introduced features | By Prasad Bonam Last updated: 2023-08-09 07:33:59 Viewed : 535
Java 9 features along with examples :
Here are some Java 9 features along with examples to illustrate how they work:
Module System (Project Jigsaw):
Java 9 introduced a module system to enhance encapsulation and modularity. Lets create a simple example with two modules, com.example.main
and com.example.util
.
java// module-info.java for com.example.main
module com.example.main {
requires com.example.util;
}
java// module-info.java for com.example.util
module com.example.util {
exports com.example.util;
}
Private Interface Methods: Java 9 allows private methods in interfaces. This helps in organizing code without exposing unnecessary methods.
javapublic interface MyInterface {
default void publicMethod() {
privateMethod();
}
private void privateMethod() {
System.out.println("Private method in interface");
}
}
Factory Methods for Immutable Collections: Java 9 introduced static factory methods for creating immutable collections.
javaimport java.util.List;
public class ImmutableCollectionsExample {
public static void main(String[] args) {
List<String> names = List.of("Alice", "Bob", "Charlie");
System.out.println(names);
}
}
Stream API Enhancements:
Java 9 added methods like takeWhile
and dropWhile
to the Stream API.
javaimport java.util.stream.Stream;
public class StreamEnhancementsExample {
public static void main(String[] args) {
Stream<Integer> numbers = Stream.of(2, 4, 6, 8, 10, 12);
numbers.takeWhile(n -> n % 4 == 0).forEach(System.out::println); // Outputs: 2 4 6 8
}
}
Private Methods in Interfaces: As shown in the private interface methods example above.
HTTP/2 Client: Java 9 introduced a new HTTP client API.
javaimport java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class HttpClientExample {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("https://www.example.com"))
.GET()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}
Process API Updates: Java 9 introduced enhancements to the Process API.
javaimport java.lang.ProcessHandle;
public class ProcessApiExample {
public static void main(String[] args) {
ProcessHandle currentProcess = ProcessHandle.current();
System.out.println("Process ID: " + currentProcess.pid());
System.out.println("Is alive? " + currentProcess.isAlive());
}
}
Multi-Release JAR Files: Create a multi-release JAR to include different versions of a class for different Java releases.
java/com/example/Example.java (Java 8 version)
/com/example/Example.java (Java 9 version)
The appropriate version of the class will be loaded based on the Java version.
These examples provide a glimpse of how Java 9 features can be used in practical scenarios. Remember that the syntax and usage may vary based on your specific development environment and requirements.