JShell 是Java 9中引入的第一个REPL工具。我们可以使用JShell工具在命令行提示符下执行简单的代码片段。我们可以通过键入“ jshell ”命令来启动JShell会话,通过键入“ / exit ”命令来停止会话,并通过使用“ / help ”命令来搜索特定命令。
的“/重载”命令可以用于重新执行在JShell所有现有片段。我们还可以使用“ / reset ”命令从JShell会话中删除所有先前的代码。
在下面的代码段中,我们创建了一组代码段。
jshell> 2+10
$1 ==> 12
jshell> String s = "Nhooo"
s ==> "Nhooo"
jshell> System.out.println("Nhooo")
Nhooo
jshell> int num1 = 25
num1 ==> 25
jshell> /1
2+10
$5 ==> 12
jshell> /2
String s = "Nhooo";
s ==> "Nhooo"
jshell> /3
System.out.println("Nhooo")
Nhooo
jshell> /4
int num1 = 25;
num1 ==> 25
我n个下面的代码片段中,我们可以将“ /重装”命令。Jshell工具会重新执行所有现有的代码片段并进行打印。
jshell> /reload
| Restarting and restoring state.
-: 2+10
-: String s = "Nhooo";
-: System.out.println("Nhooo")
Nhooo
-: int num1 = 25;
-: 2+10
-: String s = "Nhooo";
-: System.out.println("Nhooo")
Nhooo
-: int num1 = 25;
-: int num1 = 25;
在下面的代码片段中,我们可以应用“ / reset ”命令从JShell会话中删除所有先前的代码,并输出“ Reset State ”。
jshell> /reset
| Resetting state.
jshell>