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 introjshell> jshell> System.out.println("Hello World");
Hello Worldjshell> String str = "Hello JournalDev Users"
str ==> "Hello JournalDev Users"jshell> str
str ==> "Hello JournalDev Users"jshell> System.out.println(str)
Hello JournalDev Usersjshell> int counter = 0
counter ==> 0jshell> counter++
$6 ==> 0jshell> counter
counter ==> 1jshell> counter+5
$8 ==> 6jshell> counter
counter ==> 1jshell> counter=counter+5
counter ==> 6jshell> counter
counter ==> 6jshell>

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

java repl_Java REPL – jshell相关推荐

  1. 第18.6.1章 JDK9的新特性—Java的REPL工具: jShell命令

    6. 新语法结构 新的语法结构,为我们勾勒出了 Java 语法进化的一个趋势,将开发者从复杂.繁琐的低层次抽象中逐渐解放出来,以更高层次.更优雅的抽象,既降低代码量,又避免意外编程错误的出现,进而提高 ...

  2. 在 Java 9 的JShell中 跟Kotlin 的REPL中尽情体验函数式编程乐趣吧

    Java 9 JShell : 螢幕快照 2017-09-25 22.58.02.png jshell> List<String> alphabet = List.of(" ...

  3. Java 9:JShell

    JShell是Java 9中引入的新工具,用于评估在命令行上输入的Java语句. 它是Java平台上第一个正式的REPL(读取-评估-打印循环)实现,非常适合尝试Java代码而无需启动IDE或编写完整 ...

  4. Java中映射怎么实现_我们如何在Java 9的JShell中实现映射?

    JShell是Java 9中引入的Java Shell工具.它是一个交互式工具,可以读取输入,执行输入并在命令行提示符下打印输出.我们不需要像Java类一样编写一种方法来执行它.main() 我们可以 ...

  5. java streams_使用JShell的Java 9 Streams API

    java streams 这篇文章着眼于使用JShell的Java 9 Streams API. Streams API的更改以Java 8中Streams的成功为基础,并引入了许多实用程序方法– t ...

  6. java 执行shell 卡住_Aid learning/Termux之Jupyter的Java编程高级篇——包管理

    Aid Learning自从0.80版本开始就自带Jupyter了. Termux也可以安装Python,然后安装Jupyter,不过要想在Termux使用纯种Java,特别是Java使用Jupyte ...

  7. java核心面试_前100多个核心Java面试问题

    java核心面试 Core Java interview questions help you in preparing for java based interviews. Whether you ...

  8. java组合与继承始示例_Java 9功能与示例

    java组合与继承始示例 Java 9 is a major release and it has brought us a lot of features for developers. In th ...

  9. Java小农养成记第一天

    Day01 第一章 开发前言 1.1 java语言概述 Java语言是美国Sun公司(Stanford University Network),在1995年推出的高级编程语言.在2009年Oracle ...

最新文章

  1. 分布式事务,阿里为什么钟爱TCC
  2. 你想要的宏基因组-微生物组知识全在这(181001)
  3. 一步一步学习hadoop(七)
  4. Hibernate 多对多映射实列
  5. Debug Tensorflow: tensorflow.python.framework.errors_impl.InvalidArgumentError: OpKernel ‘ConcatV2‘
  6. List和DataTable的Limit
  7. Ubuntu10.10的网络配置
  8. 读懂这本书,才算读懂阿里大数据
  9. Matlab光滑曲线多项式拟合与样条曲线拟合的两个案例
  10. while 循环的理解
  11. 苹果mac视频剪辑软件:Final Cut Pro
  12. 反编译OD工具OllyDBG 2.0.1下载
  13. 用户标签照妖镜api、旺旺搜索搜索标签api、用户标签查询api、淘宝用户标签透视透视api
  14. 保持健康和快乐的20种方法
  15. bat文件的@echo off是什么作用?
  16. mysql date 24小时制_SpringBoor连接mysql数据库取数据库中时间格式是12小时制的时间,如何显示成24小时制...
  17. 基于评论的跨境电商产品满意度分析_kaic
  18. Exchange 常见问题之十
  19. 药厂中PLD-601不溶性微粒检查仪IoT的流程梳理
  20. [leetcode] 229. Majority Element II

热门文章

  1. java io读书笔记(8)FileInputStream/FileOutputStream的应用
  2. wordpress 如何移除管理后台仪表盘小工具
  3. [转载] Python杂记之 list.clear()方法,清空列表
  4. [转载] Python字符串isdecimal() isdigit()isnumeric()等判断方法的区分。
  5. [转载] Sublime Text 3 插件 ClangFormat 格式化 C/C++版
  6. lua循环,减少不必要的循环
  7. 《游戏人工智能编程》读书笔记 —— 向量的归一和点乘
  8. winform程序的皮肤问题
  9. WebResource.axd引起的问题
  10. WebDevHelper -- RESTful服务和Ajax开发时的利器