Method declared in JShell are lambdas?

Viewed 392

I am a bit curious about how method ,declared in JShell is implemented under the hood .

eg .

int add(int x,int y){
return x+y;
}

is above declared method instance of BiFunction ? May be a stupid question but just for curiosity.

1 Answers

That's a plain usual method, why would it be created as a BiFunction? unless you tell it to, of course.

There is a top level class called jdk.jshell.JShell that holds this method (or any other state); but you can't use it to declare a method reference:

jshell> int add(int x, int y) { return x + y; }

Meaning this would not work:

jshell> BiFunction<Integer, Integer> by = JShell::add; // or this::add

You would have to wrap that add method in a class:

jshell> class Foo { static int add(int x, int y) { return x + y; } ... 

And then assign it:

jshell> BiFunction<Integer, Integer, Integer> bi = Foo::add;
        bi ==> $Lambda$15/1757676444@ae45eb6
Related