2

To exploring lambda and its working, i write below code.

public class Foo { // enclose class

public static void main(String[] args) {
    System.out.println(new Foo().new InnerFoo());
    IntSupplier intSupplier = () -> 42;
    System.out.println(intSupplier);
}

class InnerFoo{} // inner class

}

After execution of code, i come across below output.

Foo$InnerFoo@3af49f1c
Foo$$Lambda$15/0x0000000840064c40@13221655

i have basically 2 question based on output.

  1. To best of my knowledge, when every inner class is instantiated, it can be represent as EnclosingClass$InnerClass@hashcode as toString()(in case we do not override toString()). so does that mean, my Foo$$Lambda$15/0x0000000840064c40@13221655 is a definition of inner class ? if is to so, than how long that inner class loaded remains in JVM ?
  2. What is an implication of $$Lambda. How JVM will interpret it ?
RBS
  • 207
  • 1
  • 11
  • 1
    Yes, a lambda becomes an inner class. What do you mean "what is an implication of $$Lambda"? That's part of the autogenerated `toString` of the inner class. – daniu Apr 27 '23 at 13:18
  • Well, for most purposes it is the same as an anonymous inner class, like we wrote Comparators, EventHandlers etc for two decades: `new IntSupplier() { int getAsInt() {return 42;} };` If you want to dig deeper into how it is actually implemented in the JVM, your search term might be [LambdaMetafactory](https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/invoke/LambdaMetafactory.html). – Hulk Apr 27 '23 at 13:25
  • 3
    See [What does $$ in javac generated name mean?](https://stackoverflow.com/q/36057007/2711488) and [Meaning of class file on the file system](https://stackoverflow.com/q/44475789/2711488) – Holger Apr 28 '23 at 16:25

1 Answers1

6

That is the name of a hidden class, as proposed in JEP 371. Since Java 15, this is how lambdas are implemented.

These are classes that are generated at runtime, using Lookup.defineHiddenClass, rather than ClassLoader.defineClass. Therefore, they are not loaded by a class loader.

As for the output of your code, the part after the @ is the identity hash code of the object, as usual. The part before that is the name of the hidden class, Foo$$Lambda$15/0x0000000840064c40. This can be separated into two parts: Foo$$Lambda$15 is the name used in the class definition (the bytes, in the class file format that is passed to defineHiddenClass), and 0x0000000840064c40 is an unqualified name generated by the JVM. The $$ doesn't mean anything special. $ is a valid character to be used in a class name.

Towards the end of the JEP, it discusses the lifetime of hidden classes. The gist of it is that hidden classes can be unloaded as soon as there are no longer any live instances of the class. e.g. when the object created by the lambda expression is garbage collected.

Sweeper
  • 213,210
  • 22
  • 193
  • 313