Java Shell 初探

版权声明:转载请通知,复制请通知 如有兴趣欢迎加群:653460549 - Java资源分享 https://blog.csdn.net/qq_42038407/article/details/82939398

1.前言

       在一波新技术快速刷新的过程中,Java一直被嘲讽没有Shell, 在Java JDK 9版本后终于出现了JShell,基于Java的高度格式化要求,代码规范的程度相对于其他类型的语言来说有点恐怖的情况下,很好奇这个Shell的使用简便性到底能达到多少。所以特此记录一下JShell 的初步使用。

2.简介

先看一下官方说明(官方文档位置

Why Use JShell?

Using JShell, you can enter program elements one at a time, immediately see the result, and make adjustments as needed.

Java program development typically involves the following process:

  • Write a complete program.

  • Compile it and fix any errors.

  • Run the program.

  • Figure out what is wrong with it.

  • Edit it.

  • Repeat the process.

JShell helps you try out code and easily explore options as you develop your program. You can test individual statements, try out different variations of a method, and experiment with unfamiliar APIs within the JShell session. JShell doesn’t replace an IDE. As you develop your program, paste code into JShell to try it out, and then paste working code from JShell into your program editor or IDE.

为什么使用JShell:我认为方便了对小型代码的理解、调试、结果锁定检查等基本操作。

使用流程上述内容已经表达:

  1. 写程序
  2. 编译并修复BUG
  3. 运行
  4. 寻找问题
  5. 编辑
  6. 重复上述工作

可以说这个Shell 并不是Java IDE 的替代品,更类似于一种工具的使用。

3.使用

3.1:基本环境要求:JDK 9+(测试环境Windows 10)

3.2:进入Shell (确保%JAVA_HOME%\bin已经在环境变量里)

3.3:windows进入控制台:JShell        进入了JShell交互状态

3.4:退出指令:/exit

4.基本语法介绍

你可以完全的把他当成一个类来使用,或者方法体,完全的随心所欲

声明变量并定义

String firstStr = "Hello JShell";

使用变量(输出该字符串)

System.out.println(firstStr);

注意:如果直接输入 也可以看到输出, 但是并没有调用该变量:

firstStr

但是这种方式没有任何意义,并不是使用该变量,而是单纯的输出这个变量的信息(进入SHell 的时候未指定反馈模式 所以 现在看来除了值信息外 没有其他信息,此过程后面会说)。

获得字符串长度

int strLength = firstStr.length();

说到这里,大家就应该明白了如何调用方法,就不再赘述了

自动变量:为声明的变量名直而直接输入变量值,JShell会自动创建一个变量名来保存

就像获取字符串长度的那个,不输入 int strLength = 的话 :

firstStr.length();

自动创建变量 $4来保存结果。

创建类

其实很简单 , publi.........如下图

输入的时候 支持Tab 补全

调用类方法

正常的调用就可以了。

直接定义方法

说白了就是不写类框了

调用的时候 和类方法一样 直接用就可以了

引入类库文件:

可以使用 shift + tab i 来实现类库引入

比如引入Frame:

new Frame然后ctrl + tab 松开后按下i 即可选择引入还是其他

5.基本的东西就这些,然后说一说特别的地方:

1.方法中变量后定义

什么意思呢,就是说 现在有一个方法求圆的面积,但是由于某些原因 我没有定义PI的值,而是先写的方法

如下图,他会提示你有一个变量没有定义,所以这个方法目前无法调用,直到你声明了这个变量才可以使用这个方法

声明变量PI:

然后再调用方法就可以正常使用了

$6为Shell创建的自动变量

2.扩展编辑器:

shell状态下 输入/edit 即可

每当你再编辑器中写一行文字,就会自动更新一次shell,自己实验一下就好了

3.扩展反馈信息

两种方式 进入shell的时候加上参数:

JShell -v

或者在Shell模式下输入:(tab 可以补全)

 /set feedback verbose

verbose 是详细模式

具体模式如下,四种模式 未加入参数的情况是normal

Setting the Feedback Mode

A feedback mode defines the prompts and feedback that are used in your interaction with JShell. Predefined modes are provided for your convenience. You can create custom modes as needed.

The predefined modes can’t be modified, but they can be used as the base of a custom mode. The predefined modes, in descending order of verbosity are verbosenormalconcise, and silent.

The following table shows the differences in the predefined modes.

Mode Value Snippets Declaration Updates Commands Prompt

verbose

name ==> value (and description)

Yes

Yes

Yes

\njshell>

normal

name ==> value

Yes

No

Yes

\njshell>

concise

name ==> value (only expressions)

No

No

No

jshell>

silent

No

No

No

No

->

4.指令缩减:

当指令可以被缩写单一识别的时候 可以使用缩减来优化指令长度:

完全等于上面的指令 /se(t) fe(edback) v(erbose)

具体指令集使用/help查看就可以

5.脚本

官方说明

A JShell script is a sequence of snippets and JShell commands in a file, one snippet or command per line.

Scripts can be a local file, or one of the following predefined scripts:

Script Name Script Contents

DEFAULT

Includes commonly needed import declarations. This script is used if no other startup script is provided.

PRINTING

Defines JShell methods that redirect to the print, println, and printf methods in PrintStream.

JAVASE

Imports the core Java SE API defined by the java.se module, which causes a noticeable delay in starting JShell due to the number of packages.

这部分大家自己查看吧 没有什么特别的说明。

官方Script说明 

鄙人喜欢Java, 如果你也想一起交流欢迎入群:653460549

猜你喜欢

转载自blog.csdn.net/qq_42038407/article/details/82939398
今日推荐