Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8: Difference between method reference Bound Receiver and UnBound Receiver

I am trying to use Java 8 method references in my code. There are four types of method references available.

  1. Static method reference.
  2. Instance Method (Bound receiver).
  3. Instance Method (UnBound receiver).
  4. Constructor reference.

With Static method reference and Constructor reference i have no problem, but Instance Method (Bound receiver) and Instance Method (UnBound receiver) really confused me. In Bound receiver, we are using an Object reference variable for calling a method like:

objectRef::Instance Method

In UnBound receiver we are using Class name for calling a method like:

ClassName::Instance Method.

I have the following question:

  1. What is the need for different types of method references for Instance Methods?
  2. What is the difference between Bound and Unbound receiver method references?
  3. Where should we use Bound receiver and where should we use Unbound receiver?

I also found the explanation of Bound and Unbound receiver from Java 8 language features books, but was still confused with the actual concept.

like image 914
Harmeet Singh Taara Avatar asked Mar 10 '16 11:03

Harmeet Singh Taara


People also ask

What is an unbound reference?

In unbound references, the receiving object is specified when the function object is applied, via an additional parameter before the method's declared parameters. Unbound references are often used as mapping and filter functions in stream pipelines.

What is method reference in Java 8?

Java provides a new feature called method reference in Java 8. 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.

What is static method reference in Java?

A static method reference refers to a static method in a specific class. Its syntax is className::staticMethodName , where className identifies the class and staticMethodName identifies the static method. An example is Integer::bitCount .

How do I print a reference method?

How to print the values using method reference? in method references you just give the name of the function (println), they don't take arguments. You can create your own function that accepts a string and calls toUpperCase and then println, and then give the name of your function as the method reference name.


2 Answers

The idea of the unBound receiver such as String::length is you're referring to a method of an object that will be supplied as one of the lambda's parameters. For example, the lambda expression (String s) -> s.toUpperCase() can be rewritten as String::toUpperCase.

But Bounded refers to a situation when you’re calling a method in a lambda to an external object that already exists. For example, the lambda expression () -> expensiveTransaction.getValue() can be rewritten as expensiveTransaction::getValue.

Situations for three different ways of method reference

(args) -> ClassName.staticMethod(args) can be ClassName::staticMethod // This is static (you can think as unBound also)

(arg0, rest) -> arg0.instanceMethod(rest) can be ClassName::instanceMethod (arg0 is of type ClassName) // This is unBound

(args) -> expr.instanceMethod(args) can be expr::instanceMethod // This is Bound

Answer retrieved from Java 8 in Action book

like image 70
Johnny Willer Avatar answered Nov 24 '22 00:11

Johnny Willer


Basically, unbound receivers allow you to use instance methods as if they were static methods with a first parameter of the declaring type - so you can use them as functions by passing in whatever instance you want. With a bound receiver, the "target" instance is effectively part of the function.

An example might make this clearer:

import java.util.function.*;

public class Test {

    private final String name;

    public Test(String name) {
        this.name = name;
    }

    public static void main(String[] args) {
        Test t1 = new Test("t1");
        Test t2 = new Test("t2");

        Supplier<String> supplier = t2::method;
        Function<Test, String> function = Test::method;

        // No need to say which instance to call it on -
        // the supplier is bound to t2            
        System.out.println(supplier.get());

        // The function is unbound, so you need to specify
        // which instance to call it on
        System.out.println(function.apply(t1));
        System.out.println(function.apply(t2));
    }

    public String method() {
        return name;
    }
}
like image 36
Jon Skeet Avatar answered Nov 23 '22 23:11

Jon Skeet