02 - using Java Jshell most suitable entry Java Tutorial

JShell

JShell target

Java Shell tool (referred to as: JShell) is a programming language for learning Java interactive tools and Java code to build the prototype. JShell is a Read-Evaluate-Print cycle (REPL), i.e. that they are calculated in the syntax, input statements and expressions, the results are displayed immediately. The JShell tool to run from the command line.

In fact, as early as in JDK9 on the introduction of JShell this interactive tool that allows Java scripting language also like to run, you can start JShell from the console, you can enter an expression directly in the JShell and view the results of its operation. When you need to test a method of operating results, or to quickly expressions are evaluated, jshell are very practical.

In JShell in addition to using the expression, you can also create Java classes and methods, but it also has basic code completion. For example, we write when the test module, reducing the need for excess "public static void main (String [] args)" a.

Why do we need to use JShell

Use JShell tools, we can enter once a program element and see the results immediately and make adjustments as needed. And a complete Java application development step is usually complicated, the general process is as follows:

  1. First, write a complete application.
  2. Compile the application and fix a variety of BUG and optimized tone excellent, making it compile.
  3. Start running the program.
  4. Running on the line and found problems.
  5. Editing application.
  6. Repeat this process until the final delivery.

JShell can help us debug code in the development of programs and easily explore all aspects. Can test a single statement, can also be a different test method body, and debugging are not familiar with the API in JShell Session. But JShell not replace the IDE, JShell only as an aid or convenient tool. In developing the program, can paste the snippet into JShell Session test run, the test passes, then the code fragment JShell pasted back editor or IDE, thereby leading to rapid commissioning, quickly identify problems, such an action would save a lot of development time, in a certain sense, it is to shorten the development cycle of the program, why not do it.

JShell use

JShell introduced in the JDK 9, as already mentioned, will not be repeated here hereby described. To start JShell, enter the command in the command line "jshell" can be. The prerequisite is to install JDK version 9 or later on this machine. For example java-home / jdk-11 / bin, if your path does not contain the "bin" directory, you need to start the tool from the bin directory. The following example shows the commands and responses JShell:

    

C:\Users\HuaZai>jshell

| Welcome JShell - version 11.0.2

| For an overview of this release, type: / help intro

 

jshell>

 

To exit JShell, enter the command "/ exit" to the example reads as follows:

jshell> /exit

| Goodbye

 

C: \ Users \ HuaZai>

JShell code fragment

Java code fragment JShell input and executed immediately, the result is displayed, the operations performed and any errors will occur immediate feedback. JShell start using verbose mode to get more data, enter here a simple statement, declaration of variables, examples of which reads as follows:

C:\Users\HuaZai>jshell -v

| Welcome JShell - version 11.0.2

| For an overview of this release, type: / help intro

 

jshell> int x = 66;

x ==> 66

| Has been created variable x: int

 

jshell>

首先,显示结果。将其读作:变量 x 的值为66,因为目前处于详细模式,所以还会显示所有具体的发生了哪些情况,比如显示已创建变量的名称和类型。

*注意:如果没有输入分号,则JShell会自动将结束分号添加到完整代码段的末尾。

当输入的表达式没有命名变量时,会创建一个临时变量,以便稍后可以引用该值。以下示例显示表达式和方法结果的临时值,示例内容如下:

 

jshell> 128 + 392

$5 ==> 520

| 已创建暂存变量 $5 : int

 

jshell> String quintic(String a){

...> return a + a;

...> }

| 已创建 方法 quintic(String)

 

jshell> quintic("Ts")

$7 ==> "TsTs"

| 已创建暂存变量 $7 : String

 

jshell>

改变定义内容

在调试代码时,我们可能会发现变量、方法或类的定义没有达到预期的目的。可以通过输入新的定义来轻松更改,新输入的定义将覆盖先前的定义,示例内容如下:

    

jshell> String quintic(String a){

...> return "Know:" + a;

...> }

| 已修改 方法 quintic(String)

| 更新已覆盖 方法 quintic(String)

 

jshell> quintic("Base");

