3

What is an interactive programming environment? Key word interaction , in such a programming environment, every time you enter a line of code, the environment will give you a feedback, which is an interactive programming environment. This programming environment is not very suitable for the complexity requirements of engineering, but it is still very useful in some scenarios such as quick verification and simple calculation. In fact, interactive programming environments have been around for a long time in other high-level languages, and Java did not officially launch such tools until Java 9.

Let's learn together, the interactive programming environment JShell in Java.

JShell Quick Start

This video: 7 minutes to learn how to use jshell, Java's interactive programming environment

Start JShell

Open the terminal and execute the command: jshell , the execution effect is as follows:

 ➜  ~ jshell
|  欢迎使用 JShell -- 版本 9
|  要大致了解该版本, 请键入: /help intro

jshell>

perform calculations

In jshell, you can quickly perform calculation operations and obtain results, such as this:

 jshell> 1+2
$1 ==> 3

define variable

Variables and functions can also be defined in jshell. For example, you can define variables and perform calculations like this:

 jshell> int a=1, b=2;
a ==> 1
b ==> 2

jshell> int c = a + b
c ==> 3

define function

Functions can also be used to encapsulate operations in jshell. For example, the following is an example of defining a sum function and calling it:

 jshell> int sum(int a, int b){
   ...>     return a + b;
   ...> }
|  已创建 方法 sum(int,int)

jshell> int c = sum(1, 2)
c ==> 3

define class

In jshell, it is also possible to define some simple classes, such as:

 jshell> public class Calculate {
   ...>
   ...>     public int a;
   ...>     public int b;
   ...>
   ...>     public int sum() {
   ...>         return a + b;
   ...>     }
   ...> }
|  已创建 类 Calculate

jshell> Calculate c = new Calculate();
c ==> Calculate@4fca772d

jshell> c.a = 1
$9 ==> 1

jshell> c.b = 2
$10 ==> 2

jshell> c.sum()
$11 ==> 3

Of course, this kind of scenario is actually more complicated. In fact, it will be simpler and more practical if it is implemented with the new record in Java 16 .

This article is included in the "New Java Features Column" that I am serializing. This series should be written in the form of e-books. If you want to immerse yourself in reading and learning, you can visit the Web version: https://www.didispace.com/java -features/

https://www.didispace.com/java-features/

View command: /help

Regarding the common commands of jshell, we can view it through /help

 jshell> /help
|  键入 Java 语言表达式, 语句或声明。
|  或者键入以下命令之一:
|  /list [<名称或 id>|-all|-start]
|      列出您键入的源
|  /edit <名称或 id>
|      编辑源条目
|  /drop <名称或 id>
|      删除源条目
|  /save [-all|-history|-start] <文件>
|      将片段源保存到文件
|  /open <file>
|      打开文件作为源输入
|  /vars [<名称或 id>|-all|-start]
|      列出已声明变量及其值
|  /methods [<名称或 id>|-all|-start]
|      列出已声明方法及其签名
|  /types [<名称或 id>|-all|-start]
|      列出类型声明
|  /imports
|      列出导入的项
|  /exit [<integer-expression-snippet>]
|      退出 jshell 工具
|  /env [-class-path <路径>] [-module-path <路径>] [-add-modules <模块>] ...
|      查看或更改评估上下文
|  /reset [-class-path <路径>] [-module-path <路径>] [-add-modules <模块>]...
|      重置 jshell 工具
|  /reload [-restore] [-quiet] [-class-path <路径>] [-module-path <路径>]...
|      重置和重放相关历史记录 -- 当前历史记录或上一个历史记录 (-restore)
|  /history [-all]
|      您键入的内容的历史记录
|  /help [<command>|<subject>]
|      获取有关使用 jshell 工具的信息
|  /set editor|start|feedback|mode|prompt|truncation|format ...
|      设置配置信息
|  /? [<command>|<subject>]
|      获取有关使用 jshell 工具的信息
|  /!
|      重新运行上一个片段 -- 请参阅 /help rerun
|  /<id>
|      按 ID 或 ID 范围重新运行片段 -- 参见 /help rerun
|  /-<n>
|      重新运行以前的第 n 个片段 -- 请参阅 /help rerun
|
|  有关详细信息, 请键入 '/help', 后跟
|  命令或主题的名称。
|  例如 '/help /list' 或 '/help intro'。主题:
|
|  intro
|      jshell 工具的简介
|  keys
|      类似 readline 的输入编辑的说明
|  id
|      片段 ID 以及如何使用它们的说明
|  shortcuts
|      片段和命令输入提示, 信息访问以及
|      自动代码生成的按键说明
|  context
|      /env /reload 和 /reset 的评估上下文选项的说明
|  rerun
|      重新评估以前输入片段的方法的说明

