Jedi Lambda Join Java Challenge

There are many concepts involved in Java Challenges! In essence, we will continue exploring lambdas and the Function interface the most.

However, we also have static methods introduced in Java 8 and the private method in interfaces introduced in Java 9. In the invocation of the interface methods, we are using anonymous inner classes too!

Are you ready for the next Java Challenge? Less introduction, more action, try out this Java Challenge, and master Java concepts by having fun!

It's time to improve your Java skills with the Join Jedi Lambda...

Join Jedi Lambda Java Challenge

By analyzing the following code, what do you think will happen when running it?

import java.util.function.Function;

public class JediLambdaJoinChallenge {

    interface Jedi {
        String MASTER = "Yoda";

        default String attack() {
            return jump(jedi ->
                    String.join(jedi, useSaber(), useForce()));
        }

        private String jump(Function<String, String> function) {
            return function.apply("Luke");
        }

        private static String useSaber() {
            return "S";
        }

        private String useForce() {
            return "F";
        }

    }

    public static void main(String... starWars) {
        System.out.println(new Jedi() {
            public String useForce() {
                return "X";
            }
        }.attack() + Jedi.useSaber() + Jedi.MASTER);
    }

}

A) SLukeFSYoda
B) Compilation error at line 28
C) LukeXYoda
D) LukeSFSYoda

Explanation:

Let's analyse this code:

System.out.println(new Jedi() { 
        public String useForce() { 
            return "X"; 
        } 
    }.attack() + Jedi.useSaber() + Jedi.MASTER);

Note that the useForce method is being overridden inside an anonymous inner class. An anonymous inner class is a class that doesn't have a name implementing in our case the Jedi interface. Then we invoke the attack method.

default String attack() { 
    return jump(jedi -> String.join(jedi, useSaber(), useForce())); 
}

There is a trick here in the join method. The first argument of the join method is a delimiter. As we are returning Luke in this code:

private String jump(Function function) { 
    return function.apply("Luke");
}

Luke will be the delimiter, and the useSaber, useForce methods are going to simply return a String.

So the first part of the String will be: SLukeF. Then the Jedi.useSaber() method will be invoked, and note that this method is being invoked in a static way. So the method we overrode won't be invoked, this method will return S.

Finally, the constant Jedi.MASTER will be printed with the final input, which is...

FULL Video Explanation below... but try out the challenge before watching the video.

That's it challenger, keep solving the Java Challenges to take your Java skills to another level!

By understanding the main Java features, you can create high-quality code where bugs have a hard time hiding.

Topics:

Don’t Forget to Share This Post!

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.

Related Articles

Subscribe to foojay updates:

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