jshell是 java 9 版本中引入的新概念。它为 java 提供了repl(read-eval-print-loop)能力。通过使用 jshell,我们可以测试基于 java 的逻辑和表达式,而无需编译它。 repl 充当即时反馈循环,对特定语言的生产力有很大影响。
第 1 步:打开命令提示符并输入 jshell。
microsoft windows [version 6.3.9600](c) 2013 microsoft corporation. all rights reserved.c:\users\user>jshell| welcome to jshell -- version 9.0.4| for an introduction type: /help introjshell>
第 2 步:jshell 命令窗口开始运行后,键入 /help(以查看 jshell 命令)。
jshell> /help| type a java language expression, statement, or declaration.| or type one of the following commands:| /list [|-all|-start]| list the source you have typed| /edit| edit a source entry referenced by name or id| /drop| delete a source entry referenced by name or id| /save [-all|-history|-start]| save snippet source to a file.| /open| open a file as source input| /vars [|-all|-start]| list the declared variables and their values| /methods [|-all|-start]| list the declared methods and their signatures| /types [|-all|-start]| list the declared types| /imports| list the imported items| /exit| exit jshell| /env [-class-path ] [-module-path ] [-add-modules <| view or change the evaluation context| /reset [-class-path ] [-module-path ] [-add-modules| reset jshell| /reload [-restore] [-quiet] [-class-path ] [-module-path| reset and replay relevant history -- current or previous (| /history| history of what you have typed| /help [|]| get information about jshell| /set editor|start|feedback|mode|prompt|truncation|format ...| set jshell configuration information| /? [|]| get information about jshell| /!| re-run last snippet| /| re-run snippet by id| /-| re-run n-th previous snippet|| for more information type '/help' followed by the name of a| command or a subject.| for example '/help /list' or '/help intro'.|| subjects:|| intro| an introduction to the jshell tool| shortcuts| a description of keystrokes for snippet and command comple| information access, and automatic code generation| context| the evaluation context options for /env /reload and /reset
第三步:在jshell命令窗口中输入 /imports,获取jshell导入的包。
jshell> /imports| import java.io.*| import java.math.*| import java.net.*| import java.nio.file.*| import java.util.*| import java.util.concurrent.*| import java.util.function.*| import java.util.prefs.*| import java.util.regex.*| import java.util.stream.*
第 4 步:在 jshell 中执行计算(尝试使用 jshell 进行算术计算)
jshell> 3+5$1 ==> 8jshell> 8-4$2 ==> 4jshell> 2*6$3 ==> 12jshell> 9%3$4 ==> 0jshell> 8/2$5 ==> 4
第 5 步:要退出 jshell,请输入 /exit。
jshell> /exit| goodbye
以上就是java 9中的jshell?的详细内容。