Java9-repl

提供:Dev Guides
移動先:案内検索

Java 9-REPL(JShell)

REPLはRead-Eval-Print Loopの略です。 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を取得し、その倍増した値を返します。

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

JShellを終了する

/exitと入力します。

jshell>/exit
| Goodbye