最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java9新特性之命令行交互式解釋器JShell全解析

 更新時(shí)間:2026年01月24日 09:36:18   作者:morris131  
JShell是Java 9引入的交互式解釋器,允許開(kāi)發(fā)者在命令行中即時(shí)編譯和運(yùn)行Java代碼片段,無(wú)需編寫(xiě)完整的類(lèi)結(jié)構(gòu),它支持自動(dòng)導(dǎo)入、變量和方法保留、錯(cuò)誤處理等功能,本文給大家介紹Java9新特性之命令行交互式解釋器JShell,感興趣的朋友跟隨小編一起看看吧

REPL,全稱(chēng)Read Eval Print Loop,中文是交互式解釋器,其實(shí),就是一種代碼所見(jiàn)即所得的即時(shí)編譯器。

Java 9引入了REPL,并將其命令為JShell,這真是我們Java開(kāi)發(fā)的福音,以后演示代碼的時(shí)候再也不用搬著一個(gè)IDE到處跑了。

對(duì)于我們Java開(kāi)發(fā)者來(lái)說(shuō),應(yīng)該是Java 9帶來(lái)的最大的個(gè)性吧。我們終于可以像Python、Ruby和Node.js那樣在Shell可見(jiàn)即可得的運(yùn)行一些范例代碼了。

也就是說(shuō),使用REPL,我們可以編寫(xiě)和測(cè)試基于Java的邏輯,無(wú)需使用javac進(jìn)行編譯,直接查看計(jì)算結(jié)果。

JShell的主要特性

  • 交互式編程:JShell提供了一個(gè)交互式環(huán)境,允許用戶(hù)輸入和執(zhí)行Java代碼片段。用戶(hù)可以在JShell會(huì)話(huà)中輸入代碼,并立即看到結(jié)果。
  • 代碼片段執(zhí)行:與完整的Java應(yīng)用程序不同,JShell允許你執(zhí)行單個(gè)代碼片段,例如表達(dá)式、方法定義或類(lèi)定義。這些代碼片段不必包含在main方法中,也不必創(chuàng)建一個(gè)完整的類(lèi)結(jié)構(gòu)。
  • 自動(dòng)導(dǎo)入:JShell會(huì)自動(dòng)導(dǎo)入常用的Java類(lèi)庫(kù),如java.lang和java.util。此外,它還支持通過(guò)/import命令手動(dòng)導(dǎo)入其他包和類(lèi)。
  • 變量和方法保留:在JShell會(huì)話(huà)中定義的變量和方法會(huì)保留在會(huì)話(huà)中,可以在后續(xù)的輸入中引用它們。
  • 方法定義和調(diào)用:你可以在JShell中定義方法,并在后續(xù)的輸入中調(diào)用這些方法。
  • 命令和語(yǔ)法高亮:JShell支持一些特定的命令,如/help(顯示幫助信息)和/exit(退出JShell會(huì)話(huà))。它還支持語(yǔ)法高亮,以提高代碼的可讀性。
  • 錯(cuò)誤處理和異常:如果代碼片段包含錯(cuò)誤或拋出異常,JShell會(huì)顯示相應(yīng)的錯(cuò)誤消息和異常堆棧跟蹤。

JShell的使用

要使用JShell,只需在命令行中輸入jshell并按回車(chē)鍵。然后,你就可以開(kāi)始輸入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

查詢(xún)幫助

正如提示那樣,我們可以輸入/help來(lái)獲得一些幫助:

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

注意:千萬(wàn)不要省略開(kāi)頭的反斜杠/。

如果要查看某個(gè)具體的命令的幫助信息,可以輸入/help [command]來(lái)獲得。

比如/help intro獲取工具簡(jiǎn)介:

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查詢(xún)/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默認(rèn)導(dǎo)入哪些包

我們可以使用/imports命令查看JShell默認(rèn)導(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)入某個(gè)包或文件

我們可以使用import命令導(dǎo)入某個(gè)類(lèi)或包,就像我們代碼中使用的那樣:

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)入第三方的類(lèi)了:

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進(jìn)行一些簡(jiǎn)單的數(shù)學(xué)運(yùn)算

JShell默認(rèn)支持一些簡(jiǎn)單的數(shù)學(xué)運(yùn)算符,例如:

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會(huì)將每一此執(zhí)行的結(jié)果保存到一個(gè)以$開(kāi)始的變量中,而后面,我們就可以對(duì)這些變量進(jìn)行引用。

JShell中默認(rèn)的上下文

可以在JShell中使用我們平時(shí)在Java中使用的類(lèi)和方法來(lái)獲取當(dāng)前的運(yùn)行上下文。

獲取當(dāng)前的執(zhí)行線(xiàn)程:

jshell> Thread.currentThread()
$7 ==> Thread[main,5,main]

在JShell中定義一些方法

因?yàn)閮?nèi)部方法和內(nèi)部類(lèi)的關(guān)系,我們還可以在JShell中定義一個(gè)方法。

