1

I got into a long discussion on Reddit about whether or not Java lambdas are just sugar for anonymous classes. I contend that lambdas are different from anonymous classes in too many important respects to consider them just sugar for anonymous classes. But, some of the replies have me questioning this.

Can anyone, once and for all, list the exact technical differences between AIC and lambdas in Java? Is it true that once the code is hot and running it's really just an AIC? I thought no originally. AIC creates a new class that must be loaded for each unique use. Not so with lambdas right? Or do lambdas create new classes for each unique use?

Update: I'm especially curious what the actual implementation in various flavors of Java 8 are. I keep reading conflicting things.

Randgalt
  • 2,907
  • 1
  • 17
  • 31
  • 1
    I don't think this is a dup. The other question is more general. I'm looking for the real technical differences between the two. – Randgalt Jul 18 '17 at 14:43
  • As for your specific question "Not so with lambdas right?", consult the [language spec](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.27.4): "These rules are meant to offer flexibility to implementations of the Java programming language, in that:... A new object need not be allocated on every evaluation.". – Andy Turner Jul 18 '17 at 14:51
  • But what does Java 8 do now? I thought I knew but am getting conflicting answers. – Randgalt Jul 18 '17 at 14:53
  • 1
    Which implementation of Java 8? The point in that quote is that the implementor can do it in different ways. – Andy Turner Jul 18 '17 at 14:53
  • Yeah, good point. – Randgalt Jul 18 '17 at 14:53
  • 1
    If you're interested, I've watched this talk a few times which talks about some of the implementation details of java.lang.invoke (presumably on HotSpot): https://www.youtube.com/watch?v=lhGPndh8R3g which is a part of the lambda implementation. – Jorn Vernee Jul 18 '17 at 15:03
  • I closed the question initially and I still think it's a duplicate since things have been discussed quite a lot here... btw anyone (with enough privilege) can re-open it if they think it's appropriate – Eugene Jul 19 '17 at 06:05
  • The linked Q&As answer quite a lot of these questions, if not even all of them. If you say, you read conflicting things, you should include these contradicting statements, together with a source reference at best. Generally, if you think there are difference to the questions that have already been asked and answered, you should highlight these differences. – Holger Jul 20 '17 at 10:47

0 Answers0