Java 9 - New Features: A Comprehensive Guide for Beginners

Hello, aspiring Java developers! I'm thrilled to be your guide on this exciting journey through Java 9's new features. As someone who's been teaching Java for many years, I can assure you that this version brings some game-changing improvements. So, let's dive in and explore these features together!

Java 9 - New Features

Introduction to Java 9

Before we delve into the new features, let's take a moment to understand what Java 9 is all about. Released in September 2017, Java 9 introduced several enhancements that make coding more efficient and enjoyable. It's like getting a shiny new toolbox with some cool gadgets you didn't even know you needed!

Module System

One of the most significant additions in Java 9 is the Module System, also known as Project Jigsaw. Now, I know what you're thinking - "Modules? That sounds complicated!" But don't worry, it's actually a neat way to organize your code better.

What is a Module?

Think of a module as a container for your code. It's like having a well-organized closet where everything has its place. Each module has:

  1. A name
  2. A set of packages
  3. A list of what it needs (dependencies)
  4. A list of what it offers to other modules

Let's look at a simple example:

module com.myapp {
    requires java.sql;
    exports com.myapp.api;
}

In this example, we're creating a module named com.myapp. It requires the java.sql module (because it needs some database functionality) and exports the com.myapp.api package (making it available for other modules to use).

REPL (Read-Eval-Print Loop)

Java 9 introduced JShell, a REPL tool. Now, you might be wondering, "What in the world is REPL?" Well, imagine having a conversation with Java where you can type in code snippets and get immediate responses. That's REPL!

To start JShell, simply type jshell in your command prompt. Let's try a simple example:

jshell> System.out.println("Hello, Java 9!")
Hello, Java 9!

Isn't that cool? You just ran Java code without creating a full program! It's perfect for testing ideas or learning new concepts.

Improved JavaDocs

Documentation might not sound exciting, but trust me, good documentation is a developer's best friend. Java 9 has made JavaDocs even better with the addition of search functionality and HTML5 support.

Collection Factory Methods

Creating collections in Java just got easier! Java 9 introduced new factory methods for collections. Let's look at how we create an immutable list:

List<String> fruits = List.of("Apple", "Banana", "Cherry");
System.out.println(fruits);

Output:

[Apple, Banana, Cherry]

Isn't that much cleaner than the old way? No more verbose ArrayList creation and adding elements one by one!

Private Interface Methods

Java 9 allows you to have private methods in interfaces. This might sound a bit abstract, so let's look at an example:

public interface MyInterface {
    private static void privateMethod() {
        System.out.println("This is a private method");
    }

    default void publicMethod() {
        privateMethod();
        System.out.println("This is a public method");
    }
}

In this example, privateMethod() can only be used within the interface. It's like having a secret helper that only the interface knows about!

Stream API Improvements

The Stream API, introduced in Java 8, got even better in Java 9. One cool addition is the takeWhile() method. Let's see it in action:

List<Integer> numbers = List.of(2, 4, 6, 8, 9, 10, 12);
List<Integer> evenNumbers = numbers.stream()
                                   .takeWhile(n -> n % 2 == 0)
                                   .collect(Collectors.toList());
System.out.println(evenNumbers);

Output:

[2, 4, 6, 8]

This code takes numbers from the list while they're even. As soon as it hits an odd number (9), it stops. Pretty neat, right?

Try-With-Resources Improvement

Java 7 introduced try-with-resources, and Java 9 made it even better. Now, you can use final or effectively final variables in try-with-resources statements. Here's an example:

BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
try (reader) {
    // Use the reader
} catch (IOException e) {
    e.printStackTrace();
}

This makes your code cleaner and more readable. It's like tidying up your room - everything just feels better!

Conclusion

We've just scratched the surface of what Java 9 has to offer. There are many more features like the Process API improvements, new versioning scheme, and HTTP/2 client. As you continue your Java journey, you'll discover how these features make your coding life easier and more enjoyable.

Remember, the best way to learn is by doing. So, fire up your IDE, start JShell, and experiment with these new features. Don't be afraid to make mistakes - that's how we learn and grow as developers.

Happy coding, future Java experts! ??‍??‍?

Credits: Image by storyset