13 ms·
Java has "had" anonymous functions for ages with anonymous inner classes, they just sucked as an implementation.
by snuxoll 9y ago
Java has "had" anonymous functions for ages with anonymous inner classes, they just sucked as an implementation.
- jdmichal 9y agoAbsolutely correct. Lambdas are just shorthand for anonymous inner classes which implement an interface with only one method -- aka single abstract method (SAM) types. For instance, you have two functions. One takes `Function<Type, Type>` and the other takes `UnaryOperator<Type>`. Giving the "same" lambda to both functions will result in two anonymous inner types, one implementing both interfaces.
- _asummers 9y agoThey're not exactly shorthand. They use `invoke_dynamic` under the hood, which defers the creation of the lambda til it is used. Conceptually they are, but in practice they're a little different.
- slaymaker1907 9y agoLambdas are also more aggressively optimized. If possible, the compiler will sometimes turn a lambda into a static method.
- masklinn 9y ago> they just sucked as an implementation. They also sucked as an interface (for developers to use), resulting in them only being used when no other option was available (Comparable) or for cute hacks (double-brace initialisation).