java shell

Earlier we looked into Java REPL i.e. jshell basics. Today we will learn some more features of jshell or java shell.

之前我们研究过Java REPL,即jshell基础。 今天,我们将学习jshell或java shell的更多功能。

壳 (jshell)

jshell or Java Shell was introduced in Java 9 to help developers run short programs quickly without going into hassle of creating classes, main method etc. We can simply declare variables, write statements and execute them in jshell. We can also compile class for the current jshell session and then use it in the next statements. We will look into some more jshell features in this tutorial.

Java 9中引入了jshell或Java Shell,以帮助开发人员快速运行简短的程序,而无需麻烦地创建类,主方法等。我们可以简单地声明变量,编写语句并在jshell中执行它们。 我们还可以为当前的jshell会话编译类,然后在下一个语句中使用它。 在本教程中,我们将研究更多jshell功能。

jshell功能 (jshell features)

Java Shell or jshell supports following features:

Java Shell或jshell支持以下功能:

  1. We can use it as an interactive tool to evaluate declarations, statements, expressions etc. of the java programming language.我们可以将其用作评估Java编程语言的声明,语句,表达式等的交互式工具。
  2. We can see the history of our previous commands.我们可以看到先前命令的历史记录。
  3. jshell supports Tab-Completion feature.jshell支持Tab-Completion功能。
  4. Java shell adds semicolons automatically so we don’t need to add them explicitly.Java Shell自动添加分号,因此我们无需显式添加分号。
  5. We can configure default imports as per our requirements.我们可以根据需要配置默认导入。
  6. We can configure default definitions as per our requirements.我们可以根据需要配置默认定义。

We will explore these features one by one in the coming sections with some useful examples.

在接下来的部分中,我们将通过一些有用的示例逐一探讨这些功能。

Java Shell算术运算 (Java Shell Arithmetic Operations)

We can perform arithmetic operations in jshell, as shown in below example.

我们可以在jshell中执行算术运算,如下例所示。

jshell> 10+5
$1 ==> 15jshell> 10/5
$2 ==> 2jshell> 10/3
$3 ==> 3jshell> 10.0/3
$4 ==> 3.3333333333333335jshell> 10*5
$5 ==> 50jshell>

If we observe the above image, we can find that it’s very easy to test arithmetic operations in Java Shell. We can write them like simple mathematics expressions and get the results, no need to use semicolons also.

如果观察上面的图片,我们会发现在Java Shell中测试算术运算非常容易。 我们可以像简单的数学表达式一样编写它们并获得结果,而无需使用分号。

Java Shell内部变量 (Java Shell Internal Variables)

If we go through the previous section image again, we can clearly observe the following code output:

如果再次浏览上一节的图像,我们可以清楚地观察到以下代码输出:

jshell> 10+5
$1 ==> 15

Here jshell is assigning a new internal variable “$1” to hold the result value of ‘+’ arithmetic operation. Further operations results are also assigned to variables $2, $3 and so on. You can access these variable values by typing theie name in java shell.

在这里,jshell分配了一个新的内部变量“ $ 1”来保存“ +”算术运算的结果值。 进一步的运算结果也分配给变量$ 2,$ 3等。 您可以通过在Java Shell中键入theie名称来访问这些变量值。

I have one quick question for you. The java shell internal variables are final or Immutable? i.e. We cannot change it’s value?

我有一个简单的问题要问你。 Java Shell内部变量是final还是Immutable? 即我们不能改变它的价值?

Let’s find it out with a simple example.

让我们用一个简单的例子找出来。

jshell> $1
$1 ==> 15jshell> $1=20
$1 ==> 20jshell> System.out.print("$1 value now = "+$1)
$1 value now = 20
jshell>

So these internal variables are mutable, we can change their values as shown in above code. Also notice that we can use these variables in java statements, just like any other variable.

因此,这些内部变量是可变的,我们可以更改它们的值,如上面的代码所示。 还要注意,我们可以像其他任何变量一样在java语句中使用这些变量。

