Method References in Java 8 – Types

In Java 8 method references has introduced, which will refer to methods or constructors without invoking them. These syntactic shortcuts create lambdas from existing methods or constructors. Basically, lambda expressions in Java are methods implemented without an enclosing class body and constructors created by lambdas are often referred to as constructor references, which consider to be a subset of method references.

In short, not all methods can be implemented as lambdas. But only those which declared inside candidate functional interface (an interface which contains only one abstract method called as functional interface) are the privileged ones.

So, we will see here how we can convert method parameters based on anonymous implementations of functional interfaces into lambdas:

Any implementation of a Functional interface can be converted to lambda expression.

When we will observe here, all that we need to be done is eliminating the elements which could be implicitly available to the compiler and separating the parameters and the actual expression by “->” operator.

So, one question here arises then when we should go for method references?

Simply we can use method references anywhere we can use Lambda Expressions, which means that a Functional Interface is needed, but only if the Lambda Expression would invoke a single, already defined, method & do nothing else.

The method signature must also match that of the Functional Interface being used. For eg, when used in conjunction with a Predicate, which requires a single input argument & returns a Boolean value, that method would need to accept an argument & return a Boolean value. We will not be able to use method references if we need to invoke more than one method within a Lambda Expression or if we need to pass extra arguments into the method.

In Java 8, we can perform method reference by following 4 types:

1Reference to a static type


Output:


Thread-0: 0
Thread-1: 0
Thread-1: 1
Thread-1: 2
Thread-0: 1
Thread-1: 3
Thread-0: 2
Thread-0: 3
Thread-0: 4
Thread-0: 5
Thread-0: 6
Thread-0: 7
Thread-1: 4
Thread-1: 5
Thread-2: 0
Thread-1: 6
Thread-2: 1
Thread-2: 2
Thread-2: 3
Thread-1: 7
Thread-2: 4
Thread-2: 5
Thread-2: 6
Thread-2: 7


Here, there are three ways to pass a unit of work described by the getThreadValue() method to a new Thread object whose associated thread is started:

  • It will pass a method reference to the static getThreadValue() method
  • It will pass an equivalent lambda whose code block executes getThreadValue()
  • It will pass an instance of an anonymous class that implements Runnable and runs getThreadValue() method.

2. Reference to an Instance Method of a Particular Object

The object reference on which an instance method is invoked is known as the Receiver of the method invocation.

We can specify the receiver of the method invocation: provide it implicitly when the method is invoked.

a) Bound receiver,

b) Unbound receiver.

a) Bound receiver: it can provide Explicitly when the method is invoked

Syntax: objectRef::instanceMethod

Output:

By References To Instance: 24

References To Instance bound type: 120

b) Unbound receiver:  it can provide implicitly when the method is invoked.

Output:

By Using lambda expression: 120

Using a method References To Instance Unbound type: 720


BiFunction<T,U,R> : Represents a function that accepts two arguments and produces a result. This is the two-arity specialization of Function.

Ref:  https://docs.oracle.com/javase/8/docs/api/java/util/function/BiFunction.html

3. Reference to an instance method of an arbitrary object of a particular type

Syntax :  ContainingType::methodName

Output:

this::calculate(): Fibonacci Series = 0 1 1 2 3 5 8

Factorial::calculate(): Factorial = 30

4. Reference to a constructor

Syntax:  ClassName::new

Output:

Sqauare Value by Reference to a constructor: 36

Leave a Reply