JDK11 | The second: JShell tool

I. Introduction

Java Shell tool is a tool JDK1.9 appeared, Java Shell tool (JShell) is a programming language for learning Java and Java code prototype interactive tools. JShell is a Read-Evaluate-Print loop (REPL), it evaluates statements, statements and expressions as you type, and displays the results immediately. The tool is run from the command line.

Second, why use JShell?

Use JShell, you can enter once a program element and immediately see the results, and make adjustments as needed.
Java application development usually involves the following process:

  • Write a complete program.
  • Compile it and fix any errors.
  • Run the program.
  • It has to figure out what the problem is.
  • Edit it.
  • This process is repeated.

JShell can help you try out the code in the development program and easily explore options. You can test a single statement, try a different method variants, and experiment with unfamiliar API in JShell session. JShell not replaced IDE. In the development program, paste the code JShell trials carried out, and then paste the working JShell editor or program code into the IDE.

Three, JShell use

1. Start and Quit

Use JShell need to configure the environment variables java.

start up:

jshell

复制代码

To start JShell in verbose mode, use the -v option:

jshell -v
复制代码

drop out:

/exit
复制代码

2. Run the code fragment

Use detail option to start JShell to obtain the maximum amount of usable feedback:

jshell -v
|  欢迎使用 JShell -- 版本 11.0.2
|  要大致了解该版本, 请键入: /help intro
复制代码

Enter the following sample statement at the prompt, and view the output displayed:

jshell> int x = 45
x ==> 45
|  已创建 变量 x : int
复制代码

First, the results are displayed. It read: 45. The value of the variable x because you are in verbose mode, it will display a description of what happened.

Note: If you do not enter a semicolon, the semicolon is automatically added to the end will terminate complete piece of code.

When the expression is entered without naming variables, it creates a temporary variable, so that you can refer to the value later. The following example shows the provisional value expression methods and results. The example also shows ...> code segment need to use multi-line continuation prompt completion input ():

jshell> String twice(String s) {
   ...>   return s + s;
   ...> }
|  已创建 方法 twice(String)

jshell> twice("Oecan")
$4 ==> "OecanOecan"
|  已创建暂存变量 $4 : String
复制代码

3. Change the definition

In the test code, you might find variables, methods, or the definition of the class does not perform the way you want. It can easily change the definition by entering a new definition, which would cover the previous definition.
To change the variables, methods, or define the class, you can simply enter the new definition. For example, twice in the definition of the method attempts to obtain a new segment is defined in the following examples:

jshell> String twice(String s) {
   ...>   return "Twice: " + s;
   ...> }
|  已修改 方法 twice(String)
|    更新已覆盖 方法 twice(String)

jshell> twice("thing")
$6 ==> "Twice: thing"
|  已创建暂存变量 $6 : String
复制代码

You can also change the type of custom variable. The following example shows the change int x is from String:

jshell> int x = 45
x ==> 45
|  已创建 变量 x : int

jshell> String x
x ==> null
|  已替换 变量 x : String
|    更新已覆盖 变量 x : int
复制代码

4. Check the default import and use of auto-completion

By default, JShell provides some common package introduced, we can use the import statement to import the necessary packets from the packet or path specified to run our code fragment. We can enter the following command to list all the packages to import:

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.*
复制代码

The automatic completion function

When we want to enter the System class, according to said earlier completion, only need to enter Sys then press the Tab key, auto-complete, and then enter ".o", it will auto-completion method, in complement " . System.out "When you press the Tab key, the next will be listed in the list of all the public methods of the current class:

jshell> System
签名:
java.lang.System

<再次按 Tab 可查看文档>

jshell> System.out.
append(        checkError()   close()        equals(        flush()        format(        getClass()     
hashCode()     notify()       notifyAll()    print(         printf(        println(       toString()     
wait(          write(    
复制代码

6. List all the valid session in the current code segment so far

jshell> /list 

   2 : 2+2
   4 : twice("Oecan")
   5 : String twice(String s) {
         return "Twice: " + s;
       }
   6 : twice("thing")
   8 : String x;

复制代码

7. List all the methods so far in the current session

jshell> /methods 
|    String twice(String)
复制代码

8. The outer code to Java code editor to write

Now, I want to make some changes to the method twice, then if there is an external code editor, then it will be very easy to do. JShell can enable the editor JShell Edit Pad, you need to enter a command to modify the above method:

After the completion of code changes, first click on "Accept" button, then click "Exit" button to exit the editor suggested method has been modified in JShell command line.

8. loaded from an external source

If you already have written in Java external file, you can use / open command into JShell environment, for example, now has a Test.java file:

void say(String name) {
     System.out.println("hello " + name);
}
复制代码
jshell> /open /Users/Documents/java11/Test.java

jshell> /methods
|    String twice(String)
|    void say(String)

jshell> say("zhangsan")
hello zhangsan
复制代码

More use JShell tools, please refer to the official Example: docs.oracle.com/javase/9/js...

Welcome scan code or search public micro-channel number "programmer fruit," I am concerned, there is concern surprise ~

Guess you like

Origin juejin.im/post/5cee4ffd5188257abf7d8461