Q. Java 8 features
A: There are many features of java 8.
- Method references/Anonymous functions,
- Lambda expressions,
- Functional interfaces,
- Stream API,
- Default methods,
- Base64 Encode Decode,
- Static methods in interface,
- Optional class,
- Collectors class,
- ForEach() method,
- Parallel array sorting,
Method references/Anonymous functions
Java provides a new feature called method reference. Method reference is used to refer method of functional interface. It is compact and easy form of lambda expression. Each time when you are using lambda expression to just referring a method, you can replace your lambda expression with method reference.
Types of Method references:
There are following types of method references in java:
- Reference to a static method: Syntex:- ContainingClass::staticMethodName
- interface Sayable{
- void say();
- }
- public class MethodReference {
- public static void saySomething(){
- System.out.println("Hello, this is static method.");
- }
- public static void main(String[] args) {
-
- Sayable sayable = MethodReference::saySomething;
-
- sayable.say();
- }
- }
In the following example, we are using predefined functional interface Runnable to refer static method.
- public class MethodReference2 {
- public static void ThreadStatus(){
- System.out.println("Thread is running...");
- }
- public static void main(String[] args) {
- Thread t2=new Thread(MethodReference2::ThreadStatus);
- t2.start();
- }
- }
Example 3
You can also use predefined functional interface to refer methods. In the following example, we are using BiFunction interface and using it's apply() method.
- import java.util.function.BiFunction;
- class Arithmetic{
- public static int add(int a, int b){
- return a+b;
- }
- }
- public class MethodReference3 {
- public static void main(String[] args) {
- BiFunction<Integer, Integer, Integer>adder = Arithmetic::add;
- int result = adder.apply(10, 20);
- System.out.println(result);
- }
- }
Example 4
You can also override static methods by referring methods. In the following example, we have defined and overloaded three add methods.
- import java.util.function.BiFunction;
- class Arithmetic{
- public static int add(int a, int b){
- return a+b;
- }
- public static float add(int a, float b){
- return a+b;
- }
- public static float add(float a, float b){
- return a+b;
- }
- }
- public class MethodReference4 {
- public static void main(String[] args) {
- BiFunction<Integer, Integer, Integer>adder1 = Arithmetic::add;
- BiFunction<Integer, Float, Float>adder2 = Arithmetic::add;
- BiFunction<Float, Float, Float>adder3 = Arithmetic::add;
- int result1 = adder1.apply(10, 20);
- float result2 = adder2.apply(10, 20.0f);
- float result3 = adder3.apply(10.0f, 20.0f);
- System.out.println(result1);
- System.out.println(result2);
- System.out.println(result3);
- }
- }
Reference to an instance method.:
like static methods, you can refer instance methods also. In the following example, we are describing the process of referring the instance method.
Syntax
- containingObject::instanceMethodName
Example 1
In the following example, we are referring non-static methods. You can refer methods by class object and anonymous object.
- interface Sayable{
- void say();
- }
- public class InstanceMethodReference {
- public void saySomething(){
- System.out.println("Hello, this is non-static method.");
- }
- public static void main(String[] args) {
- InstanceMethodReference methodReference = new InstanceMethodReference();
-
- Sayable sayable = methodReference::saySomething;
-
- sayable.say();
-
- Sayable sayable2 = new InstanceMethodReference()::saySomething;
-
- sayable2.say();
- }
- }
Test it NowOutput:
Hello, this is non-static method.
Hello, this is non-static method.
Example 2
In the following example, we are referring instance (non-static) method. Runnable interface contains only one abstract method. So, we can use it as functional interface.
- public class InstanceMethodReference2 {
- public void printnMsg(){
- System.out.println("Hello, this is instance method");
- }
- public static void main(String[] args) {
- Thread t2=new Thread(new InstanceMethodReference2()::printnMsg);
- t2.start();
- }
- }
Test it NowOutput:
Hello, this is instance method
Example 3
In the following example, we are using BiFunction interface. It is a predefined interface and contains a functional method apply(). Here, we are referring add method to apply method.
- import java.util.function.BiFunction;
- class Arithmetic{
- public int add(int a, int b){
- return a+b;
- }
- }
- public class InstanceMethodReference3 {
- public static void main(String[] args) {
- BiFunction<Integer, Integer, Integer>adder = new Arithmetic()::add;
- int result = adder.apply(10, 20);
- System.out.println(result);
- }
- }
Reference to a constructor:
You can refer a constructor by using the new keyword. Here, we are referring constructor with the help of functional interface.Syntax
Example
- interface Messageable{
- Message getMessage(String msg);
- }
- class Message{
- Message(String msg){
- System.out.print(msg);
- }
- }
- public class ConstructorReference {
- public static void main(String[] args) {
- Messageable hello = Message::new;
- hello.getMessage("Hello");
- }
- }
3. Functional/SAM interface:Functional interfaces are those interfaces which can have only one abstract method. It can have any number of default, static methods but can contain only one abstract method.
Note: Java provides predefined functional interfaces to deal with functional programming by using lambda and method references.
No comments:
Post a Comment