0

I saw on a tutorial that lambda is not associated with any object that works with invokedynamic. But on this site and other sites, I saw that it said that a lambda is an object. Now I do not know if this tutorial is wrong or invokedynamic Is related to object creation? Because I do not know the relation between invokedynamic and object creation.

I visited this link but did not get my answer.

AMZ
  • 305
  • 1
  • 5
  • I was under the impression that every lambda was an instance of an object that implements at least one interface, but I have no knowledge of the underlying internals. When you say "object", what exactly do you mean? Since people have multiple definitions of "object" – byxor Jan 18 '22 at 17:56
  • I mean the reference variable that is stored in the heap.(like new Integer()). – AMZ Jan 18 '22 at 18:14
  • In Java or in general in OOP when someone says "object" it means "an instance of a class" period. If people has different definition of an object, they are simply wrong. There's no such thing like *an instance of an object*. Sadly `Object` is also a class but it's not "an object" :-\ – fantaghirocco Jan 18 '22 at 19:04
  • I know what an object is, but he asked what I meant by an object in Lambda. – AMZ Jan 18 '22 at 19:09

1 Answers1

3

There are a couple bits that need untangling here.

A lambda is an object. In particular, it's an instance of a functional interface, which has only one abstract method.

Not every occurrence of a lambda makes a new object.

invokedynamic can, but is not required to, make a new object.

invokedynamic is used to get lambda objects, whether or not they are newly created.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • 1
    This is technically correct, but highly misleading, I don't think it serves as a good answer as a consequence. In two senses: [A] The JVM explicitly states that it either doesn't have identity, or, if it does, that you can't rely on this identity. If you `synchronized(x)` where x is e.g. `Runnable x = () -> foo;`, or you do `==` equality to check something, or use them as keys in a hashmap, that sort of thing - that's all neccessarily a code bug as the thing doesn't have (defined) identity. [B] In practice, JVMs _do not_ make an object, at all, in certain cases. – rzwitserloot Jan 18 '22 at 18:03
  • can you explain "Not every occurrence of a lambda makes a new object"?Is it possible that two lambdas with two different bodies have the same reference? – AMZ Jan 18 '22 at 18:56
  • No, that's not possible. They would have to have the same bodies. – Louis Wasserman Jan 18 '22 at 20:12
  • @aa5 __There is no reference__ (usably, at least). They do not have a meaningful identity. Because java is java, you can of course write `Runnable r = System.out::println; synchronized (r) {}` (treat it as: It is a reference and that reference can be used for stuff), but that code is gobbledygook. The JVM spec says this is meaningless, Another `Runnable f2 = System.out::println` may or may not be the same reference. If you write this code, i.e. if you write code where 'it is a reference' is relevant in the first place, your code is broken in an untestable way (that's bad). So don't. – rzwitserloot Jan 18 '22 at 21:25
  • @aa5 Are you asking because you are thinking about the performance impact of lambdas (oh no! It creates objects! That might get pricey! - that kind of thinking), or more thinking about using the notion that a lambda is a reference for something, such as locking on it, using them as a key in a map, or because you want to write some bytecode directly? – rzwitserloot Jan 18 '22 at 21:26
  • I mean. There is a reference. It's the same as any other Java object. It's just a particularly useless reference. – Louis Wasserman Jan 18 '22 at 23:07
  • @rzwitserloot No, I just want to know the exact difference between a lambda and an anonymous class – AMZ Jan 19 '22 at 08:28
  • @aa5 The _exact_ difference cannot be summarized; just read the JLS. – rzwitserloot Jan 19 '22 at 13:15
  • I recommend you search StackOverflow for the many informative answers about the differences between lambdas and anonymous classes. – Louis Wasserman Jan 19 '22 at 17:45