Java 1.8 New Features
Following are the main features of Java 1.8
1. Lambda Expressions
2. Stream API
3. Functional Interfaces
4. Default Methods in Interfaces
5. Static Methods in Interfaces
6. Predefined Functional Interfaces like Predicate, Function, Consumer,
Supplier.
7. Method Reference & Constructer Ref. by Double Colon (::) Operator.
8. Date & Time API (Joda API)
The main intension of java 1.8 is to enable functional programming in java.
It doesn't mean java is function oriented programming language.
But we are getting benefits of functional programming by using Lambda Expression.
Purpose of Java 1.8
To simplify programming.
To utilize functional programming benefits in java.
To enable parallel processing in java.
What is Lambda Expression?
It is an anonymous function, i.e. a function without any name, modifiers and return type and with a list of formal parameters and a body.
What is Functional Interface?
A functional interface is an interface that contains only a single abstract method (a method that doesn’t have a body).
You don’t have to include the abstract keyword because, by default, a method declared inside a functional interface is abstract.
Only one abstract method is allowed, but it accepts multiple default and static methods.
The main reason why we need functional interfaces is that we can use them in lambda expressions and method references. This way, we reduce boilerplate code.
The main benefit of using functional interfaces is that they can be instantiated using lambda expressions rather than lengthy anonymous classes.
What is Optional?
Optional is a new type introduced in Java 8.
It is used to represent a value that may or may not be present.
In other words, an Optional object can either contain a non-null value (in which case it is considered present) or it can contain no value at all (in which case it is considered empty).
By using Optional, you can avoid NullPointerExceptions and write cleaner code.
An Optional object can have one of the following possible states:
Present: The Optional object does not represent absence.
A value is in the Optional object and it can be accessed by invoking get().
Absent: The Optional object does represent absence of a value; you
cannot access its content with get().
How to create an Optional object in java?
There are several ways to create an Optional object in Java, including the static factory methods empty() and of(), which pertain to the Optional class.
You can create an Optional object using the of() method, which will return an Optional object containing the given value if the value is non-null, or an empty Optional object if the value is null.
Programmers can also use the ofNullable() method, which will return an empty Optional object if the value is null, or an Optional object containing the given value if it is non-null.
Finally, you can create an empty Optional object using the empty() method.
Once you have created an Optional object, you can use the isPresent() method to check if it contains a non-null value.
If it does, you can use the get() method to retrieve the value. Developers can also use the getOrElse() method, which will return the value if it is present, or a default value if it is not.