jshell制表符完成功能 (jshell Tab-Completion feature)

Java Shell supports tab key in very nice way to avoid typing lot of characters and save developers time. It’s just like IDE support to auto complete of variable, function or class name.

Java Shell以非常好的方式支持Tab键,以避免键入很多字符并节省开发人员时间。 就像IDE支持自动完成变量,函数或类名一样。

Let’s explore this feature now with REPL. Just type couple of characters and press “Tab” key from your keyboard, it will provide you available hints to select. If there are more hits, it displays all of them as shown below. So type few more characters to avoid conflicts.

现在让我们使用REPL探索此功能。 只需输入几个字符并按键盘上的“ Tab”键,它将为您提供可用的提示来选择。 如果还有更多匹配,它将显示所有匹配,如下所示。 因此,请再输入几个字符以避免冲突。

Below image shows jshell tab completion feature example.

下图显示了jshell选项卡完成功能示例。

Java Shell导入类型 (Java Shell Import Types)

Java shell automatically imports some packages to help us. We can use /imports command to list out all the packages imported by default.

Java Shell自动导入一些软件包来帮助我们。 我们可以使用/imports命令列出默认情况下导入的所有软件包。

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>

We can use any class from these packages without importing them explicitly. That’s why we can use Math class without importing it, as shown in below example.

我们可以使用这些包中的任何类,而无需显式导入它们。 这就是为什么我们可以使用Math类而不导入它的原因,如下面的示例所示。

jshell> Math.max(5,10)
$11 ==> 10jshell> Math.min(5,10)
$12 ==> 5

However, if we want to test any other package related class or interface, then we need to use external import. Otherwise we will get error as shown in the below code snippet:

但是,如果我们要测试任何其他与包相关的类或接口,则需要使用外部导入。 否则,我们将得到错误,如下面的代码片段所示:

jshell> ByteBuffer buf = ByteBuffer.allocate(48);
|  Error:
|  cannot find symbol
|    symbol:   class ByteBuffer
|  ByteBuffer buf = ByteBuffer.allocate(48);
|  ^--------^
|  Error:
|  cannot find symbol
|    symbol:   variable ByteBuffer
|  ByteBuffer buf = ByteBuffer.allocate(48);
|                   ^--------^
jshell>

We need to use import statement as shown below to import that whole package or specific class.

我们需要使用如下所示的import语句来导入整个包或特定的类。

jshell> import java.nio.ByteBufferjshell> ByteBuffer buf = ByteBuffer.allocate(48);
buf ==> java.nio.HeapByteBuffer[pos=0 lim=48 cap=48]jshell>

Here we are able to create a ByteBuffer object successfully without any issues because we have imported that class manually.

在这里,我们能够成功创建ByteBuffer对象,而不会出现任何问题,因为我们已经手动导入了该类。

That’s all about jshell or java shell. I have grown fond of this tool because it help me in saving time by running some small test code quickly.

这就是关于jshell或java shell的全部内容。 我非常喜欢这个工具,因为它可以通过快速运行一些小的测试代码来帮助我节省时间。

Reference: Oracle Documentation

参考: Oracle文档

翻译自: https://www.journaldev.com/12938/jshell-java-shell

java shell

