0

I just started learning Java's functional programming, and got really confused why the class implementation could work but the lambda expression won't in the following expression (I know the way to add the generic to the function is kind of silly, but its for learning purpose). Can anyone help?


@FunctionalInterface
interface Applier {
    <A> void apply(A a);
}

@FunctionalInterface
interface Getter {
    <V> V get();
}

public class Main {
    public static void main(String[] args) {
        Applier nonDoer = TestImpl::apply; //works
        Getter nonGetter = TestImpl::get; //works
        Applier lambdaApply = (Object o) -> {}; // does not work
        Getter lambdaGet = () -> null; // does not work
    }
}

class TestImpl {
    static void apply(Object O) {

    }

    static <V> V get() {
        return null;
    }
}
Jay
  • 9
  • 1
  • Please be more specific than "does not work". Are you getting an error message? (Which one?) – chrylis -cautiouslyoptimistic- Oct 30 '20 at 01:53
  • The short answer is: method references are treated differently from lambdas, when deciding whether or not it is compatible with a functional interface. The long answer you'll have to read the spec. – Sweeper Oct 30 '20 at 02:00

0 Answers0