Last updated on February 29th, 2024
Java 8 features give us concise and enabling functional programming in Java. Because modern programming languages are providing fewer lines of code to achieve some tasks in those languages. That’s why Java 8 introduced some features to write fewer lines of code like modern-day programming languages.
Example
Before Java 8 we can write code for this task:
public class Test {
public static int square(int n) {
return n*n;
}
public static void main(String[] args) {
System.out.println("Square Of No.="+square(10));
}
}
In Java 8 we can write code for this task:
import java.util.function.Function;
public class Test {
public static void main(String[] args) {
//Predefined Functional Interface with Lambda Expression
Function<Integer,Integer> f=i->i*i;
System.out.println("Square Of No.="+f.apply(10));
}
}
Here write fewer lines of code for this task with the use of the Java 8 feature.
Java 8 Features:
- Lambda Expressions
- Functional Interface
- Default Methods and Static Methods
- Predefined Functional Interfaces
- Predicate
- Function
- Consumer
- Supplier etc.
- Double Colon Operator(::)
- Method reference
- Constructor reference
- Streams
- Date and Time API
- Optional class
- Nashorn JavaScript Engine etc.
In the above, we mentioned some of the features of Java 8.
Lambda Expressions:
Lambda expressions are anonymous functions(i.e. without a name), without return type, without a modifier, and with arrow symbol “->”.
Syntax:
() -> expression
(parameters) -> expression
(parameters) ->{statement;}
Functional Interface:
An interface that contains a single abstract method but contains multiple default and static methods if needed. Invoking lambda expressions we need a functional interface.
Example:
public interface Test{
void m1( );
}
Default Methods and Static Methods:
Before Java 8 an Interface contained only abstract methods but in Java 8 features the Interface contains the default and static methods also.
Example:
public interface Test{
default void m(){
//your logic
}
}
In the above example we created a concrete method with a default modifier.
public interface Test{
static void m(){
//your logic
}
}
In the above example we created a concrete method with a static keyword.
Predefined Functional Interfaces:
Java 8 provides Lambda expression features but for the implementation of Lambda Expression, we need a functional interface. So Java 8 provides Predefined Functional Interfaces with functionalities to use Lambda Expression in regular code. Some of the Predefined Functional Interfaces are Predicate and Function etc.
Double Colon Operator(::):
The Double Colon Operator(::) is an alternative to Lambda Expression because Lambda Expression implements code separately whenever we use this. But using the double colon operator(::) we can reuse the code which is already existing with the help of Method and Constructor references.
Streams:
Java 8 Stream API is used to process the collection of data in different ways like filter and transformation. Use another way that may be useful for the application.
Date and Time API:
Date and Time API Java 8 features some modifications provided as compared to old Date and Time API like thread-safe, Local, etc.
Optional class:
Optional is used to contain not-null objects. This Optional class is containing different methods that provide a facility in our code to check values with “available” or “not available” instead of checking null values.
Nashorn JavaScript Engine:
Nashorn JavaScript Engine was introduced in Java 8. This feature helps Javascript code execute in JVM(Java Virtual Machine).
Conclusion:
This topic is explained What are the features of the Java 8 version? How can Java 8 concise the code?