Java9新特性之命令行交互式解釋器JShell全解析
REPL,全稱Read Eval Print Loop,中文是交互式解釋器,其實,就是一種代碼所見即所得的即時編譯器。
Java 9引入了REPL,并將其命令為JShell,這真是我們Java開發(fā)的福音,以后演示代碼的時候再也不用搬著一個IDE到處跑了。
對于我們Java開發(fā)者來說,應(yīng)該是Java 9帶來的最大的個性吧。我們終于可以像Python、Ruby和Node.js那樣在Shell可見即可得的運行一些范例代碼了。
也就是說,使用REPL,我們可以編寫和測試基于Java的邏輯,無需使用javac進行編譯,直接查看計算結(jié)果。
JShell的主要特性
- 交互式編程:JShell提供了一個交互式環(huán)境,允許用戶輸入和執(zhí)行Java代碼片段。用戶可以在JShell會話中輸入代碼,并立即看到結(jié)果。
- 代碼片段執(zhí)行:與完整的Java應(yīng)用程序不同,JShell允許你執(zhí)行單個代碼片段,例如表達式、方法定義或類定義。這些代碼片段不必包含在main方法中,也不必創(chuàng)建一個完整的類結(jié)構(gòu)。
- 自動導(dǎo)入:JShell會自動導(dǎo)入常用的Java類庫,如java.lang和java.util。此外,它還支持通過/import命令手動導(dǎo)入其他包和類。
- 變量和方法保留:在JShell會話中定義的變量和方法會保留在會話中,可以在后續(xù)的輸入中引用它們。
- 方法定義和調(diào)用:你可以在JShell中定義方法,并在后續(xù)的輸入中調(diào)用這些方法。
- 命令和語法高亮:JShell支持一些特定的命令,如
/help(顯示幫助信息)和/exit(退出JShell會話)。它還支持語法高亮,以提高代碼的可讀性。 - 錯誤處理和異常:如果代碼片段包含錯誤或拋出異常,JShell會顯示相應(yīng)的錯誤消息和異常堆棧跟蹤。
JShell的使用
要使用JShell,只需在命令行中輸入jshell并按回車鍵。然后,你就可以開始輸入Java代碼片段并查看結(jié)果了。
$ jshell Feb 26, 2024 2:30:09 PM java.util.prefs.FileSystemPreferences$1 run INFO: Created user preferences directory. | Welcome to JShell -- Version 18.0.1.1 | For an introduction type: /help intro
查詢幫助
正如提示那樣,我們可以輸入/help來獲得一些幫助:
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 | /drop <name or id> | delete a source entry | /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 type declarations | /imports | list the imported items | /exit [<integer-expression-snippet>] | exit the jshell tool | /env [-class-path <path>] [-module-path <path>] [-add-modules <modules>] ... | view or change the evaluation context | /reset [-class-path <path>] [-module-path <path>] [-add-modules <modules>]... | reset the jshell tool | /reload [-restore] [-quiet] [-class-path <path>] [-module-path <path>]... | reset and replay relevant history -- current or previous (-restore) | /history [-all] | history of what you have typed | /help [<command>|<subject>] | get information about using the jshell tool | /set editor|start|feedback|mode|prompt|truncation|format ... | set configuration information | /? [<command>|<subject>] | get information about using the jshell tool | /! | rerun last snippet -- see /help rerun | /<id> | rerun snippets by ID or ID range -- see /help rerun | /-<n> | rerun n-th previous snippet -- see /help rerun | | 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 | keys | a description of readline-like input editing | id | a description of snippet IDs and how use them | shortcuts | a description of keystrokes for snippet and command completion, | information access, and automatic code generation | context | a description of the evaluation context options for /env /reload and /reset | rerun | a description of ways to re-evaluate previously entered snippets
注意:千萬不要省略開頭的反斜杠
/。
如果要查看某個具體的命令的幫助信息,可以輸入/help [command]來獲得。
比如/help intro獲取工具簡介:
jshell> /help intro | | intro | ===== | | The jshell tool allows you to execute Java code, getting immediate results. | You can enter a Java definition (variable, method, class, etc), like: int x = 8 | or a Java expression, like: x + x | or a Java statement or import. | These little chunks of Java code are called 'snippets'. | | There are also the jshell tool commands that allow you to understand and | control what you are doing, like: /list | | For a list of commands: /help
比如/help /list查詢/list命令的使用:
jshell> /help /list | | /list | ===== | | Show the snippets, prefaced with their snippet IDs. | | /list | List the currently active snippets of code that you typed or read with /open | | /list -start | List the evaluated startup snippets | | /list -all | List all snippets including failed, overwritten, dropped, and startup | | /list <name> | List snippets with the specified name (preference for active snippets) | | /list <id> | List the snippet with the specified snippet ID. | One or more IDs or ID ranges may used, see '/help id'
查看JShell默認導(dǎo)入哪些包
我們可以使用/imports命令查看JShell默認導(dǎo)入了哪些包:
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 import命令導(dǎo)入某個包或文件
我們可以使用import命令導(dǎo)入某個類或包,就像我們代碼中使用的那樣:
jshell> import java.time.* jshell> /import | 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.* | import java.time.*
導(dǎo)入外部jar
我們可以使用/env命令將第三方的jar添加到class-path中,這樣就可以使用import命令導(dǎo)入第三方的類了:
jshell> /env -class-path /root/hutool-all-5.8.12.jar
| Setting new options and restoring state.
jshell> import cn.hutool.core.util.*
jshell> StrUtil.isBlankIfStr("abc")
$2 ==> false使用JShell進行一些簡單的數(shù)學運算
JShell默認支持一些簡單的數(shù)學運算符,例如:
jshell> 5 * 6 $2 ==> 30 jshell> $2 $2 ==> 30 jshell> 10 * $2 $4 ==> 300 jshell> int a = $4 a ==> 300 jshell> a a ==> 300
可以看到,JShell會將每一此執(zhí)行的結(jié)果保存到一個以$開始的變量中,而后面,我們就可以對這些變量進行引用。
JShell中默認的上下文
可以在JShell中使用我們平時在Java中使用的類和方法來獲取當前的運行上下文。
獲取當前的執(zhí)行線程:
jshell> Thread.currentThread() $7 ==> Thread[main,5,main]
在JShell中定義一些方法
因為內(nèi)部方法和內(nèi)部類的關(guān)系,我們還可以在JShell中定義一個方法。
在一行內(nèi)定義:
jshell> int add(int a, int b) { return a + b;}
| created method add(int,int)
jshell> add(3,4)
$10 ==> 7在多行定義:
jshell> int sub(int a, int b) {
...> return a - b;
...> }
| created method sub(int,int)
jshell> sub(10, 5)
$12 ==> 5退出JShell
如果要退出JShell,只能輸入/exit命令:
jshell> /exit | Goodbye
輸入exit或按下CTRL+C鍵是沒有任何效果的:
jshell> exit | Error: | cannot find symbol | symbol: variable exit | exit | ^--^

