亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> java

Java 9 - REPL (JShell)

REPL 代表读取-评估-打印循环。使用 JShell,java 具有 REPL 功能。使用REPL,我们可以在不使用javac编译的情况下对基于java的逻辑进行编码和测试,并直接查看计算结果。

运行 JShell

打开命令提示符并输入 jshell。

$ jshell
|  Welcome to JShell -- Version 9-ea
|  For an introduction type: /help intro
jshell>

查看 JShell 命令

一旦 jshell 命令开始运行,输入 /help。

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>
|  edit a source entry referenced by name or id
|  /drop <name or id>
|  delete a source entry referenced by name or id
|  /save [-all|-history|-start] <file>
|  Save snippet source to a file.
|  /open <file>
|  open a file as source input
|  /vars [<name or id>|-all|-start]
|  list the declared variables and their values
|  /methods [<name or id>|-all|-start]
|  list the declared methods and their signatures
|  /types [<name or id>|-all|-start]
|  list the declared types
|  /imports 
|  list the imported items

运行 JShell 命令

一旦 jshell 命令开始运行,输入 /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>

在 JShell 中运行计算。

尝试在 JShell 中运行简单的计算。

jshell> 3+1
$1 ==> 4
jshell> 13%7
$2 ==> 6
jshell> $2
$2 ==> 6
jshell>

在 JShell 中创建和使用函数

创建一个函数 doubled() 来获取 int 并返回它的 double 值。

jshell> int doubled(int i){ return i*2;}
|  created method doubled(int)
jshell> doubled(6)
$3 ==> 12
jshell>

退出 JShell

输入 /exit。

jshell> /exit
| Goodbye

java

  1. Java 运算符
  2. Java 评论
  3. Java if...else 语句
  4. Java for-each 循环
  5. Java 字符串
  6. Java 接口
  7. Java 匿名类
  8. Java try-with-resources
  9. Java 注释
  10. Java 断言
  11. Java 向量
  12. Java 9 - REPL (JShell)