javascript
五分钟的JShell
這篇文章建立在我的My Java 9頂級(jí)功能文章的基礎(chǔ)上,通過(guò)對(duì)這些功能的深入研究。 在這里,我們向您展示如何在五分鐘內(nèi)學(xué)習(xí)jshell并改善Java 9開(kāi)發(fā)經(jīng)驗(yàn)。
入門(mén)
假設(shè)您已經(jīng)下載并安裝了Java 9,則可以通過(guò)鍵入以下內(nèi)容啟動(dòng)Shell:
jshell或者,如果您要冗長(zhǎng),
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)分配給以$開(kāi)頭的變量–
jshell> "Hello World" $1 ==> "Hello World" | created scratch variable $1 : String這意味著我們以后可以重用該值–
jshell> System.out.println($1); Hello World控制流程
jshell的下一步是使用控制流(如果……則為……)。 我們可以通過(guò)輸入條件,對(duì)每個(gè)新行使用return來(lái)做到這一點(diǎn)–
jshell> if ("Hello World".equals($1)) {...> System.out.println("Woohoo my if condition works");...> } Woohoo my if condition works快速提示是使用TAB進(jìn)行代碼完成
方法
我們可以用與流程控制類(lèi)似的方式聲明一個(gè)方法,然后按
對(duì)于每個(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中定義類(lèi)–
jshell> class HelloWorld {...> public String helloWorldClass() {...> return "helloWorldClass";...> }...> } | created class HelloWorld并分配和訪問(wèn)它們-
/env有用的命令
現(xiàn)在我們有了基本知識(shí),這里有一些快速命令–
| 標(biāo)簽 | 代碼完成 |
| / vars | 當(dāng)前shell中的變量列表 |
| /方法 | 當(dāng)前shell中的方法列表 |
| /清單 | jshell會(huì)話(huà)中的所有代碼段 |
| /進(jìn)口 | 當(dāng)前在外殼進(jìn)口 |
| /方法 | 當(dāng)前shell中的方法列表 |
| /類(lèi)型 | 當(dāng)前的類(lèi)在外殼中定義,在上述情況下,我們將看到“類(lèi)HelloWorld” |
| /編輯 | 使您可以在編輯器中編輯會(huì)話(huà)(默認(rèn)為JEditPad) |
| /出口 | 閉門(mén)會(huì)議 |
翻譯自: https://www.javacodegeeks.com/2017/10/jshell-five-minutes.html
總結(jié)
以上是生活随笔為你收集整理的五分钟的JShell的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: pr音频拉大快捷键(pr音频放大快捷键)
- 下一篇: 什么是JavaServer Faces(