4

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.

optional
  • 3,260
  • 5
  • 26
  • 47
  • 2
    Its just a [`MethodSnippet`](https://docs.oracle.com/javase/9/docs/api/jdk/jshell/MethodSnippet.html) and I believe since JShell primarily relies on [`Snippet`](https://docs.oracle.com/javase/9/docs/api/jdk/jshell/Snippet.html) and its evaluation. I would insist you to read over its types/subclasses. Probably then, I hope that the question wouldn't turn into how does JShell evaluate snippets. – Naman Nov 06 '17 at 10:59

1 Answers1

4

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
Eugene
  • 117,005
  • 15
  • 201
  • 306
  • where I can get the details for jdk.jshell.Jshell class ,any link is much appreciated ? – optional Nov 06 '17 at 10:29
  • 2
    @Mukeshkumarsaini did you try googling? seriously, the first 10 searches would give u a very big picture about this – Eugene Nov 06 '17 at 10:33
  • 1
    https://docs.oracle.com/javase/9/docs/api/jdk/jshell/package-summary.html got it here thanks – optional Nov 06 '17 at 10:36
  • 4
    In other words, its just a [MethodSnippet](https://docs.oracle.com/javase/9/docs/api/jdk/jshell/MethodSnippet.html) – Naman Nov 06 '17 at 10:50