Java 8 Introduced Key Features
Java 8 introduced several key features that significantly enhanced the language and its libraries. Here are the principal features:
Lambda Expressions:
Allow you to treat functionality as a method argument, or to create small function-like objects more concisely.
Functional Interfaces:
An interface with just one abstract method, used as the type for lambda expressions and method references.
Stream API:
Introduces a new abstraction for working with sequences of elements and performing operations like filter, map, and reduce on them.
Default Methods:
Allow you to add new methods to interfaces without breaking the existing implementations.
Optional:
A container object which may or may not contain a non-null value, to avoid `NullPointerException`.
opt.ifPresent(System.out::println);
New Date and Time API:
A comprehensive new date and time library under `java.time` package, which is more flexible and easier to use than the old java.util.Date and java.util.Calendar classes.
Nashorn JavaScript Engine:
Replaces the old Rhino JavaScript engine, allowing for the embedding of JavaScript code within Java applications.
Method References:
Provide a way to refer to methods without invoking them, which can be used for constructing lambda expressions.
These features aim to make Java code more expressive, readable, and efficient.
Lambda Expressions:
Allow you to treat functionality as a method argument, or to create small function-like objects more concisely.
Syntax:
(parameters) -> expression or (parameters) -> { statements; }
An interface with just one abstract method, used as the type for lambda expressions and method references.
Example:
java.util.function.Predicate;
java.util.function.Function;
Introduces a new abstraction for working with sequences of elements and performing operations like filter, map, and reduce on them.
Example:
List myList = ...; myList.stream().filter(s -> s.startsWith("a")).forEach(System.out::println);
Allow you to add new methods to interfaces without breaking the existing implementations.
Example:
interface MyInterface {
default void newMethod() {
............ // write Method body
} }
A container object which may or may not contain a non-null value, to avoid `NullPointerException`.
Example:
Optional opt = Optional.of("value");
A comprehensive new date and time library under `java.time` package, which is more flexible and easier to use than the old java.util.Date and java.util.Calendar classes.
Nashorn JavaScript Engine:
Replaces the old Rhino JavaScript engine, allowing for the embedding of JavaScript code within Java applications.
Method References:
Provide a way to refer to methods without invoking them, which can be used for constructing lambda expressions.
Example: ClassName::methodName.
These features aim to make Java code more expressive, readable, and efficient.
Subscribe to:
Posts
(
Atom
)