why the following code can pass compile phase and run correctly?
There are two points that I can't understand:
First,mapToLong
method accept a functionalInterface like this
@FunctionalInterface
public interface ToLongFunction<T> {
/**
* Applies this function to the given argument.
*
* @param value the function argument
* @return the function result
*/
long applyAsLong(T value);
}
but the method longValue
of class Number
is public abstract long longValue();
Second, the method longValue
is a abstract method, but it can be passed to the mapToLong
method as a argument, why is that?
Here is the code:
package com.pay.screen;
import java.util.ArrayList;
import java.util.List;
public class MainTest {
public static void main(String[] args) {
List<Long> list = new ArrayList<>();
list.add(1L);
list.add(2L);
long sum = list.stream().mapToLong(Number::longValue).sum();
System.out.println(sum);
}
}