Home  >  Article  >  Java  >  How to implement lambda expression in JShell in Java 9?

How to implement lambda expression in JShell in Java 9?

WBOY
WBOYforward
2023-08-20 18:29:021399browse

如何在Java 9的JShell中实现lambda表达式?

JShell 是Java的第一个REPL 命令行工具,提供对Java编程语言元素的交互使用。我们可以使用这个工具在类的隔离环境中测试功能。 JShell 命令行中创建了一个简单而易用的编程环境,它接收用户的输入,读取并打印结果。 lambda表达式是一种不属于任何类的创建的函数。

在下面的示例中,我们可以在JShell中实现一个lambda表达式。

<strong>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</strong>

如果我们记不住Consumer 接口的方法,那么输入一个已创建的变量的名称,然后加上并按下Tab键。它会显示可以在Consumer 接口上调用的方法列表。

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

jshell> s.accept("Welcome to Tutorialspoint")
Welcome to Tutorialspoint</strong>

The above is the detailed content of How to implement lambda expression in JShell in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete