Java 教程
REPL(Read Eval Print Loop)意為交互式的編程環(huán)境。
JShell 是 Java 9 新增的一個交互式的編程環(huán)境工具。它允許你無需使用類或者方法包裝來執(zhí)行 Java 語句。它與 Python 的解釋器類似,可以直接 輸入表達式并查看其執(zhí)行結果。
執(zhí)行 JSHELL
$ jshell | Welcome to JShell -- Version 9-ea | For an introduction type: /help intro jshell>
查看 JShell 命令
輸入 /help 可以查看 JShell相關的命令:
jshell> /help | Type a Java language expression, statement, or declaration. | Or type one of the following commands: | /list [<name or id>|-all|-start] | list the source you have typed | /edit <name or id> | edit a source entry referenced by name or id | /drop <name or id> | delete a source entry referenced by name or id | /save [-all|-history|-start] <file> | Save snippet source to a file. | /open <file> | open a file as source input | /vars [<name or id>|-all|-start] | list the declared variables and their values | /methods [<name or id>|-all|-start] | list the declared methods and their signatures | /types [<name or id>|-all|-start] | list the declared types | /imports | list the imported items
執(zhí)行 JShell 命令
/imports 命令用于查看已導入的包:
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.* jshell>
JShell 執(zhí)行計算
以下實例執(zhí)行 JShell 簡單計算:
jshell> 3+1 $1 ==> 4 jshell> 13%7 $2 ==> 6 jshell> $2 $2 ==> 6 jshell>
JShell 創(chuàng)建與使用函數(shù)
創(chuàng)建一個函數(shù) doubled() ,將傳入的整型參數(shù)乘于 2 后返回:
jshell> int doubled(int i){ return i*2;} | created method doubled(int) jshell> doubled(6) $3 ==> 12 jshell>
退出 JShell
輸入 /exit 命令退出 jshell:
jshell> /exit | Goodbye其他擴展