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.

4
  • 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" Commented Jan 18, 2022 at 17:56
  • I mean the reference variable that is stored in the heap.(like new Integer()). Commented Jan 18, 2022 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" :-\ Commented Jan 18, 2022 at 19:04
  • I know what an object is, but he asked what I meant by an object in Lambda. Commented Jan 18, 2022 at 19:09

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

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.
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?
No, that's not possible. They would have to have the same bodies.
@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.
@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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.