jshell> /help
|  键入 Java 语言表达式, 语句或声明。
|  或者键入以下命令之一:
|  /list [<名称或 id>|-all|-start]
|      列出您键入的源
|  /edit <名称或 id>
|      编辑源条目
|  /drop <名称或 id>
|      删除源条目
|  /save [-all|-history|-start] <文件>
|      将片段源保存到文件
|  /open <file>
|      打开文件作为源输入
|  /vars [<名称或 id>|-all|-start]
|      列出已声明变量及其值
|  /methods [<名称或 id>|-all|-start]
|      列出已声明方法及其签名
|  /types [<名称或 id>|-all|-start]
|      列出类型声明
|  /imports
|      列出导入的项
|  /exit [<integer-expression-snippet>]
|      退出 jshell 工具
|  /env [-class-path <路径>] [-module-path <路径>] [-add-modules <模块>] ...
|      查看或更改评估上下文
|  /reset [-class-path <路径>] [-module-path <路径>] [-add-modules <模块>]...
|      重置 jshell 工具
|  /reload [-restore] [-quiet] [-class-path <路径>] [-module-path <路径>]...
|      重置和重放相关历史记录 -- 当前历史记录或上一个历史记录 (-restore)
|  /history [-all]
|      您键入的内容的历史记录
|  /help [<command>|<subject>]
|      获取有关使用 jshell 工具的信息
|  /set editor|start|feedback|mode|prompt|truncation|format ...
|      设置配置信息
|  /? [<command>|<subject>]
|      获取有关使用 jshell 工具的信息
|  /!
|      重新运行上一个片段 -- 请参阅 /help rerun
|  /<id>
|      按 ID 或 ID 范围重新运行片段 -- 参见 /help rerun
|  /-<n>
|      重新运行以前的第 n 个片段 -- 请参阅 /help rerun
|
|  有关详细信息, 请键入 '/help', 后跟
|  命令或主题的名称。
|  例如 '/help /list' 或 '/help intro'。主题:
|
|  intro
|      jshell 工具的简介
|  keys
|      类似 readline 的输入编辑的说明
|  id
|      片段 ID 以及如何使用它们的说明
|  shortcuts
|      片段和命令输入提示, 信息访问以及
|      自动代码生成的按键说明
|  context
|      /env /reload 和 /reset 的评估上下文选项的说明
|  rerun
|      重新评估以前输入片段的方法的说明

View defined functions: /methods

 jshell> /methods
|    int sum(int,int)

View defined variables: /vars

 jshell> /vars
|    int $1 = 3
|    int b = 2
|    Calculate c = Calculate@4fca772d
|    int $9 = 1
|    int $10 = 2
|    int $11 = 3
|    int $13 = 3

List input source entries: /list

After execution, you can see the list of contents previously entered in jshell :

 jshell> /list

   1 : 1+2
   3 : int a=1, b=2;
   5 : int sum(int a, int b){
           return a + b;
       }
   7 : public class Calculate {

           public int a;
           public int b;

           public int sum() {
               return a + b;
           }
       }
   8 : Calculate c = new Calculate();
   9 : c.a = 1
  10 : c.b = 2
  11 : c.sum()
  12 : Map a = new HashMap();

The number on the left is the entry id, which can be used to edit and delete

Edit source entry: /edit

The above listed entry information through /list 277cec4da14e5bf29b8db095e4492e5e---, try to edit it through /edit , for example:

 jshell> /edit 7

A modification box will pop up:

After the modification is complete, click accept

Remove source entry: /drop

/drop command can be used to delete a source entry, for example, we can use the following command to delete the line with id 12 Map a = new HashMap();

 jshell> /drop 12
|  已删除 变量 a

It can be seen that after execution, a prompt is also given: the a variable of the defined Map type is deleted.

Save file: /save

If you want to save the edited content for execution in other environments, or continue to use it next time, you can save it to a file by /save , for example:

 jshell> /save aaa.txt

Open file: /open

When we change a jshell environment, we can quickly restore the previous execution content by opening the previously saved file, such as:

 jshell> /open aaa.txt

reset jshell: /reset

When we want to write another content, we need to clear the previously executed entry (clear the content of /list), which can be achieved at this time:

 jshell> /reset
|  正在重置状态。

View imported packages: /imports

 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.*

View defined classes: /types

 jshell> /types
|    class Calculate

Exit jshell: /exit

 jshell> /exit
| Goodbye

Well, today's sharing is here! If you encounter difficulties in the learning process? You can join our high-quality technical exchange group , participate in exchanges and discussions, and learn and progress better!

Also, don't walk away, follow me! Continue to update the new Java feature column !

Welcome to my public account: Programmer DD. Learn about cutting-edge industry news for the first time, share in-depth technical dry goods, and obtain high-quality learning resources

程序猿DD
2.2k 声望2.8k 粉丝

作品:《Spring Cloud微服务实战》、SpringForAll社区、OpenWrite、Youtube中文配音