Java 9 features along with examples

Category : Java | Sub Category : Java 9 introduced features | By Prasad Bonam Last updated: 2023-08-09 07:33:59 Viewed : 308


Java 9 features along with examples :

Here are some Java 9 features along with examples to illustrate how they work:

  1. 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; }
  2. Private Interface Methods: Java 9 allows private methods in interfaces. This helps in organizing code without exposing unnecessary methods.

    java
    public interface MyInterface { default void publicMethod() { privateMethod(); } private void privateMethod() { System.out.println("Private method in interface"); } }
  3. Factory Methods for Immutable Collections: Java 9 introduced static factory methods for creating immutable collections.

    java
    import java.util.List; public class ImmutableCollectionsExample { public static void main(String[] args) { List<String> names = List.of("Alice", "Bob", "Charlie"); System.out.println(names); } }
  4. Stream API Enhancements: Java 9 added methods like takeWhile and dropWhile to the Stream API.

    java
    import 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 } }
  5. Private Methods in Interfaces: As shown in the private interface methods example above.

  6. HTTP/2 Client: Java 9 introduced a new HTTP client API.

    java
    import 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()); } }
  7. Process API Updates: Java 9 introduced enhancements to the Process API.

    java
    import 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()); } }
  8. 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.


Search
Related Articles

Leave a Comment: