0

Below is a simple interface that my lambda will target.

@FunctionalInterface
interface Converter<F, T> {
  T convert(F from);
}

Here is my targeting lambda in the first line of the code below.

Converter<String, Integer> converter = (from) -> Integer.valueOf(from);
Integer converted = converter.convert("123");
System.out.println(converted);

Syntax-wise, I can even further simplify my targetting line like this:

Converter<String, Integer> converter = Integer::valueOf;

My question is about the reference to the static method valueOf() in Integer. (i.e. Integer::valueOf)

Why does its presence work here in place of a lambda? Is it because the lambda is there (hiding) and the lambda is providing a method body for convert() by "borrowing" an existing body from elsewhere (i.e. the Integer class), instead of explicitly defining the body?

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
softwarelover
  • 1,009
  • 1
  • 10
  • 22
  • 2
    You should read about [method references](https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html) – Naman Nov 01 '17 at 03:55
  • 1
    also: https://stackoverflow.com/questions/32283833/please-explain-java-8-method-reference-to-instance-method-using-class-name and: https://stackoverflow.com/questions/44720264/method-reference-in-java – Nir Alfasi Nov 01 '17 at 04:03
  • Neither the lambda expression nor the method reference provide a body for the `convert` method. The generated `Converter` instances consist of a plain delegation (a single method invocation) in both cases. In one case, the invocation of a synthetic method holding your lambda expression’s body, in the other case, the invocation to the already existing method `Integer.valueOf`. – Holger Nov 01 '17 at 13:41
  • @Holger, somehow there has to be an implementation of convert() declared in the Converter interface. We are associating a lambda when we create an instance of Converter. Does it not imply the lambda behind the scene is providing an implementation? In this example, the lambda is simple, but a lambda can be as complex as multiple lines of code within curly brackets, with return statement in the end, etc.... Also, you used the word "delegate". We can delegate only if we are inside a method. – softwarelover Nov 01 '17 at 15:55
  • Maybe, my explanation was too brief. There will be a JRE generated implementation class of `Converter` whose `convert` method will consist of a delegation to another method and nothing else. That’s a simple structure, easy to generated at runtime. Your lambda expression’s body will get compiled to a special method of the class containing the lambda expression and may be as complex as an ordinary method. The generated `Converter` implementation will eventually invoke that method. In the case of the method reference, the `Converter` implementation will usually invoke the target method directly. – Holger Nov 01 '17 at 17:26
  • Now, that's what I call "perfect". – softwarelover Nov 02 '17 at 15:24

0 Answers0