Java - Method reference of the overloaded static methods

Introduction

valueOf() of the Integer class has three versions:

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

The following code shows how different target types will use the three different versions of the Integer.valueOf() static method.

Demo

import java.util.function.BiFunction;
import java.util.function.Function;

public class Main {
  public static void main(String[] args) {
    // 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(17));
    System.out.println(func2.apply("17"));
    System.out.println(func3.apply("10001", 2));

  }//from  w  ww .j a va  2s .co  m
}

Result

Related Topic