Why does lambda translation need generation of a static method?

后端 未结 3 1299
花落未央
花落未央 2020-12-16 12:35

Lambda translation is a two step process, One: desugaring the lambda into a static method in same class.

public class Main {
    public stat         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 13:05

    Because this way it's actually cheaper. Generating a lambda from the method on the fly during the first invocation is better than loading a separate class via class loader. Internally it uses UNSAFE.defineAnonymousClass which is more light-weight class than normal. Such "lambda-class" is not bound to any class loader, so can be easily garbage-collected when it's no longer necessary. Also I guess there are plans to make this mechanism even more light-weight and faster. For normal anonymous class this would not be possible as from the point of JVM such classes don't differ from usual classes and much more heavy.

提交回复
热议问题