Java 8 Vs Java 9
Java 8 Features
1. forEach()
forEach(): method is used to iterate the elements. It takes a single parameter. It is a default method of the Iterable interface. We can pass lambda expressions as an argument.
public class ForEachExample {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("One");
list.add("Two");
list.add("Three");
list.add("Four");
list.forEach(li -> System.out.println(li));
}
}
2. Functional Interfaces and Lambda Expressions
Functional Interface
The Functional interface contains only one abstract method and also contains a number of default and static methods. Functional Interface is annotated by @FunctionalInterface annotation, it ensures that the interface contains only one abstract method. Some predefined functional interfaces are: Runnable, Comparable, and ActionListener.
Lambda Expression
Lambda expressions provide the simplest and shortest way to define anonymous functions. Java Swing frequently uses anonymous functions for event handling. Lambda expressions define the body of Functional Interfaces. Lambda expression introduces a new operator in Java called the ARROW (->) operator.
For more click here lambda-expressions.html
3. Default and Static Methods in Interfaces
Before JDK 1.8, interfaces could only have abstract methods. From JDK 1.8, interfaces can have default and static methods too. The reason we have default methods in interfaces is to allow the developers to add new methods to the interfaces without affecting the classes that implement these interfaces.
interface FunctInt {
public void say();
public static void show() {}
public default void display() {}
}
4. Java Stream
For sequential and parallel operations with collections, usually with huge amounts of data, to perform filter/map/reduce-like operations, Java 8 introduces a new feature Stream API that exists in java.util.stream package which provides bulk operations on collections such as sequential or parallel map-reduce transformation.
List<Integer> list = new ArrayList<Integer>();
for (int i = 1; i < 10; i++) {
list.add(i);
}
Stream<Integer> stream = list.stream();
stream.forEach(p -> System.out.println(p));
Two special methods to generate a stream:
- stream() − returns a sequential stream on collection.
- parallelStream() − returns a parallel stream on collection.
Click here to see java-streams-api.html
5. Parallel Sort
To sort array elements in parallel, Java provides a new method parallelSort() in java.util.Arrays package.
Integer[] arr = {2, 3, 5, 1, 0, 8, 6, 4, 8};
Arrays.parallelSort(arr);
Java 9 Features
1. JShell (REPL)
JShell is a Read-Evaluate-Print Loop (REPL): For interactive mode and immediate results, Java added JShell from JDK 9. It allows us to immediately evaluate the result and make changes as needed.
G:\>jshell
| Welcome to JShell -- Version 9-ea
| For an introduction type: /help intro
jshell> int a = 10
a ==> 10
jshell> System.out.println("a value = " + a)
a value = 10
2. Private Methods in Interfaces
For sharing common code between two default methods or non-abstract methods, Java 9 introduced “Private Methods”. Private methods cannot be overridden in implementation classes and cannot be abstract.
public interface MyInterface {
default void methodFirst() {
init();
}
default void methodSecond() {
init();
}
private void init() {
System.out.println("Initializing");
}
}
3. Try With Resources Improvement
Before Java 9, resources could not be declared outside the try-with-resource block. But after Java 9, we can use the resources that are not declared locally.
FileOutputStream file =
new FileOutputStream("path");
try (file) {
// use file
}
But after Java 9 we can use the resources that are not declared locally.
4. Factory Methods for Immutable List, Set, Map and Map Entry
Java 9 provides a new feature called Factory Methods that create immutable or unmodifiable small collections of List, Set, and Map.
Factory method for:
List - List.of()
List<String> list =
List.of("One", "Two", "Three", "Four", "Five");
Set - Set.of()
Set<String> set =
Set.of("One", "Two", "Three", "Four", "Five");
Map - Map.of() and Map.ofEntries()
Map<Integer, String> map =
Map.of(1, "One", 2, "Two", 3, "Three");
5. <> (Diamond) Operator Enhancement
Now in Java 9, we can use diamond operators with anonymous classes that were not allowed in Java 7.
abstract class TestAnnoy<T> {
abstract T show(T a, T b);
}
public class AnnoyExample {
public static void main(String[] args) {
TestAnnoy<String> a = new TestAnnoy<>() {
String show(String a, String b) {
return a + b;
}
};
String result = a.show("Java", "9");
System.out.println(result);
}
}
6. JLink (Java Linker/Custom JRE)
Java 9 introduces JLink, allowing us to create a custom JRE to execute small programs. This helps in running only the required modules instead of the default JRE, which runs many unnecessary classes.
7. Underscore (_) Keyword
From Java 9, underscore (_) can no longer be used as an identifier. It is now a keyword, and using it as an identifier will result in a compile-time error.
Want to go deeper? Explore our Core Java course.
Transform Access To Education
Let's Try! Get This Free Support Solution?
Feel free to Call: (+91) 731 4249244