您当前的位置:首页 > IT编程 > Java
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch |

自学教程:Java 9 REPL (JShell)

51自学网 2023-01-08 20:14:58
  Java
这篇教程Java 9 REPL (JShell)写得很实用,希望能帮到您。

REPL(Read Eval Print Loop)意为交互式的编程环境。

JShell 是 Java 9 新增的一个交互式的编程环境工具。它允许你无需使用类或者方法包装来执行 Java 语句。它与 Python 的解释器类似,可以直接 输入表达式并查看其执行结果。

执行 JSHELL

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

查看 JShell 命令

输入 /help 可以查看 JShell相关的命令:

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 命令

/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 ==> 4jshell> 13%7$2 ==> 6jshell> $2$2 ==> 6jshell>

JShell 创建与使用函数

创建一个函数 doubled() ,将传入的整型参数乘于 2 后返回:

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

退出 JShell

输入 /exit 命令退出 jshell:

jshell> /exit| Goodbye 

Java 9 模块系统
Java 9 改进 Javadoc
51自学网,即我要自学网,自学EXCEL、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。
京ICP备13026421号-1