Functional Interface:-Lambda Expressions In Java 8 Part 2
In my previous article Lambda Expressions In Java 8 Part 1, I explained about the basics of lamda expressions. In this article ,I will explain in details about Functional Interfaces . I highly recommend to fo through part 1 of this article before studying this.
Functional Interface:-
The concept of functional interface is introduced in java 8 to promote functional programming.Functional Interface is a special type of interface which can have only one abstract methods.
Note:-
1. Functional Interface can have only one abstract method.
2. Functional Interface can have any number of default methods.
3. @FunctionalInterface is an annotation which can be used in a functional interface to signal the JVM that this is an functional interface. If there are more than one abstract methods then an exception is thrown.
4. You can see the documentation of java.util.function package to check the functional interfaces provided in java 8.
In the last article I explained that lambda expressions are nothing but function as a value and the data type of that value can only be a functional interface. Lets take an example :-
This is a functional interface which has an abstract method apply and it takes an argument of type int.
This program prints 12001;
Below is another example which performs the same logic but here we use lambda expressions.
See how simple and short it is to write the same code using lamda functions .
Java 8 has provided many functional interfaces in its package java.util.function to use in most of the general usecases.
I hope this article must have cleared out the basic concepts behind functional interfaces in java 8.
Feel free to comment and ask in case of any doubts.
Functional Interface:-
The concept of functional interface is introduced in java 8 to promote functional programming.Functional Interface is a special type of interface which can have only one abstract methods.
Note:-
1. Functional Interface can have only one abstract method.
2. Functional Interface can have any number of default methods.
3. @FunctionalInterface is an annotation which can be used in a functional interface to signal the JVM that this is an functional interface. If there are more than one abstract methods then an exception is thrown.
4. You can see the documentation of java.util.function package to check the functional interfaces provided in java 8.
In the last article I explained that lambda expressions are nothing but function as a value and the data type of that value can only be a functional interface. Lets take an example :-
This program prints 12001;
Below is another example which performs the same logic but here we use lambda expressions.
See how simple and short it is to write the same code using lamda functions .
Java 8 has provided many functional interfaces in its package java.util.function to use in most of the general usecases.
I hope this article must have cleared out the basic concepts behind functional interfaces in java 8.
Feel free to comment and ask in case of any doubts.
Comments
Post a Comment