关于 JShell,开发人员需要知道的10件事情

点击左上角蓝字,关注“锅外的大佬”640?wx_fmt=gif

专注分享国外最新技术内容

640?wx_fmt=png

Jshell 作为 Kulla 项目下 Java 增强建议(JEP) 222 的一部分而被引入在 JDK 9 中。许多编程语言,例如 JavaScript,Python,Ruby 等为他们的执行提供一些易于使用的命令行工具,但是 Java 仍然缺少这样的实用工具。因此, JDK 9 引入了 Java shell (JShell)工具。

我在前一篇 文章 中讨论了 JShell (这是一个读-求值-打印循环:REPL) 的基础知识。在本文中,我将介绍 JShell 中的一些高级概念,这些概念是用户在快速开发时应该了解的。

1.   重新声明变量

在 java 中,是不可能重新声明变量的。当然,在 JShell 的帮助下,您总是可以根据需要重新声明变量。请注意,这适用于原始类型变量和引用变量。事实上,用户可以根据需要多次重新声明。

示例:

jshell> String str="Hello"	
str ==> "JShell"
jshell> Integer str=10	
str ==> 10

2. 临时变量

如果未由用户明确指定,则从 JShell 命令行中的任何表达式都将分配给某些变量。这些变量称为临时变量。例:

jshell> "Hello"+"JShell"	
$1 ==> "HelloJShell"

注意,为了了解变量类型或者更多关于表达式求值的详细信息,我们可以将反馈模式设置为 verbose,如下所示:

/set feedback verbose
jshell> 60+10	
$2 ==> 70	
|  created scratch variable $21 : int

要退出 verbose 模式,设置反馈模式为 normal :

/set feedback normal

3. JShell 中的前向引用

JShell 中的前向引用允许您预先调用构造,即使它们并不存在。例如,假设有一个名为 greet()  的方法,如下所示。注意,greet()  在内部调用另一个名为 greetHelloWorld()  的方法,该方法尚未声明。创建 greet() 是成功的,但是在声明 greetHelloWorld()  之前不能调用它。这在 JShell 中称为前向引用。

例:

jshell> public void greet(){	
...> greetHelloWorld();}	
|  created method greet(), however, it cannot be invoked until method greetHelloWorld() is declared               jshell> greet()	
|  attempted to call method greet() which cannot be invoked until method greetHelloWorld() is declared
jshell> public void greetHelloWorld(){	
...> System.out.println("Hello World");}	
|  created method greetHelloWorld()
jshell> greet()	
Hello World

4.  JShell 中的异常处理

例:

jshell> int divide(int a,int b) throws IOException{	
...> if(b==0){	
...> throw new IOException();	
...> }	
...> return a/b;	
...> }	
|  created method divide(int,int)
jshell> divide(1,0)	
|  java.io.IOException thrown:	
|        at divide (#2:3)	
|        at (#3:1)

注意,我们没有捕捉到任何由 divide 方法抛出的异常;JShell 负责这方面的工作。还要注意,我们并没有导入  IOException 类,但是代码编译并执行得很好。原因是,对于任何 JShell 会话,默认情况下都会导入一些包。要检查在任意 JShell 会话中默认导入包的方法如下:

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.*	
|    import java.io.IOException

5. JShell会话中指令的持久性行为

默认情况下,所有在 JShell session 中的指令都不持续。当用户从 JShell session 退出时,指令瞬间丢失。

然而,JShell 为用户提供了保存一个特定的 JShell session 信息,并在不同的 JShell Session  中访问该信息的方法。如果用户想要从 JShell session 中保存有用的片段并在不同的 JShell session 中访问它们,是非常方便的。

例:

jshell> String s="Hello"	
s ==> "Hello"	

	
jshell> int i=100;	
i ==> 100
jshell> /save C:\data\mySession.jsh	
jshell> /exit	
|  Goodbye	

	
λ jshell	
|  Welcome to JShell -- Version 9.0.4	
|  For an introduction type: /help intro
jshell> /vars
jshell> /open C:\Data\mySession.jsh
jshell> /vars	
|    String s = "Hello"	
|    int i = 100

6.  使用外部库

有许多有用的第三方开源库。通常,开发人员将这些库保存在项目的类路径中并使用它。但是,对于 JShell ,使用第三方库非常容易。

举例说明,假设我们想要使用第三方库 Apache Commons Lang 的 String 工具类。下面是在类路径中保存库的语法:

shell> /env --class-path <Relative Path of lib from where JShell is run>
jshell> /env --class-path ../lib/commons-lang3-3.8.1.jar	
|  Setting new options and restoring state.
import org.apache.commons.lang3.StringUtils;
jshell> System.out.println(StringUtils.isEmpty(""))	
true
jshell> System.out.println(StringUtils.isEmpty("hello"))	
false

7. 使用 JShell  标准命令和工具进行快速开发

JShell 有自己的特定的方便命令,可以用于在 JShell 控制台上进行更快的测试。以下是一些有用的命令:

 /history - Prints all commands executed on JShell (Java Commands+ JShell specific commands)

例:

jshell> String s ="Hello"	
s ==> "Hello"	

	
jshell> class Employee{	
...> }	
|  created class Employee	

	
jshell> /vars	
|    String s = "Hello"	

	
jshell> /history	
String s ="Hello"	
class Employee{	
}	
/vars	
/history
 /list    - Prints all JAVA related commands executed in JShell. Notice that this list the command in Numerical              order of each command identifier. This identifier can be used to execute certain construct again.

例:

jshell> /list	
1 : String s ="Hello";	
2 : class Employee{	
}
jshell> /1	
String s ="Hello";	
s ==> "Hello"
/reset   - Resets the state of current JShell session.
CTRL+R   - For searching a particular command
CTRL+S   - Performing Forward Search
CTRL+C   - To exit from JShell session
/exit    - To exit from JShell session
/vars     - To list all variables inside current JShell session
/imports  - To list all imports inside current JShell session
/help    - To know more about JShell specific commands

8.   JShell 中的代码补全制表符

JShell 允许开发人员使用 Tab 键来自动完成代码构造。

例:

640?wx_fmt=jpeg

除此之外,用户还可以使用 JShell 查看相关包的文档:

jshell> java.io	
io	
Signatures:	
java.io
<press tab again to see documentation>

9. 在 JShell 中编写一个构造器

在开发过程中,想要在 JShell 会话中编辑以前执行的命令是很常见的。JShell 提供了一个非常方便的命令和编辑器。

例:

/edit        - Edit all constructs in current JShell session
/edit 1      - Edit only 1st construct (see from /list) in current JShell session
/edit Person - Edit only Person class in current JShell session

640?wx_fmt=jpeg

10.  使用 JShell 编程

JDK 为程序员提供了以编程的方式来访问 JShell 的 API ,而不必通过 REPL 。请参考相应的 Java 文档以获取更多详情。

在本文中,我只展示了 JShell 中的一些高级概念,而这并不是结束。我还建议读者通过 JShell 文档来了解更多信息。

640?wx_fmt=gif

右上角按钮分享给更多人哦~smiley_63.pngsmiley_63.png

640?wx_fmt=png

来都来了,点个在看再走吧~~~

640?wx_fmt=gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值