工業製造
産業用モノのインターネット | 工業材料 | 機器のメンテナンスと修理 | 産業プログラミング |
home  MfgRobots >> 工業製造 >  >> Industrial programming >> Java

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

  1. Java演算子をマスター:種類、構文、実践例
  2. Javaコメント:種類、使い方、ベストプラクティス
  3. Java拡張forループをマスター:構文、例、ベストプラクティス
  4. Java文字列をマスター:生成、メソッド、ベストプラクティス
  5. Javaインターフェース完全ガイド:概念、実装、ベストプラクティス
  6. Java無名インナークラス完全ガイド:定義、構文、実践例
  7. Javaのtry-with-resourcesをマスターする:自動リソース管理の仕組みと活用法
  8. Javaアノテーション完全ガイド:種類、配置場所、実践例
  9. Javaアサーション完全ガイド:バグ早期発見とコード信頼性の向上
  10. Java Vectorの理解:使い方、特徴、ArrayListとの比較
  11. Java 9 REPL(JShell):インタラクティブなコーディングとテスト