$9 ==> "Know:Base"

| 已创建暂存变量 $9 : String

 

jshell>

请注意,创建方法和以前一样,通过反馈表明修改方法。消息表示定义已更改,但该方法具有相同的方法名,因此所有现有用法仍然有效。

我们还可以以不兼容的方式来更改定义,例如,将 x 从 int 更改为 String 类型的,示例内容如下:

jshell> int x = 66;

x ==> 66

| 已创建 变量 x : int

 

jshell> String x;

x ==> null

| 已替换 变量 x : String

| 更新已覆盖 变量 x : int

 

jshell>

 

JShell 命令

JShell命令在JShell会话中输入,用于控制环境和显示信息。命令与代码段之间的区别是,前面有一个正斜杠(/),有关当前变量、方法和类型的信息,使用 /vars/methods 和 /types 命令。获取有关输入的代码片段列表,则使用该/list命令,示例内容如下:

jshell> /vars

| String x = null

| int $3 = 520

| String $5 = "Know:Base"

 

jshell> /methods

| String quintic(String)

 

jshell> /list

 

2 : String x;

3 : 128 + 392

4 : String quintic(String a){

return "Know:" + a;

}

5 : quintic("Base");

 

jshell>

JShell 还有一个默认的启动脚本,它在 JShell 启动之前以默认的方式自动执行,这样你就可以快速开始工作。除非您使用 /list -start 或 /list -all 命令请求启动脚本中的内容,否则不会列出这些脚本儿内容,示例内容如下:

jshell> /list -all

 

s1 : import java.io.*;

s2 : import java.math.*;

s3 : import java.net.*;

s4 : import java.nio.file.*;

s5 : import java.util.*;

s6 : import java.util.concurrent.*;

s7 : import java.util.function.*;

s8 : import java.util.prefs.*;

s9 : import java.util.regex.*;

s10 : import java.util.stream.*;

1 : int x = 66;

2 : String x;

3 : 128 + 392

e1 : String quintic(String a){

...> return "Know:" + a;

4 : String quintic(String a){

return "Know:" + a;

}

5 : quintic("Base");

 

jshell>

JShell 命令的缩写

使用缩写命令的方式,可以减少不必要的内容输入,更主要的是节省时间。命令 /set 子命令,命令参数和命令选项都可以缩写,只要缩写是唯一的即可。例如,/list 命令的开头是 /l ,而 /l 再命令中是唯一的,在其命令选项方面 -all ,而 -a 在其命令选项中是唯一的,所以可以直接将 " /list -all " 缩写为 " /l -a ",示例内容如下:

jshell> /l -a

 

s1 : import java.io.*;

s2 : import java.math.*;

s3 : import java.net.*;

s4 : import java.nio.file.*;

s5 : import java.util.*;

s6 : import java.util.concurrent.*;

s7 : import java.util.function.*;

s8 : import java.util.prefs.*;

s9 : import java.util.regex.*;

s10 : import java.util.stream.*;

1 : int x = 66;

2 : String x;

3 : 128 + 392

e1 : String quintic(String a){

...> return "Know:" + a;

4 : String quintic(String a){

return "Know:" + a;

}

5 : quintic("Base");

 

jshell>

JShell 小试牛刀

类的创建于调用,示例内容如下图:

 

 

小结

JShell 工具功能非常的强大,除了以上示例内容外,还包括另外的两个强大功能,即:支持外部编辑器编辑、支持外部代码引用。

外部编辑器:JShell 除了支持在 JShell 提示符处编辑输入外,还可以选择外部编辑器进行编辑代码,这个操作是不是很6。通过 Shell 编辑,我们可以在输入时编辑片段和命令,以及检索和更改以前输入的片段和命令。外部编辑器提供了另一种编辑和创建代码段的方法,使用多行代码段时更容易实现。

外部代码引用:在 JShell 会话中,通过类路径的方式访问外部类。可通过模块路径、附加模块设置和模块导出设置的方式来访问外部模块。

Guess you like

Origin www.cnblogs.com/ningbj/p/11665907.html