1

Would appreciate any help on understanding below two concepts in Java 8.

What I know

  1. A lambda is an object without an identity and should not be used as a regular object.
  2. A lambda expression should not be calling methods from Object class like toString, equals, hashCode etc.

What I'd like know more about

  1. What difference does lambda expression have to be called as an object without identity?

  2. Why exactly should methods from Objectclass not be used while using lambda expression?

1
  • 3
    Hey! Could you explain what exactly you are unsure about? This would make the question less reliant on someone explaining the whole thing and more focused on an understanding problem. Commented Jun 14, 2020 at 4:31

2 Answers 2

3

1) A lambda is an object without an identify and should not be used as a regular object.

This is not true. A lambda is a Java object, and it does have an identity1. The problem is that the lifecycle is deliberately left specified to allow Java compilers freedom to optimize the code that evaluates them. Therefore, you cannot know when new lambda objects are going to be created, or existing ones are going to be reused.

JLS 15.27.4 says:

"At run time, evaluation of a lambda expression is similar to evaluation of a class instance creation expression, insofar as normal completion produces a reference to an object. Evaluation of a lambda expression is distinct from execution of the lambda body.

Either a new instance of a class with the properties below is allocated and initialized, or an existing instance of a class with the properties below is referenced. ... "


2) A lambda expression should not be calling methods from Object class like toString, equals, hashCode etc.

As written, that is also not true. A lambda expression may call those methods. However, it is not advisable to rely on those methods to have any specific behavior when you call them on a lambda object

The JLS states:

"The class ... may override methods of the Object class."

In other words, it may ... or may not ... override them. If you rely a particular behavior of these methods, your application is (in theory) non-portable.

Furthermore, since the instantiation semantics are also unspecified, the behavior of Object::equals and Object::hashCode are uncertain.

Finally, it is unspecified whether lambdas are clonable.


1 - Sure, a lambda doesn't have a name: it is anonymous. But name and identity are different concepts.

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

Comments

0

Basically, a lambda is a convenience of doing this:

@FunctionalInterface
interface A {
    void b();
}

void c(A a) {
    ...
}

void main() {
    c(new A() {
        void b() {
            ...
        }
    });
}

I apologize for the less than stellar variable names, but as you can see, A is an interface with one method. c is a method that takes in an A interface. However, instead of creating your own class that implements A, you can create it on the spot. This is what you call an anonymous class, since it doesn't have a name. This is where the quote you have:

A lambda is an object without an identify

comes from. The class doesn't have an identity. The reason it relates to lambdas is because if an interface has only one method, you can use lamdas to simplify it.

void main() {
    c(
        () -> {
            ...
        }
    );
}

This is the exact same as before.

The second part, why lambdas shouldn't use Object's methods, I didn't know before. You should probably have someone else answer this, however my guess is that lambda classes don't look like it extends Object directly, so you can't use it's methods.

1 Comment

Identity and name are different concepts.

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.