在一行內(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鍵是沒(méi)有任何效果的:

jshell> exit
|  Error:
|  cannot find symbol
|    symbol:   variable exit
|  exit
|  ^--^

到此這篇關(guān)于Java9新特性之命令行交互式解釋器JShell的文章就介紹到這了,更多相關(guān)java解釋器JShell內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解決springboot 獲取form-data里的file文件的問(wèn)題

    解決springboot 獲取form-data里的file文件的問(wèn)題

    這篇文章主要介紹了解決springboot 獲取form-data里的file文件的問(wèn)題的相關(guān)資料,這里提供了詳細(xì)的解決步驟,需要的朋友可以參考下
    2017-07-07
  • 使用SpringBoot+AOP實(shí)現(xiàn)可插拔式日志的示例代碼

    使用SpringBoot+AOP實(shí)現(xiàn)可插拔式日志的示例代碼

    這篇文章主要介紹了使用SpringBoot+AOP實(shí)現(xiàn)可插拔式日志的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Spring @ConditionalOnMissingBean 注解的主要作用解析

    Spring @ConditionalOnMissingBean 注解的主要作用解析

    @ConditionalOnMissingBean是Spring Boot中用于條件化配置的注解,確保只有在指定Bean不存在時(shí)才創(chuàng)建,它在自動(dòng)配置中廣泛應(yīng)用,提供默認(rèn)配置并允許用戶(hù)自定義Bean,本文給大家介紹Spring @ConditionalOnMissingBean注解的作用,感興趣的朋友一起看看吧
    2026-01-01
  • struts2獲取服務(wù)器臨時(shí)目錄的方法

    struts2獲取服務(wù)器臨時(shí)目錄的方法

    這篇文章主要為大家詳細(xì)介紹了struts2獲取服務(wù)器臨時(shí)目錄的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Java中八大包裝類(lèi)舉例詳解(通俗易懂)

    Java中八大包裝類(lèi)舉例詳解(通俗易懂)

    這篇文章主要介紹了Java中的包裝類(lèi),包括它們的作用、特點(diǎn)、用途以及如何進(jìn)行裝箱和拆箱,包裝類(lèi)還提供了許多實(shí)用方法,如轉(zhuǎn)換、獲取基本類(lèi)型值、比較和類(lèi)型檢測(cè),文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-02-02
  • springboot 使用poi進(jìn)行數(shù)據(jù)的導(dǎo)出過(guò)程詳解

    springboot 使用poi進(jìn)行數(shù)據(jù)的導(dǎo)出過(guò)程詳解

    這篇文章主要介紹了springboot 使用poi進(jìn)行數(shù)據(jù)的導(dǎo)出過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • 手把手教你搞懂冒泡排序和選擇排序

    手把手教你搞懂冒泡排序和選擇排序

    這篇文章主要介紹了java數(shù)組算法例題代碼詳解(冒泡排序,選擇排序),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-07-07
  • 創(chuàng)建springBoot模塊沒(méi)有目錄結(jié)構(gòu)的解決方案

    創(chuàng)建springBoot模塊沒(méi)有目錄結(jié)構(gòu)的解決方案

    2023版IntelliJ IDEA創(chuàng)建模塊時(shí)可能出現(xiàn)目錄結(jié)構(gòu)識(shí)別錯(cuò)誤,導(dǎo)致文件顯示異常,解決方法為選擇模塊后點(diǎn)擊確認(rèn),重新校準(zhǔn)項(xiàng)目結(jié)構(gòu)設(shè)置,確保源代碼和資源目錄正確配置
    2025-08-08
  • spring cloud config和bus組件實(shí)現(xiàn)自動(dòng)刷新功能

    spring cloud config和bus組件實(shí)現(xiàn)自動(dòng)刷新功能

    今天通過(guò)本文給大家介紹spring cloud config和bus組件實(shí)現(xiàn)自動(dòng)刷新功能,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-10-10
  • Spring Cloud 整合Apache-SkyWalking實(shí)現(xiàn)鏈路跟蹤的方法

    Spring Cloud 整合Apache-SkyWalking實(shí)現(xiàn)鏈路跟蹤的方法

    這篇文章主要介紹了Spring Cloud 整合Apache-SkyWalking鏈路跟蹤的示例代碼,代碼簡(jiǎn)單易懂,通過(guò)圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06

最新評(píng)論

神农架林区| 扶余县| 黑山县| 清涧县| 新沂市| 元谋县| 郎溪县| 鄂伦春自治旗| 沾化县| 五莲县| 土默特右旗| 湘潭市| 曲麻莱县| 西盟| 宁明县| 惠安县| 南郑县| 昌图县| 京山县| 雅江县| 佛学| 灵山县| 云梦县| 高淳县| 凌源市| 两当县| 靖远县| 荣成市| 民勤县| 平和县| 江孜县| 从化市| 宜昌市| 嘉荫县| 喜德县| 绥中县| 高青县| 咸丰县| 房山区| 泗洪县| 永定县|