41

There is a new hype with the long awaited lambda expressions in Java 8; every 3 day another article appears with them about how cool they are.

As far as I have understood a lambda expression is nothing more than an anonymous inner class with a single method (at least at the byte-code level). Besides this it comes with another nice feature - type inference but I believe the equivalent of this can be achieved with generics on some level (of course not in such a neat way as with lambda expressions).

Knowing this, are lambda expressions going to bring something more than just a syntactic sugaring in Java? Can I create more powerful and flexible classes or other object-oriented constructs with lambda expressions that aren't possible to be built with current language features?

Random42
  • 10,520
  • 10
  • 52
  • 65

5 Answers5

48

tl;dr: while it's mostly syntactic sugar, that nicer syntax makes lots of things practical that used to end in endless, unreadable lines of braces and parentheses.

Well, it's actually the other way around as lambdas are much older than Java. Anonymous inner classes with a single method are (were) the closest Java came to lambdas. It's an approximation that was "good enough" for some time, but has a very nasty syntax.

On the surface, Java 8 lambdas seem to be not much more than syntactic sugar, but when you look below the surface, you see tons of interesting abstractions. For example the JVM spec treats a lambda quite differently from a "true" object, and while you can handle them as if they where objects, the JVM is not required to implement them as such.

But while all that technical trickery is interesting and relevant (since it allows future optimizations in the JVM!), the real benefit is "just" the syntactic sugar part.

What's easier to read:

myCollection.map(new Mapper<String,String>() {
  public String map(String input) {
    return new StringBuilder(input).reverse().toString();
  }
});

or:

myCollection.map(element -> new StringBuilder(element).reverse().toString());

or (using a method handle instead of a lambda):

myCollection.map(String::toUpperCase);

The fact that you can finally express in a concise way which would previously be 5 lines of code (of which 3 are utterly boring) brings a real change of what is practical (but not of what is possible, granted).

Joachim Sauer
  • 11,016
14

For Java, yes, it's nothing more than a better way of creating an anonymous inner class. This is because of the fundamental decision in java that every bit of byte code has to live within a specific class, which cannot be changed now after decades of legacy code to consider.

However, that is not what lambda expressions really are about. In formalisms where they are native concepts rather than a jump-on-the-bandwagon contortion, lambdas are fundamental building blocks; both their syntax and people's attitude towards them are very different. The example of an anonymous recursive function created purely out of lambdas in Structure and Interpretation of Computer Programs is capable of changing your entire conception of computation. (However, I'm pretty certain that way of learning to program is just to esoteric ever to become a mainstream success story.)

Kilian Foth
  • 110,899
2

Joachim Sauer already did a good job answering you question but only hinted at something I regard as important. Since Lambdas are no classes they are also not compiled as such. All anonymous inner classes result in the creation of a .class file which in turn has to be loaded by the ClassLoader. So using Lambdas instead does not only make your code more beautiful, it also reduces the size of your compiled code, the memory footprint of your ClassLoader, and the time it takes to transfer the bits from your hard drive.

1

Yes, it is just a syntactic sugar, in the sense that anywhere you write a lambda, you can re-write that expression as an anonymous inner class expression with one method, where the class implements the functional interface inferred for the lambda's context, and it would be exactly equivalent semantically. And you can do this by simply replacing the lambda expression with the anonymous class expression, without changing any other expression or line of code.

user102008
  • 508
  • 2
  • 8
-1

No. They are not.

Another way to put it is lambdas are an un-object-oriented, however concise, way to achieve the same thing that an anonymous class or, my preference, inner class would achieve in an object-oriented way.

Guido Anselmi
  • 292
  • 2
  • 8