Java REPL - jshell

Java REPL 或 jshell 是 [java 9] 中引入的新工具(/community/tutorials/java-9-features-with-examples)。

Java 回复

首先,让我们试着理解为什么在Java中添加了REPL支持,如果它是那么重要,那么为什么在这么晚的版本中。正如你所知道的,Scala(/community/tutorials/scala)由于其功能和优势而变得非常受欢迎,从小到大规模的应用程序。它支持多范式(对象导向和功能编程)和REPL。Oracle Corporation正试图将大多数Skala功能集成到Java中。他们已经将一些功能编程功能集成到Java 8(/community/tutorials/java-8-features-with-examples)的一部分。如Lambda表达式。Scala的最佳功能之一是 REPL(Read-Evaluate-Print-Loop)。它是一个命令行界面和Scala Interpreter来执行S

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. Open command prompt and check java version to make sure you have java 9 or above, then only you can use jshell. java repl, 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.

Java REPL - jshell 基本知识

We can access Java REPL by using jshell command available as shown in below image. java repl Now, it's time to execute few simple java examples to get the taste of java REPL tool.

 1pankaj:~ pankaj$ jshell 
 2|  Welcome to JShell -- Version 9
 3|  For an introduction type: /help intro
 4
 5jshell> 
 6
 7jshell> System.out.println("Hello World");
 8Hello World
 9
10jshell> String str = "Hello JournalDev Users"
11str ==> "Hello JournalDev Users"
12
13jshell> str
14str ==> "Hello JournalDev Users"
15
16jshell> System.out.println(str)
17Hello JournalDev Users
18
19jshell> int counter = 0
20counter ==> 0
21
22jshell> counter++
23$6 ==> 0
24
25jshell> counter
26counter ==> 1
27
28jshell> counter+5
29$8 ==> 6
30
31jshell> counter
32counter ==> 1
33
34jshell> counter=counter+5
35counter ==> 6
36
37jshell> counter
38counter ==> 6
39
40jshell>

java repl example 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. NOTE: We don't need to use "semicolons" for simple statements as shown in the above diagram.

Java REPL - 执行类

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

 1jshell> class Hello {
 2   ...> public static void sayHello() {
 3   ...> System.out.print("Hello");
 4   ...> }
 5   ...> }
 6|  created class Hello
 7
 8jshell> Hello.sayHello()
 9Hello
10jshell>

java repl class

Java REPL - 帮助和退出

若要获取 jshell 工具帮助部分,请使用 /help 命令. 若要离开 jshell,请使用 /exit 命令。

 1jshell> /help
 2|  Type a Java language expression, statement, or declaration.
 3|  Or type one of the following commands:
 4|  /list [<name or id>|-all|-start]
 5|  	list the source you have typed
 6|  /edit <name or id>
 7...
 8
 9jshell> /exit
10|  Goodbye
11pankaj:~ pankaj$

我们还可以使用Ctrl + D命令来退出jshell工具. 这是关于Java REPL和jshell工具的基本知识,阅读更多在 jshell - java shell

Published At
Categories with 技术
Tagged with
comments powered by Disqus