|
|
|
|
|
by cogman10
42 days ago
|
|
> many still think nested anonymous classes are used. Anonymous classes are still used (sometimes). It simply depends on the circumstance of the lambda. For example, this will result in a new anonymous class being generated. void foo(String s) {
stream.filter(i->s.equals(i));
}
The class gets generated to capture the `s` variable. Indy gets used in the `filter` method because the incoming lambda or method reference could be several types of method calls. For example, a constructor, an instance method, or a static method (I believe there are few more in the JVM bytecode).What won't generate a new anonymous class is this sort of lambda stream.filter(i->"foo".equals(i));
That will generate a new method on the parent class which ultimately gets called. Since nothing is captured it can be directly called without a new instance being created. |
|
Never bothered to actually look into the generated bytecodes.