2

EDIT: how is this question different from Java8 Lambdas vs Anonymous classes: The answers provided in the previous question are regarding of how they work in a higher level. I already understand the syntax benefits. My question here is regarding the facts, that anonymous classes are ultimately obejct isntances, and lambdas are not objects. I wan't to understand the differences and implications under the hood of this mechanism and how does it work.

So, the first notable difference is naturally in the syntax. The second is that as far as I udnerstand, lambdas may only be implemented as @FunctionalInterfaces which requires that the interface we are implementing have only one method, which allows better inference.

Premise: as I understand a functional interface has a "non-object method" (source here)

My question is, exactly how do they differ "under the hood", by this I mean, if how are they difference in a sense that anonymous classes are instanciated, they ultimately objects, but lambdas.. are not? This is what I don't fully understand, are lambdas not really instances of objects? What are the real differences of lambdas not being objects in comparison to an anonymous class?

Community
  • 1
  • 1
Jorch914
  • 3,465
  • 2
  • 16
  • 21
  • Each lambda is an object whose (unspecified) class implements the appropriate functional interface. For the most part, they are a more convenient syntax for simple anonymous inner classes. – John Bollinger Oct 02 '15 at 16:18
  • 3
    http://stackoverflow.com/questions/27524445/does-a-lambda-expression-create-an-object-on-the-heap-every-time-its-executed – Sotirios Delimanolis Oct 02 '15 at 16:20
  • 2
    You are mixing up two entirely different things. Lambda expressions are not objects on a conceptual, *very high* level. To understand that, asking about what happens *under the hood*, is the wrong question. Under the hood, classes are generated and instances of these classes created. But these objects are not lambda expressions. – Holger Oct 02 '15 at 16:28
  • 1
    Why do you assume lambdas aren't objects? – Louis Wasserman Oct 02 '15 at 17:30
  • @LouisWasserman in the premise in my question there is a link to "Translation of Lambda Expressions" from which I understand that lambdas are not instanciated objects. That is what I'm trying to understand. – Jorch914 Oct 02 '15 at 20:30
  • 1
    I'm not sure why you think that document implies lambdas are not instantiated objects. That document pretty much indicates that the JRE is free to pick its own way to generate lambdas, and mostly deals with the interface from lambdas in source code to the bytecode used to hook into the JRE. – Louis Wasserman Oct 02 '15 at 20:35
  • 1
    The main low-level difference is that lambda runtime representation is *implementation dependent*. Currently new anonymous class is generated in runtime for every lambda and if the lambda captures no context, only one object is generated and reused. But unlike anonymous classes this may change in future. For example, single class (accepting method handle and context) might be generated per one functional interface, so, for example, every `Runnable` lambda would be actually the same class. – Tagir Valeev Oct 03 '15 at 01:27
  • 4
    @LouisWasserman I think the question is more fair than you give it credit for; I think the disconnect may be over the meaning of "object". If you are talking java.lang.Object, then yes, *currently*, lambda expressions always evaluate to an instance of Object (though not necessarily a *new* Object.) However, if you interpret "object" more abstractly, as in "object-oriented programming", then the OP's assumption is more valid. We worked hard in JLS 8 to not foreclose on a future where lambda expressions are values, not objects. – Brian Goetz Oct 03 '15 at 16:18
  • @BrianGoetz Yes! that's exactly what I'm trying to say! I just couldn't find the words to express it(due to my lack of knowledge). Thanks for the insight, this is what I'm actually trying to understand. Could you please expand this comment a little bit more and make it an answer? – Jorch914 Oct 04 '15 at 22:01
  • @Jorch914: this is exactly what I said [before](http://stackoverflow.com/questions/32911450/how-do-java-8-lambdas-differ-from-anonymous-classes-as-objects-under-the-hood#comment53651307_32911450), lambda expressions are not objects on a conceptual, programming language level, but your question is something about “under the hood”, thus has nothing to do with that point. Even worse, when your question was closed with a pointer to that [discussion](http://stackoverflow.com/questions/22637900/java8-lambdas-vs-anonymous-classes) you insisted on not talking about a higher level. – Holger Oct 05 '15 at 09:41
  • @Holger yes and no, my friend, I do understsand now that I asked the wrong question, I hve been doing some research and I lambdas in general I always find the "the are treated as values, not objects", and then Brian mentioned that they are currently "evaluated" as java.lang.objects, that statement made it more clear to me. To be fair I agree with your previous comment but maybe I didn't understand it well enough. I agree I may have asked the wrong question, but within the comments an answer seems to come up. An invitation to make an answer based from the findings stands up.Regards – Jorch914 Oct 05 '15 at 14:08
  • I’m glad that the comments helped you, but the question is closed which means no new answers can be added to it. And in its current form, reopening is unlikely. – Holger Oct 05 '15 at 17:29

0 Answers0