Java 9 – Exploring the REPL – Java 9 – 探索 REPL

最后修改: 2017年 3月 1日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

This article is about jshell, an interactive REPL (Read-Evaluate-Print-Loop) console that is bundled with the JDK for the upcoming Java 9 release. For those not familiar with the concept, a REPL allows to interactively run arbitrary snippets of code and evaluate their results.

本文是关于jshell,一个交互式的REPL(Read-Evaluate-Print-Loop)控制台,它被捆绑在即将发布的Java 9的JDK上。对于那些不熟悉这个概念的人来说,REPL允许交互式地运行任意的代码片断并评估其结果。

A REPL can be useful for things such as quickly checking the viability of an idea or figuring out e.g. a formatted string for String or SimpleDateFormat.

REPL对于快速检查一个想法的可行性或找出例如StringSimpleDateFormat的格式化字符串等事情非常有用。

2. Running

2.跑步

To get started we need to run the REPL, which is done by invoking:

为了开始工作,我们需要运行REPL,这可以通过调用。

$JAVA_HOME/bin/jshell

If more detailed messaging from the shell is desired, a -v flag can be used:

如果需要从shell得到更详细的信息,可以使用-v标志。

$JAVA_HOME/bin/jshell -v

Once it is ready, we will be greeted by a friendly message and a familiar Unix-style prompt at the bottom.

一旦准备好了,我们就会看到一条友好的信息和底部熟悉的Unix式提示。

3. Defining and Invoking Methods

3.定义和调用方法

Methods can be added by typing their signature and body:

方法可以通过输入其签名和主体来添加。

jshell> void helloWorld() { System.out.println("Hello world");}
|  created method helloWorld()

Here we defined the ubiquitous “hello world” method. It can be invoked using normal Java syntax:

这里我们定义了无处不在的 “hello world “方法。 它可以用正常的Java语法来调用。

jshell> helloWorld()
Hello world

4. Variables

4.变量

Variables can be defined with the normal Java declaration syntax:

变量可以用正常的Java声明语法来定义。

jshell> int i = 0;
i ==> 0
|  created variable i : int

jshell> String company = "Baeldung"
company ==> "Baeldung"
|  created variable company : String

jshell> Date date = new Date()
date ==> Sun Feb 26 06:30:16 EST 2017
|  created variable date : Date

Note that semicolons are optional. Variables can also be declared without initialization:

注意,分号是可选的。 变量的声明也可以不进行初始化。

jshell> File file
file ==> null
|  created variable file : File

5. Expressions

5.表达式

Any valid Java expression is accepted and the result of the evaluation will be shown. If no explicit receiver of the result is provided, “scratch” variables will be created:

任何有效的Java表达式都可以被接受,并且评估的结果将被显示。如果没有提供明确的结果接收器,将创建 “scratch “变量。

jshell> String.format("%d of bottles of beer", 100)
$6 ==> "100 of bottles of beer"
|  created scratch variable $6 : String

The REPL is quite helpful here by informing us that it created a scratch variable named $6 which value is “100 of bottles of beer on the wall” and its type is String.

REPL在这里很有帮助,它告诉我们它创建了一个名为$6的变量,其值为 “墙上的100瓶啤酒”,其类型为String

Multi-line expressions are also possible. Jshell is smart enough to know when an expression is incomplete and will prompt the user to continue on a new line:

多行表达式也是可能的。 Jshell足够聪明,知道什么时候表达式是不完整的,并将提示用户在新的一行中继续:

jshell> int i =
   ...> 5;
i ==> 5
|  modified variable i : int
|    update overwrote variable i : int

Note how the prompt changed to an indented …> to signify the continuation of an expression.

请注意提示符如何变成了缩进的…>,以表示表达式的延续性。

6. Commands

6.命令

Jshell provides quite a few meta-commands that aren’t related to evaluating Java statements. They all start with a forward-slash (/) to be distinguished from normal operations. For example, we can request a list of all available commands by issuing /help or /?.

Jshell提供了许多与评估Java语句无关的元命令。 它们都以正斜杠(/)开始,以区别于正常操作。例如,我们可以通过发出/help或者/? 来请求一个所有可用命令的列表。

Let’s take a look at some of them.

让我们来看看其中的一些情况。

6.1. Imports

6.1.进口

To list all the imports active in the current session we can use the /import command:

要列出当前会话中所有激活的导入,我们可以使用/import命令。

jshell> /import
|    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.*

As we can see, the shell starts with quite a few useful imports already added.

我们可以看到,shell开始时已经添加了不少有用的导入。

6.2. Lists

6.2.列表

Working in a REPL is not nearly as easy as having a full-featured IDE at our fingertips: it is easy to forget what variables have which values, what methods have been defined and so on. To check the state of the shell we can use /var, /methods, /list or /history:

在REPL中工作并不像在我们的指尖上有一个全功能的IDE那样容易:我们很容易忘记哪些变量有哪些值,哪些方法已经被定义等等。 要检查shell的状态,我们可以使用/var/methods/list/history:

jshell> /var
| int i = 0
| String company = "Baeldung"
| Date date = Sun Feb 26 06:30:16 EST 2017
| File file = null
| String $6 = "100 of bottles of beer on the wall"

jshell> /methods
| void helloWorld()

jshell> /list

 1 : void helloWorld() { System.out.println("Hello world");}
 2 : int i = 0;
 3 : String company = "Baeldung";
 4 : Date date = new Date();
 5 : File file;
 6 : String.format("%d of bottles of beer on the wall", 100)

jshell> /history

void helloWorld() { System.out.println("Hello world");}
int i = 0;
String company = "Baeldung"
Date date = new Date()
File file
String.format("%d of bottles of beer on the wall", 100)
/var
/methods
/list
/history

The difference between /list and /history is that the latter shows commands in addition to expressions.

/list/history的区别在于,后者除了显示表达式外,还显示命令。

6.3. Saving

6.3.储蓄

To save the expression history the /save command can be used:

要保存表达历史,可以使用/save命令。

jshell> /save repl.java

This saves our expression history into repl.java in the same directory from which we ran the jshell command.

这将把我们的表达式历史保存到我们运行jshell命令的同一目录中的repl.java

6.4. Loading

6.4.加载

To load a previously saved file we can use the /open command:

要加载一个先前保存的文件,我们可以使用/open命令。

jshell> /open repl.java

A loaded session can then be verified by issuing /var, /method or /list.

然后可以通过发出/var/method/list来验证一个加载的会话。

6.5. Exiting

6.5.退场

When we are done with the work, the /exit command can terminate the shell:

当我们完成工作后,/exit命令可以终止shell。

jshell> /exit
|  Goodbye

Goodbye jshell.

再见jshell

7. Conclusion

7.结论

In this article, we took a look at Java 9 REPL. Since Java has been around for over 20 years already, perhaps it arrived a little late. However, it should prove to be another valuable tool in our Java toolbox.

在这篇文章中,我们看了一下Java 9 REPL。由于Java已经有20多年的历史了,也许它来得有点晚。但是,它应该被证明是我们Java工具箱中的另一个有价值的工具。