天天看点

java repl_Java REPL – jshell

java repl

Java REPL or jshell is the new tool introduced in java 9. Today we will look into Java REPL basics and run some test programs in jshell interface.

Java REPL或jshell是java 9中引入的新工具。 今天,我们将研究Java REPL基础知识,并在jshell界面中运行一些测试程序。

Java REPL (Java REPL)

Let’s first try to understand why REPL support was added in Java, if it was that important then why in so late release.

首先,让我们尝试理解为什么在Java中添加了REPL支持,如果那么重要,那么为什么要在这么晚的版本中发布。

As you know, Scala has become very popular to develop from small to large-scale applications because of it’s features and advantages. It supports multi-paradigm (Object-Oriented and Functional Programming) and REPL.

如您所知, Scala的特性和优势已成为从小型应用程序到大型应用程序的非常受欢迎的开发。 它支持多范式(面向对象和功能编程)和REPL。

Oracle Corporation is trying to integrate most of Scala features into Java. They have already integrated some functional programming features as part of Java 8, such as lambda expressions.

Oracle Corporation正在尝试将大多数Scala功能集成到Java中。 他们已经集成了一些功能性编程功能作为Java 8的一部分,例如lambda表达式。

Scala’s one of the best features is REPL (Read-Evaluate-Print-Loop). It’s a command line interface and Scala Interpreter to execute Scala programs. It’s very easy to use Scala REPL to learn basics of scala programming and even run small test code.

Scala的最佳功能之一是REPL (读取-评估-打印循环)。 这是一个命令行界面和Scala解释器,用于执行Scala程序。 使用Scala REPL可以很轻松地学习scala编程的基础知识,甚至可以运行小的测试代码。

Because of Scala REPL and it’s benefits in reducing the learning curve and ease of running test code, Java REPL got introduced in java 9.

由于使用了Scala REPL,它在减少学习曲线和简化运行测试代码方面的优势,在Java 9中引入了Java REPL。

Java REPL – jshell (Java REPL – jshell)

Java REPL application name is

jshell

. JShell stands for Java Shell. jshell is an interactive tool to execute and evaluate java simple programs like variable declarations, statements, expressions, simple Programs etc.

Java REPL应用程序名称为

jshell

。 JShell代表Java Shell。 jshell是一个交互式工具,用于执行和评估Java简单程序,例如变量声明,语句,表达式,简单程序等。

Open command prompt and check java version to make sure you have java 9 or above, then only you can use jshell.

打开命令提示符并检查Java版本以确保您具有Java 9或更高版本,然后只有您可以使用jshell。

Since jshell don’t need any IDEs or extra editors to execute simple java programs, It’s very useful for beginners in core java and experts to use it to learn and evaluate new features and small test code.

由于jshell不需要任何IDE或额外的编辑器即可执行简单的Java程序,因此对于核心Java的初学者和专家来说,使用它来学习和评估新功能和小型测试代码非常有用。

Java REPL – jshell基础 (Java REPL – jshell basics)

We can access Java REPL by using

jshell

command available as shown in below image.

我们可以使用可用的

jshell

命令访问Java REPL,如下图所示。

Now, it’s time to execute few simple java examples to get the taste of java REPL tool.

现在,是时候执行一些简单的Java示例来了解Java REPL工具的味道了。

pankaj:~ pankaj$ jshell 
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro

jshell> 

jshell> System.out.println("Hello World");
Hello World

jshell> String str = "Hello JournalDev Users"
str ==> "Hello JournalDev Users"

jshell> str
str ==> "Hello JournalDev Users"

jshell> System.out.println(str)
Hello JournalDev Users

jshell> int counter = 0
counter ==> 0

jshell> counter++
$6 ==> 0

jshell> counter
counter ==> 1

jshell> counter+5
$8 ==> 6

jshell> counter
counter ==> 1

jshell> counter=counter+5
counter ==> 6

jshell> counter
counter ==> 6

jshell>
           

As shown in the above Java REPL examples, it’s very easy to develop “Hello World” program. We don’t need to define “public class” and public static void main(String[] args) method just to print one message.

如上述Java REPL示例所示,开发“ Hello World”程序非常容易。 我们不需要定义“公共类”和公共静态void main(String [] args)方法来仅打印一条消息。

NOTE: We don’t need to use “semicolons” for simple statements as shown in the above diagram.

注意:如上图所示,对于简单的语句,我们不需要使用“分号”。

Java REPL –执行类 (Java REPL – execute class)

We can also define and execute class methods in Java REPL shell.

我们还可以在Java REPL Shell中定义和执行类方法。

jshell> class Hello {
   ...> public static void sayHello() {
   ...> System.out.print("Hello");
   ...> }
   ...> }
|  created class Hello

jshell> Hello.sayHello()
Hello
jshell>
           

Java REPL –帮助和退出 (Java REPL – Help and Exit)

To get jshell tool help section, use

/help

command. To exit from jshell, use command

/exit

.

要获取jshell工具的帮助部分,请使用

/help

命令。 要从jshell退出,请使用命令

/exit

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

jshell> /exit
|  Goodbye
pankaj:~ pankaj$
           

We can also use

Ctrl + D

command to exit from jshell tool.

我们还可以使用

Ctrl + D

命令退出jshell工具。

That’s all about Java REPL and jshell tool basics, read more at jshell – java shell.

这就是有关Java REPL和jshell工具基础的全部内容,更多内容请参见jshell – Java shell 。

Reference: JEP 222

参考: JEP 222

翻译自: https://www.journaldev.com/9879/java-repl-jshell

java repl