Java 9 中的 JShell?


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 intro

jshell>

步驟 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

步驟 3:在 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 ==> 8

jshell> 8-4
$2 ==> 4

jshell> 2*6
$3 ==> 12

jshell> 9%3
$4 ==> 0

jshell> 8/2
$5 ==> 4

步驟 5:要退出 JShell,請鍵入/exit

jshell> /exit
| Goodbye

更新於: 17-2-2020

157 次瀏覽

開啟你的職業生涯

完成課程以獲得認證

開始使用
廣告
© . All rights reserved.