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 intro jshell>
JShell コマンドを表示する
入力/helpJShell に関連するコマンドを確認できます:
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 ==> 4 jshell> 13%7 $2 ==> 6 jshell> $2 $2 ==> 6 jshell>
JShell での関数の作成と使用
関数 doubled() を作成し、渡された整数型パラメータを 2 倍して返します:
jshell> int doubled(int i){ return i*2;}
| created method doubled(int)
jshell> doubled(6)
$3 ==> 12
jshell>
JShell を終了する
/exit コマンドを入力して jshell を終了します:
jshell> /exit | Goodbyeその他の拡張
Java 9 新機能