1. 程式人生 > >Java Shell 初探

Java Shell 初探

1.前言

       在一波新技術快速重新整理的過程中,Java一直被嘲諷沒有Shell, 在Java JDK 9版本後終於出現了JShell,基於Java的高度格式化要求,程式碼規範的程度相對於其他型別的語言來說有點恐怖的情況下,很好奇這個Shell的使用簡便性到底能達到多少。所以特此記錄一下JShell 的初步使用。

2.簡介

先看一下官方說明(官方文件位置

Why Use JShell?

Using JShell, you can enter program elements one at a time, immediately see the result, and make adjustments as needed.

Java program development typically involves the following process:

  • Write a complete program.

  • Compile it and fix any errors.

  • Run the program.

  • Figure out what is wrong with it.

  • Edit it.

  • Repeat the process.

JShell helps you try out code and easily explore options as you develop your program. You can test individual statements, try out different variations of a method, and experiment with unfamiliar APIs within the JShell session. JShell doesn’t replace an IDE. As you develop your program, paste code into JShell to try it out, and then paste working code from JShell into your program editor or IDE.

為什麼使用JShell:我認為方便了對小型程式碼的理解、除錯、結果鎖定檢查等基本操作。

使用流程上述內容已經表達:

  1. 寫程式
  2. 編譯並修復BUG
  3. 執行
  4. 尋找問題
  5. 編輯
  6. 重複上述工作

可以說這個Shell 並不是Java IDE 的替代品,更類似於一種工具的使用。

3.使用

3.1:基本環境要求:JDK 9+(測試環境Windows 10)

3.2:進入Shell (確保%JAVA_HOME%\bin已經在環境變數裡)

3.3:windows進入控制檯:JShell        進入了JShell互動狀態

3.4:退出指令:/exit

4.基本語法介紹

你可以完全的把他當成一個類來使用,或者方法體,完全的隨心所欲

宣告變數並定義

String firstStr = "Hello JShell";

使用變數(輸出該字串)

System.out.println(firstStr);

注意:如果直接輸入 也可以看到輸出, 但是並沒有呼叫該變數:

firstStr

但是這種方式沒有任何意義,並不是使用該變數,而是單純的輸出這個變數的資訊(進入SHell 的時候未指定反饋模式 所以 現在看來除了值資訊外 沒有其他資訊,此過程後面會說)。

獲得字串長度

int strLength = firstStr.length();

說到這裡,大家就應該明白瞭如何呼叫方法,就不再贅述了

自動變數:為宣告的變數名直而直接輸入變數值,JShell會自動建立一個變數名來儲存

就像獲取字串長度的那個,不輸入 int strLength = 的話 :

firstStr.length();

自動建立變數 $4來儲存結果。

建立類

其實很簡單 , publi.........如下圖

輸入的時候 支援Tab 補全

呼叫類方法

正常的呼叫就可以了。

直接定義方法

說白了就是不寫類框了

呼叫的時候 和類方法一樣 直接用就可以了

引入類庫檔案:

可以使用 shift + tab i 來實現類庫引入

比如引入Frame:

new Frame然後ctrl + tab 鬆開後按下i 即可選擇引入還是其他

5.基本的東西就這些,然後說一說特別的地方:

1.方法中變數後定義

什麼意思呢,就是說 現在有一個方法求圓的面積,但是由於某些原因 我沒有定義PI的值,而是先寫的方法

如下圖,他會提示你有一個變數沒有定義,所以這個方法目前無法呼叫,直到你聲明瞭這個變數才可以使用這個方法

宣告變數PI:

然後再呼叫方法就可以正常使用了

$6為Shell建立的自動變數

2.擴充套件編輯器:

shell狀態下 輸入/edit 即可

每當你再編輯器中寫一行文字,就會自動更新一次shell,自己實驗一下就好了

3.擴充套件反饋資訊

兩種方式 進入shell的時候加上引數:

JShell -v

或者在Shell模式下輸入:(tab 可以補全)

 /set feedback verbose

verbose 是詳細模式

具體模式如下,四種模式 未加入引數的情況是normal

Setting the Feedback Mode

A feedback mode defines the prompts and feedback that are used in your interaction with JShell. Predefined modes are provided for your convenience. You can create custom modes as needed.

The predefined modes can’t be modified, but they can be used as the base of a custom mode. The predefined modes, in descending order of verbosity are verbosenormalconcise, and silent.

The following table shows the differences in the predefined modes.

Mode Value Snippets Declaration Updates Commands Prompt

verbose

name ==> value (and description)

Yes

Yes

Yes

\njshell>

normal

name ==> value

Yes

No

Yes

\njshell>

concise

name ==> value (only expressions)

No

No

No

jshell>

silent

No

No

No

No

->

4.指令縮減:

當指令可以被縮寫單一識別的時候 可以使用縮減來優化指令長度:

完全等於上面的指令 /se(t) fe(edback) v(erbose)

具體指令集使用/help檢視就可以

5.指令碼

官方說明

A JShell script is a sequence of snippets and JShell commands in a file, one snippet or command per line.

Scripts can be a local file, or one of the following predefined scripts:

Script Name Script Contents

DEFAULT

Includes commonly needed import declarations. This script is used if no other startup script is provided.

PRINTING

Defines JShell methods that redirect to the print, println, and printf methods in PrintStream.

JAVASE

Imports the core Java SE API defined by the java.se module, which causes a noticeable delay in starting JShell due to the number of packages.

這部分大家自己檢視吧 沒有什麼特別的說明。

鄙人喜歡Java, 如果你也想一起交流歡迎入群:653460549