Java 简明教程
Java - REPL (JShell)
Introduction to REPL (JShell)
REPL 代表 Read Evaluate Print Loop 。 JShell 在 Java 9 中引入,这是一种交互式控制台。JShell 作为 REPL,无需保存且允许在控制台中运行任意 Java 代码片段并执行 compile java code file 。此功能对于快速测试代码非常重要,例如评估 regular expression 、检查 strings 、日期格式等。
REPL stands for Read Evaluate Print Loop. JShell was introduced in Java 9 which is an interactive console. JShell as REPL allows to run arbitrary snippet of java code in console without need to save and compile java code file. This facility is very important to test codes quickly like evaluating regular expression, checking formating of strings, date formats etc.
JShell 读取输入的每一行,评估并打印结果,然后再次准备下一组输入。
JShell reads each line entered, evaluates it and then print the result and then again becomes ready for next set of input.
Advantages of Using JShell
JShell 的这一功能为开发人员提供了以下优势 -
This capability of JShell gives developers following advantages -
-
No editor is needed to write a Java program. JShell itself works as editor and executes the Java code.
-
Using JShell, there is no requirement to save a Java file, compile and execute code cycle. Code can be directly tested in JShell without saving anything.
-
Compilation is not needed prior to execution of code.
-
If any compile-time or runtime error occurs, start fresh.
Running JShell
打开命令提示符并键入 jshell。
Open command prompt and type jshell.
D:\test>jshell
| Welcome to JShell -- Version 20.0.2
| For an introduction type: /help intro
使用 JShell,我们还可以测试方法、类和表达式。在以下示例中,让我们探讨 JShell 的一些特性。
With JShell, we can test methods, classes, expressions as well. In following examples, let’s explore some of the features of JShell.
Create and Invoke Method in JShell
以下代码段展示 JShell 中的一个示例“Hello World”程序。在这里,我们创建了一个包含一个语句来打印消息“Hello World!”的方法 greet()。然后,我们调用方法 greet(),结果打印到控制台上。
Following snippet showing a sample "Hello World" program in JShell. Here, we’ve created a method greet() which has a single statement to print a message as "Hello World!". As next, we’ve invoked the method greet() and result is printed on the console.
Creating Variables in JShell
以下代码段展示如何在 JShell 中创建 variables。分号是可选的。我们还可以在 JShell 中创建 objects。如果变量未初始化,则会对其赋予默认值,如果它是一个对象引用,则赋予 null 值。一旦创建了一个变量,就可以使用它,如最后一条语句所示,我们使用了字符串变量来打印其值。
Following snippet shows how to create variables in JShell. semi-colon is optional. We can create objects as well in JShell. If a variable is not initialized then it is given a default value or null if it is an object reference. Once a variable is created, it can be used as shown in the last statement where we’ve used the string variable to print its value.
Evaluate Expression in JShell
以下代码段展示了如何使用 JShell 求一个表达式的值。在这里,我们传递了一个返回一个格式化字符串的语句。JShell 自动创建一个字符串变量 $9 并将其赋给结果。作为下一条语句,我们打印了它。
Following snippet shows how to evaluate an expression using JShell. Here we’ve passed a statement which returns a formatted string. JShell automatically created a String variable $9 and assigned it the result. As next statement, we’ve printed.
Jshell Built-In Commands
JShell 提供了各种命令来列出已创建的变量、已创建的方法、已使用的导入等。其中一些重要的 JShell 命令有 -
JShell provides various commands to list the variables created, methods created, imports used etc. Some of the important JShell commands are -
-
/drop – This command drops code snippets identified by name, ID, or ID range.
-
/edit – This command opens an editor.
-
/env – This command displays the environment settings.
-
/exit – This command exists from the tool.
-
/history – This command displays the history of the tool.
-
/help – This command displays the command’s help.
-
/imports – This command displays the current active imports.
Example: Demonstrating /help Command
我们可以使用 /help 选项查看所有命令。
We can view all commands using /help option.
jshell> /help
| Type a Java language expression, statement, or declaration.
| Or type one of the following commands:
| /list [<name or id>|-all|-start]
| list the source you have typed
| /edit <name or id>
| edit a source entry
| /drop <name or id>
| delete a source entry
| /save [-all|-history|-start] <file>
| Save snippet source to a file
| /open <file>
| open a file as source input
| /vars [<name or id>|-all|-start]
| list the declared variables and their values
| /methods [<name or id>|-all|-start]
| list the declared methods and their signatures
| /types [<name or id>|-all|-start]
| list the type declarations
...
Example: Demonstrating /vars Command
在以下示例中,我们使用 /vars 命令来打印在一个会话中声明的变量。
In following example, we’ve used /vars command to print the variables declared during a session.
C:\Users\Mahesh>jshell
| Welcome to JShell -- Version 20.0.2
| For an introduction type: /help intro
jshell> int i = 10
i ==> 10
jshell> String name="Mahesh"
name ==> "Mahesh"
jshell> /vars
| int i = 10
| String name = "Mahesh"
jshell>
Example: Demonstrating /imports Command
我们可以使用 /imports 命令来检查 JShell 中可用的导入,如下所示:
We can use /imports command to check the imports available in JShell as shown below:
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.*
jshell>