1

In this article they say:

Anonymous compiles to a class, while lambda is an invokedynamic instruction.

As I understand when we create lambda in java then this lambda compiler doesn't create a separate class. If it is correct, is it possible to create an instance of lambda without generating a class in byte buddy? If yes, could anyone give example of how to do it for this example:

Function<Integer, Integer> f = (x) -> { return x * x; };
ThatsMe
  • 123
  • 4
  • 24
  • @oleg.cherednik that's [not correct](https://stackoverflow.com/q/16827262/2541560). – Kayaman May 11 '21 at 13:04
  • My powers don't include knowing why questions are downvoted. Downvotes happen for many reasons and they're chaotic. The tooltip on the downvote button says "This question doesn't show any research effort; it is unclear or not useful". – Kayaman May 11 '21 at 19:12

1 Answers1

2

I have written an extensive description of invokedynamic. In essence: The JVM writes the lambda body to a private method. At runtime, it dispatches the dyanmic call to the JVM's lambda meta factory that creates a class on the fly that implements the functional interface. This might however change in the future.

Byte Buddy allows you to create invokedynamic call sites that point to the same lambda metafactory, but they will apply the same mechanism.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
  • From your article - `These methods are named lambda$X$Y with X being the name of the method that contains the lambda expression and with Y being a zero-based sequence number`. And later in example `private /* non-static */ void lambda$foo$0(int j, int k)` . However, the lambda is defined in bar method, not in foo. Is it a typo or I understand something wrong? – Pavel_K May 18 '21 at 04:19