How to implement a lambda expression in JShell in Java 9?

JavaObject Oriented ProgrammingProgramming

JShell is a Java's first REPL and command-line tool that provides interactive use of Java programming language elements. We can test the functionality in isolation of a class by using this tool. JShell creates a simple and easy programming environment in the command-line that takes input from the user, reads it, and prints the result. A lambda expression is a function that has created without belonging to any class.

In the below example, we can implement a lambda expression in JShell.

C:\Users\User>jshell
| Welcome to JShell -- Version 9.0.4
| For an introduction type: /help intro

jshell> Consumer<String> s = (String s) -> System.out.println(s)
s ==> $Lambda$14/1268066861@3159c4b8


If we can’t remember the method of Consumer interface then type the name of a variable created followed by a dot and press tab. It populates a list of methods that can be called on the Consumer interface.

jshell> s.
accept( andThen( equals( getClass() hashCode()
notify() notifyAll() toString() wait(

jshell> s.accept("Welcome to Tutorialspoint")
Welcome to Tutorialspoint
raja
Published on 16-Mar-2020 09:31:21
Advertisements