javascript
jshell_五分钟的JShell
jshell
這篇文章建立在我的My Java 9頂級(jí)功能文章的基礎(chǔ)上,通過對(duì)這些功能的深入研究。 在這里,我們向您展示如何在五分鐘內(nèi)學(xué)習(xí)jshell并改善Java 9開發(fā)經(jīng)驗(yàn)。
入門
假設(shè)您已經(jīng)下載并安裝了Java 9,則可以通過鍵入以下內(nèi)容啟動(dòng)Shell:
jshell或者,如果您要詳細(xì)-
C:\jdk9TestGround>jshell -v | Welcome to JShell -- Version 9 | For an introduction type: /help introjshell>變數(shù)
只需鍵入變量,帶或不帶分號(hào)–
jshell> int i = 1; i ==> 1 | created variable i : int未分配的值會(huì)自動(dòng)分配給以$ –開頭的變量
jshell> "Hello World" $1 ==> "Hello World" | created scratch variable $1 : String這意味著我們以后可以重用該值–
jshell> System.out.println($1); Hello World控制流程
jshell的下一步是使用控制流(對(duì)于if,while,…)。 我們可以通過輸入條件,對(duì)每個(gè)新行使用return來做到這一點(diǎn)–
jshell> if ("Hello World".equals($1)) {...> System.out.println("Woohoo my if condition works");...> } Woohoo my if condition works快速提示是使用TAB進(jìn)行代碼完成
方法
我們可以用與流程控制類似的方式聲明一個(gè)方法,然后按
每條新線–
jshell> String helloWorld() {...> return "hello world";...> } | created method helloWorld()然后叫它–
jshell> System.out.println(helloWorld()); hello world我們還可以更改外殼程序中的方法,并使用方法調(diào)用尚未定義的方法-
jshell> String helloWorld() {...> return forwardReferencing();...> } | modified method helloWorld(), however, it cannot be invoked until method forwardReferencing() is declared | update overwrote method helloWorld()現(xiàn)在我們修復(fù)方法–
jshell> String forwardReferencing() {...> return "forwardReferencing";...> } | created method forwardReferencing() | update modified method helloWorld()班級(jí)
我們還可以在jshell中定義類–
jshell> class HelloWorld {...> public String helloWorldClass() {...> return "helloWorldClass";...> }...> } | created class HelloWorld并分配和訪問它們-
/env有用的命令
現(xiàn)在,我們已經(jīng)掌握了一些基本的快速命令–
| 標(biāo)簽 | 代碼完成 |
| / vars | 當(dāng)前shell中的變量列表 |
| /方法 | 當(dāng)前shell中的方法列表 |
| /清單 | jshell會(huì)話中的所有代碼段 |
| /進(jìn)口 | 當(dāng)前在外殼進(jìn)口 |
| /方法 | 當(dāng)前shell中的方法列表 |
| /類型 | 當(dāng)前的類在外殼中定義,在上述情況下,我們將看到“類HelloWorld” |
| /編輯 | 使您可以在編輯器中編輯會(huì)話(默認(rèn)為JEditPad) |
| /出口 | 閉門會(huì)議 |
翻譯自: https://www.javacodegeeks.com/2017/10/jshell-five-minutes.html
jshell
總結(jié)
以上是生活随笔為你收集整理的jshell_五分钟的JShell的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 备案查询网站(备案查首页)
- 下一篇: 迈克尔 杰克逊mv_杰克逊JSON解析错