Back to: Java Tutorials
In this tutorial, we are going to discover the key features of Java 8, including lambda expressions, Stream API, Date and Time API, and more. Learn how Java 8 can improve your code.
Introduction
Java 8, released in March 2014, was a major update to the Java programming language that introduced several new features and enhancements. In this tutorial, we will discuss the main features of Java 8 and how they can be used to improve your code.

Key features of Java 8
Lambda Expressions
Lambda expressions are a powerful new feature of Java 8 that allow you to pass behavior as a method argument. This makes it easier to write functional-style code and can simplify your codebase. Lambda expressions can be thought of as anonymous methods that can be passed around and executed when needed.
The syntax for a lambda expression is as follows:
(parameter list) -> { expression }
For example, the following lambda expression takes an integer as input and returns its square:
x -> x * x
Lambda expressions can be used in a variety of contexts, such as sorting, filtering, and mapping collections of objects.
Stream API
The Stream API is another major feature of Java 8 that provides a simple and efficient way to process collections of objects in parallel. Streams allow you to perform operations on a collection of objects, such as filtering, mapping, and reducing, in a functional and declarative way.
For example, the following code creates a stream of integers, filters out the even numbers, squares each remaining number, and sums the results:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream()
.filter(n -> n % 2 != 0)
.map(n -> n * n)
.reduce(0, Integer::sum);
The Stream API can significantly improve the performance of your code by allowing you to process collections in parallel, especially when dealing with large datasets.
Date and Time API
The new Date and Time API introduced in Java 8 provides a more comprehensive and flexible way to handle dates and times. The API includes classes for representing dates, times, and time intervals, as well as classes for formatting and parsing dates and times.
For example, the following code creates a date and time object and formats it using a DateTimeFormatter:
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = now.format(formatter);
System.out.println(formattedDateTime);
The Date and Time API is more robust and flexible than the previous Date and Calendar classes and provides better support for handling time zones and daylight saving time.
Default Methods
Default methods are a new feature of Java 8 that allow you to add new methods to an interface without breaking backward compatibility. A default method is a method that is defined in an interface with a default implementation. Default methods can be overridden in implementing classes, or they can be inherited from the interface.
For example, the following code defines an interface with a default method:
interface Vehicle { default void drive() { System.out.println("Driving the vehicle"); } }
The default method can be overridden in a class that implements the interface:
class Car implements Vehicle { public void drive() { System.out.println("Driving the car"); } }
Default methods can be used to provide backward-compatible updates to existing interfaces or to add new functionality to existing interfaces without breaking existing implementations.
Method References
Method references are another new feature of Java 8 that enable you to refer to a method by its name instead of calling it directly. Method references can be thought of as a shorthand for lambda expressions that call a single method.
For example, the following code uses a method reference to sort a list of strings in alphabetical order:
List<String> names = Arrays.asList("John", "Mary", "Alice", "Bob");
names.sort(String::compareToIgnoreCase);
Method references can simplify your code and make it easier to read by removing unnecessary boilerplate code.
Optional Class
The Optional class is a new feature of Java 8 that provides a way to handle null values without having to use null checks. The Optional class is a container object that may or may not contain a non-null value. If a value is present, the Optional object provides methods for accessing and manipulating the value. If a value is not present, the Optional object provides methods for handling the absence of the value.
For example, the following code creates an Optional object that may or may not contain a string:
Optional<String> name = Optional.ofNullable(getName());
If the getName() method returns a non-null value, the Optional object will contain that value. If the getName() method returns null, the Optional object will be empty.
The Optional class can help prevent null pointer exceptions and make your code more robust and reliable.
Nashorn JavaScript Engine
The Nashorn JavaScript engine is a new feature of Java 8 that provides a way to run JavaScript code on the Java Virtual Machine. The Nashorn engine supports the latest ECMAScript 5.1 specification and provides high performance and low memory usage.
For example, the following code creates a Nashorn engine and evaluates a JavaScript expression:
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
Object result = engine.eval("var x = 5; x * 2");
The Nashorn engine can be used to integrate JavaScript code with Java applications, or to provide a JavaScript scripting environment within a Java application.
Parallel Array Sorting
Parallel array sorting is a new feature of Java 8 that provides a more efficient way to sort arrays in parallel. The Arrays class provides a new sort() method that can be used to sort arrays in parallel, using multiple threads.
For example, the following code sorts an array of integers in parallel:
int[] numbers = {5, 3, 8, 1, 9}; Arrays.parallelSort(numbers);
Parallel array sorting can significantly improve the performance of your code when dealing with large arrays.
Conclusion
In conclusion, Java 8 introduced many new features and enhancements that make the language more expressive, flexible, and powerful. The main features of Java 8 include lambda expressions, the Stream API, the Date and Time API, default methods, method references, the Optional class, the Nashorn JavaScript engine, and parallel array sorting. These features can help you write more efficient, robust, and maintainable code, and can provide a foundation for modern, functional-style programming in Java.
Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.
Follow us on social media
Follow Author