Friday, August 4, 2017

What's Cool in Java 8, and New in Java 9

Java has evolved significantly, and Java 8 and Java 9 introduced features that made development more modern, expressive, and powerful. Let’s explore what made Java 8 stand out and what Java 9 brought to us:


Cool Features of Java 8

Java 8, released in 2014, introduced functional programming concepts and cleaner coding patterns.

Lambdas and Functional Interfaces

Lambdas reduce boilerplate code and make it easier to write inline behavior.


list.forEach(item -> System.out.println(item));

Streams API

Streams allow you to process collections declaratively with operations like map, filter, and reduce.


list.stream() .filter(x -> x.startsWith("A")) .map(String::toUpperCase) .forEach(System.out::println);

Optional

Optional provides a safer way to handle potential null values, reducing the chance of NullPointerException.

Optional<String> value = Optional.ofNullable(getValue()); value.ifPresent(System.out::println);

Default and Static Methods in Interfaces

Interfaces can now contain default implementations, which helps evolve APIs without breaking existing code.

interface MyInterface { default void sayHello() { System.out.println("Hello!"); } }

New Date and Time API

The java.time package offers an immutable, thread-safe way to handle dates and times.

LocalDate today = LocalDate.now();

What’s New in Java 9

Java 9, released in 2017, focused on modularity, improved APIs, and tooling for developers.

Java Platform Module System (JPMS)

Modules provide a way to better structure applications and the JDK itself.

module com.example.myapp { requires java.logging; }

JShell (Java REPL)

JShell allows developers to experiment with Java code interactively without writing full classes or methods.

jshell> System.out.println("Hello"); Hello

Stream API Enhancements

Java 9 added new stream methods like takeWhiledropWhile, and ofNullable.

Stream.of(1, 2, 3, 4, 5) .takeWhile(x -> x < 4) .forEach(System.out::println);

Improved APIs and Performance

  • Optional.ifPresentOrElse for more flexible handling of optional values

  • Compact string storage for better memory efficiency

  • Enhanced Process API for better control of system processes


Final Thoughts

Java 8 introduced modern programming techniques like lambdas and streams, while Java 9 added modularity, better tooling, and API enhancements. Together, they set a solid foundation for building cleaner, more scalable Java applications now and beyond.

If you’re exploring Java, both versions offer features that can significantly improve your codebase.

No comments: