Friends of OpenJDK Today

Method Reference VS Lambda Java Challenge

June 29, 2021

Author(s)

  • Rafael del Nero

    Java Champion, a passionate and creative developer who possesses strong technical knowledge and delivers solid systems focused on Spring and Java EE specifications for cloud applications. More than 10 years' ... Learn more

Do you know what the differences are between method references and lambdas?

In this Java Challenge, we will explore how lambdas and method references behave so that you can really understand how they work!

Now that you know the main context, it’s time for the Java Challenge!

It's time to improve your Java skills with this Method Reference VS Lambda Challenge.

Method Reference VS Lambda Challenge

What will happen when we run the following main method?

public class MethodReferenceVSLambdaChallenge {

    public static void main(String... doYourBest) {
        Runnable universeImpactRunnable = () -> new ChuckNorris().roundHouseKick();
        Runnable galaxyImpactRunnable = new ChuckNorris()::roundHouseKick;

        System.out.print("The galaxy is finished = ");

        universeImpactRunnable.run();
        universeImpactRunnable.run();

        galaxyImpactRunnable.run();
        galaxyImpactRunnable.run();
    }

    static class ChuckNorris {
        private static int numberOfKicks;
        private int galaxyDamage;

        ChuckNorris() {
            this.galaxyDamage = numberOfKicks++;
        }

        void roundHouseKick() {
            System.out.print(this.galaxyDamage);
        }
    }

}

A) The galaxy is finished = 1234
B) The galaxy is finished = 0123
C) The galaxy is finished = 0100
D) The galaxy is finished = 1200

Explanation:

Now that you know the main context, it’s time for the Java Challenge!

Let's analyze the code:

Runnable universeImpactRunnable = () -> new ChuckNorris().roundHouseKick();
Runnable galaxyImpactRunnable = new ChuckNorris()::roundHouseKick;

There is a crucial difference between lambda and method reference. Lambdas are lazy and they will invoke the class constructor only when the method is invoked. On the other hand, with method reference, the constructor will be invoked right away only where the method reference is assigned, not on the method invocation.

So, at this line the constructor is not invoked:

Runnable universeImpactRunnable = () -> new ChuckNorris().roundHouseKick();

And, at this line the constructor is invoked:

Runnable galaxyImpactRunnable = new ChuckNorris()::roundHouseKick;

With that in mind, let's analyze the invoked methods:

universeImpactRunnable.run();
universeImpactRunnable.run();

With lambdas, every time the run method is invoked, the constructor will be invoked, which means that the numberOfKicks variable will be increased. So the value from galaxyDamage will be:

12

Then when we invoke those methods:

galaxyImpactRunnable.run();
galaxyImpactRunnable.run();

The value will be 0 because remember that the constructor will be only invoked one and it was already invoked at the moment of the method reference declaration.

By knowing that, and also remembering that the post ++ operator increases a value after the line being processed. Then the values will be:

00

The final result will be then... what do you think?

If you want to watch the video explanation, check it out, but I recommend trying out the Java Challenge first!

That’s it challenger, rock on! Keep taking action and relentlessly break your limits! Don’t hesitate to leave a comment with a question if anything is not clear!

If you want to see the original post, go to the following link:

https://javachallengers.com/method-reference-vs-lambda-java-challenge/

Topics:

Author(s)

  • Rafael del Nero

    Java Champion, a passionate and creative developer who possesses strong technical knowledge and delivers solid systems focused on Spring and Java EE specifications for cloud applications. More than 10 years' ... Learn more

Comments (0)

Your email address will not be published. Required fields are marked *

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard