Static Method References in Overloading

Description

We can use overloaded static method in static method reference.

When the overloaded method we have to pay more attention to the method signature and corresponding functional interface.

Example

In the following list we have three versions of the valueOf() from the Integer class.


static Integer valueOf(int i)
static Integer  valueOf(String s)
static Integer  valueOf(String s, int radix)

The following code shows how different target functional interfaces can be used with the overloaded the Integer.valueOf() static methods.


import java.util.function.BiFunction;
import java.util.function.Function;
//www .  j a  v a  2  s . c om
public class Main{
  public static void main(String[] argv){
    // Uses  Integer.valueOf(int)
    Function<Integer, Integer> func1  = Integer::valueOf;

    // Uses  Integer.valueOf(String)
    Function<String, Integer> func2  = Integer::valueOf;

    // Uses  Integer.valueOf(String, int)
    BiFunction<String, Integer,  Integer> func3  = Integer::valueOf;

    System.out.println(func1.apply(7)); 
    System.out.println(func2.apply("7")); 
    System.out.println(func3.apply("101010101010", 2));
  }
}




















Home »
  Java Lambda »
    Java Lambda Tutorial »




Java Lambda Tutorial