Difference Between Java 8 and Java 9

The Java programming language has consistently evolved to meet the demands of modern software development. Two significant milestones in this evolution are Java 8 and Java 9. These versions brought substantial changes and enhancements to the language, libraries, and runtime environment. In this section, we will explore the key differences between Java 8 and Java 9, highlighting the improvements that each version introduced.

Key Differences Between Java8 and Java9

FeatureJava 8Java 9
Lambdas and Functional InterfacesIntroduced lambda expressions and functional interfaces.Continued support for lambdas and functional interfaces.
Stream APIIntroduced Stream API for functional-style operations on collections.Added new methods (takeWhile(), dropWhile(), etc.) to enhance Stream API.
Default Methods in InterfacesIntroduced default methods in interfaces for backward compatibility.Private methods in interfaces and improved diamond problem resolution.
Optional ClassIntroduced Optional class to handle null values more safely.Enhanced Optional class with methods like ifPresentOrElse(), stream(), and or().
Date and Time APIIntroduced new Date and Time API (java.time) to replace the old java.util.Date and java.util.Calendar classes.No significant changes to the Date and Time API.
Nashorn JavaScript EngineIntroduced Nashorn JavaScript engine as a replacement for the old Rhino engine.Continued support for Nashorn, but marked for removal in future releases.
PermGen RemovalPermGen memory space was removed and replaced with Metaspace to improve memory management.PermGen removal remains in Java 9, leading to improved memory allocation.
Compact StringsIntroduced compact strings to optimize memory usage for strings containing only ASCII characters.Compact strings remain in Java 9, leading to more memory-efficient string storage.
Project Jigsaw (Module System)No module system; applications were packaged using JAR files and classpath.Introduced the Java Platform Module System (JPMS) to create modular applications.
Interface Private MethodsInterfaces could only have abstract methods and static constants.Added support for private methods in interfaces for better code organization.
Reactive Streams APINo built-in support for reactive programming and handling asynchronous data streams.Introduced the java.util.concurrent.Flow package for Reactive Streams API.
New Versioning SchemeVersion numbers like Java 8, Java 7, etc.Introduced a more predictable versioning scheme with explicit version numbers.

Java 8 Example: Using Lambdas and Stream API

This program demonstrates the use of lambdas and the Stream API in Java 8 to filter and process a list of numbers

Java8Example.java

Output:

4
16
36
64
100

Java 9 Example: Using Module System and Private Interface Methods

This program showcases the Java 9 module system and the use of private interface methods to implement a simple calculator.

Java9Example.java

Output:

Sum: 8

Here is an example program that demonstrates the usage of some features introduced in Java 8 and Java 9. The program will showcase the use of lambdas, the Stream API, and the enhanced Optional class:

Java8vsJava9Example.java

Output:

Hello, Alice
Hello, David
Hello, John
Even numbers: [2, 4, 6]

Explanation

In this example, the program first demonstrates the use of lambdas and the Stream API in Java 8. It filters and prints names that start with "A" or "D" using lambda expressions.

Next, it showcases the enhanced Optional class methods in Java 9. The ifPresentOrElse() method is used to either print the name if it's present or print "No name available" if the Optional is empty.

Finally, the takeWhile() method introduced in Java 9 is demonstrated. It creates a new list containing even numbers from the input list until the first odd number is encountered.

This example highlights the practical application of features introduced in both Java 8 and Java 9 and shows how they contribute to writing more expressive and concise code.

Conclusion

Java 8 and Java 9 brought significant enhancements to the language and its ecosystem. Java 8 introduced lambdas, the Stream API, and default methods, revolutionizing how developers write code. Java 9 continued this evolution with features like the module system, stream improvements, and enhanced Optional class. As a developer, understanding these differences empowers you to leverage the strengths of each version, write more efficient and maintainable code, and stay up-to-date with the ever-evolving Java landscape.






Latest Courses