到此這篇關(guān)于Java9新特性之命令行交互式解釋器JShell的文章就介紹到這了,更多相關(guān)java解釋器JShell內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決springboot 獲取form-data里的file文件的問題
這篇文章主要介紹了解決springboot 獲取form-data里的file文件的問題的相關(guān)資料,這里提供了詳細的解決步驟,需要的朋友可以參考下2017-07-07
使用SpringBoot+AOP實現(xiàn)可插拔式日志的示例代碼
這篇文章主要介紹了使用SpringBoot+AOP實現(xiàn)可插拔式日志的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07
Spring @ConditionalOnMissingBean 注解的主要作用解析
@ConditionalOnMissingBean是Spring Boot中用于條件化配置的注解,確保只有在指定Bean不存在時才創(chuàng)建,它在自動配置中廣泛應(yīng)用,提供默認配置并允許用戶自定義Bean,本文給大家介紹Spring @ConditionalOnMissingBean注解的作用,感興趣的朋友一起看看吧2026-01-01
springboot 使用poi進行數(shù)據(jù)的導(dǎo)出過程詳解
這篇文章主要介紹了springboot 使用poi進行數(shù)據(jù)的導(dǎo)出過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-09-09
創(chuàng)建springBoot模塊沒有目錄結(jié)構(gòu)的解決方案
2023版IntelliJ IDEA創(chuàng)建模塊時可能出現(xiàn)目錄結(jié)構(gòu)識別錯誤,導(dǎo)致文件顯示異常,解決方法為選擇模塊后點擊確認,重新校準項目結(jié)構(gòu)設(shè)置,確保源代碼和資源目錄正確配置2025-08-08
spring cloud config和bus組件實現(xiàn)自動刷新功能
今天通過本文給大家介紹spring cloud config和bus組件實現(xiàn)自動刷新功能,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-10-10
Spring Cloud 整合Apache-SkyWalking實現(xiàn)鏈路跟蹤的方法
這篇文章主要介紹了Spring Cloud 整合Apache-SkyWalking鏈路跟蹤的示例代碼,代碼簡單易懂,通過圖文相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06

