无涯教程-Java9 - JShell(REPL)

本文介绍了Java9中的JShell(Read-Eval-PrintLoop)工具,它提供了交互式编程环境,无需编译即可进行编码和测试,展示了如何运行计算、创建函数以及退出JShell的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

REPL代表"Read-Eval-Print Loop"。使用JShell,java具有REPL函数。使用REPL,无涯教程可以对基于Java的逻辑进行编码和测试,而无需使用javac进行编译,并且可以直接查看计算输出。

运行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并返回其doubled值。

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

退出JShell

输入/exit。

jshell> /exit
| Goodbye

Java9 - JShell(REPL) - 无涯教程网无涯教程网提供REPL代表"Read-Eval-Print Loop"。使用JShell,java具有REPL函数。使用REPL,无涯教程...https://www.learnfk.com/java/java9-repl.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值