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> /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> 3+1 $1 ==> 4 jshell> 13%7 $2 ==> 6 jshell> $2 $2 ==> 6 jshell>
JShellでの関数作成と使用
intを受け取り2倍の値を返すdoubled()関数を作成:
jshell> int doubled(int i){ return i*2;}
| created method doubled(int)
jshell> doubled(6)
$3 ==> 12
jshell>
JShellの終了
jshell> /exit | Goodbye
Java
- Java演算子をマスター:種類、構文、実践例
- Javaコメント:種類、使い方、ベストプラクティス
- Java拡張forループをマスター:構文、例、ベストプラクティス
- Java文字列をマスター:生成、メソッド、ベストプラクティス
- Javaインターフェース完全ガイド:概念、実装、ベストプラクティス
- Java無名インナークラス完全ガイド:定義、構文、実践例
- Javaのtry-with-resourcesをマスターする:自動リソース管理の仕組みと活用法
- Javaアノテーション完全ガイド:種類、配置場所、実践例
- Javaアサーション完全ガイド:バグ早期発見とコード信頼性の向上
- Java Vectorの理解:使い方、特徴、ArrayListとの比較
- Java 9 REPL(JShell):インタラクティブなコーディングとテスト