java shell_jshell – Java Shell相关推荐

  1. 【Java】Java调用shell脚本

    1.概述 java 执行shell 2.utils类 package com.shell.java;import java.io.InputStreamReader; import java.io.L ...

  2. java 交互式 shell_Java9 Shell工具(JShell)

    Java Shell工具(JShell) 它是一个交互式Java Shell工具,它使我们能够从Shell中执行Java代码并立即显示输出. JShell是一个REPL(读取评估打印循环)工具,可从命 ...

  3. Google道歉作协“不理” 新语言anic:比C快比Java安全比shell简单(每日关注2010.1.11)

    Google被迫向中国作家道歉 中国作协回应称其诚意不够 2010年1月11日消息,昨日中国作协在其官网发布谷歌侵权案的最新进展,谷歌公司代表埃瑞克·哈特曼正式致信中国作协表示道歉,表示希望通过与文著 ...

  4. java makefile jar包_java makefile学习实践(编译的javac命令写在makefile中,运行命令java写在shell脚本中)...

    学习makefile教程,ubuntu中文网 1.写一个简单的java项目,不需要外部jar,用的简单的 importjava.util.ArrayList;是可以从CLASSPATH环境变量中找到的 ...

  5. 命令行,使用java的java 命令,直接调用执行class文件

    目录 0.最基本使用,参照下面链接(以前整理的资料) 1.直接调用java文件■cmd 2.调用jar包(Main Class打包到 jar包时) 3.补充说明 4.运行时,设置系统变量 5.查看类运 ...

  6. 【java】Java 包(package)

    文章目录 包的作用 创建包 import 关键字 package 的目录结构 设置 CLASSPATH 系统变量 系统包 为了更好地组织类,Java 提供了包机制,用于区别类名的命名空间. 包的作用 ...

  7. which java 找不到jdk_一、查看Jdk的安装路径:whereis javawhich java (java执行路径)echo $JAVA_HOMEecho $PATH备注:如...

    一.查看Jdk的安装路径: whereis java which java (java执行路径) echo $JAVA_HOME echo $PATH 备注:如果是windows中,可以使用: set ...

  8. java语言基础 : 初识计算机和Java语言-----java初识之路

    初识计算机和Java语言 第一节:计算机的体系结构(常识) 1.计算机的基本概念 计算机(Computer)俗称电脑,是一种用于高级计算,使用非常广泛的设备,主要由 计算机硬件和 计算机软件两个部分组 ...

  9. java未来趋势 Java促进大数据的大发展

    没有Java,甚至不会有大数据的大发展,Hadoop本身就是用Java编写的.当你需要在运行MapReduce的服务器集群上发布新功能时,你需 要进行动态的部署,而这正是Java所擅长的. 大数据领域 ...

最新文章

  1. Java实现均摊_Java均摊复杂度和防止复杂度的震荡原理分析
  2. Oracle中快速查找锁与锁等待
  3. %@page contentType=text/html;charset=gbk%与meta http-equiv=Content-Type content=text/html; ch...
  4. windows下增加python的库搜索路径
  5. Velocity的layout功能
  6. before与after的一些应用总结
  7. HorizontalScrollView里的标签改变颜色(今日头条里的功能仿照)
  8. 【操作系统】进程间通信的五种方式
  9. 《CLR via C#》读书笔记 之 基元类型、引用类型和值类型
  10. Axure谷歌浏览器Chrome扩展程序下载及安装方法
  11. 测试点击屏幕次数的软件_软件测试工程师面试如何回答登录功能怎么进行测试?...
  12. 软开关设计漫谈_软件篇
  13. Django模板过滤
  14. 新传要不要学计算机,传媒计算机实在性:真实性表象和新传媒
  15. OpenContrail 体系
  16. AutoCAD中禁用shift+鼠标中键组合作为动态观察的功能
  17. runge phenomenon(龙格现象)和过拟合
  18. [活动]和Jeffery大师的最近距离
  19. UniCode编码表,过滤不可见特殊字符
  20. Java课程设计——文本文件加密与解密软件设计与实现

热门文章

  1. PHP魔术方法小结.md
  2. 【转】深入理解JVM—JVM内存模型
  3. document.addEventListener的使用介绍
  4. Fade out transition effect using CSS3
  5. ubuntu下安装beanstalkd
  6. [转载] python判断字符串中包含某个字符串_干货分享| Python中最常用的字符串方法
  7. [转载] python indices_Python numpy.indices() 使用实例
  8. [转载] Golang-简洁的并发
  9. 设计模式——命令模式
  10. myeclipse发布项目