Wednesday, 20 August 2014

Method References In JAVA 8

Method References :

    Method reference is an important feature related to lambda expressions. It provides a way to a method without executing it. In order to that a method reference requires a target type context that consists of a compatible functional interface.

Similar to lambda expressions, a method reference when evaluated creates an instance of the functional interface. The 3 types of method references are,
1. Static Method References.
2. Instance Method References Of Objects.
3. Instance Method References Of Classes.

In the first two types, the method reference is equivalent to a lambda expression on which parameters of a method will be supplied. In the 3rd type, the 1st parameter of a method becomes the target of the method and second parameter becomes argument to the method just like s1.equals(s2); .

Static Method References :

 The syntax for static method references is
ClassName :: methodName
Lets see a simple example to understand static method references.In this example we create a Thread.
package com.speakingcs.lambda;

public class MetRefThread { 
 
    public static void runBody() {
 for(int i = 0;i < 10; i++) {
     System.out.println("square of i is " + (i*i));
        }
    }
 
    public static void main(String[] args) {
 new Thread(MetRefThread :: runBody).start();
    }

}
In this example we have passed runBody() as method reference, which is simillar to run() of Thread class. This method also doesn't accept any arguments and is void type, just like run(). You can refer any method with void type and no arguments as a method reference to Runnable Interface.

This method can be used any where in which it is compatible with its target type.

Instance Method References Of Objects :

    You can pass a reference to an instance method on a specific object, the syntax is
 objRef :: methodName 
Here is the example, this example is similar to above, except here we use instance method reference.
 
package com.speakingcs.lambda;

public class MetRefThread { 
 
     public void runBody() {
 for(int i = 0;i < 10; i++) {
     System.out.println("square of i is " + (i*i));
 }
     }
 
     public static void main(String[] args) {
 MetRefThread mr = new MetRefThread();
 
 new Thread(mr :: runBody).start();
     }

}
Instance Method References Of Classes :

    You can also pass a reference to an instance method on class name directly. This can be useful in situations where you want to specify an instance method can be used with any object of a given class.

The general syntax is
 ClassName :: methodName 
Lets write a simple program to sort array of Strings by using Instance method reference of a String class.
 
package com.speakingcs.lambda;

import java.util.Arrays;

public class ArraySort {

     public static void main(String[] args) {
 String[] strArray = {"abe","adb","deb","abc","ghi","acd","acg","acb"};
  
 Arrays.sort(strArray,String::compareToIgnoreCase);
 
 for(String str: strArray) {
     System.out.print(str+" ");
 }
    }
}
here, we passed compateToIgnoreCase() method of String class as an argument to sort() method of Arrays class. sort() method takes a generic array and an instance of Comparator as arguments, and Comparator is a functional interface is Java 8.
you can also capture "this" and "super" variables in method references.

Using "this" Variable In Method References :

Lets rewrite the program to create a Thread using "this" variable in method reference.
package com.speakingcs.lambda;

public class MetRefThread { 
 
    public void runBody() {
 for(int i = 0;i < 10; i++) {
    System.out.println("square of i is " + (i*i));
 }
    }
 
    public static void main(String[] args) {
 MetRefThread mr = new MetRefThread();
 mr.createThread();
    }

    private void createThread() {
  new Thread(this :: runBody).start();  
    }

}
Using "super" Variable In Method References :

You can refer a method from super class using super variable. Lets see an example.
 
package com.speakingcs.lambda;

class superRef {
    public void runBody() {
 for(int i = 0; i < 10; i++) {
        System.out.println("square of i is " + (i*i));
 }
   }
}

public class MetRefSuper extends superRef{
    public static void main(String[] args) {
 MetRefSuper mrs = new MetRefSuper();
 mrs.createThread();  
    }

    private void createThread() {
 new Thread(super :: runBody).start();  
    }
}


0 comments:

Post a Comment

Note: only a member of this blog may post a comment.