Java 9のJShellとは?REPLでJavaコードを手軽に実行する方法
JShellは、Java 9で新しく導入された機能です。JavaにREPL(Read-Eval-Print-Loop:読み込み・評価・表示・ループ)の仕組みをもたらしました。JShellを利用することで、コンパイルを行わずにJavaベースのロジックや式をその場でテストできます。REPLは即時フィードバックループとして機能し、開発の生産性向上に大きな効果を発揮します。
JShellの基本的な使い方
ステップ1:JShellを起動する
コマンドプロンプトを開き、「JShell」と入力します。
Microsoft Windows [Version 6.3.9600] (c) 2013 Microsoft Corporation. All rights reserved. C:\Users\User>JShell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell>
ステップ2:ヘルプコマンドを確認する
JShellが起動したら、利用可能なコマンド一覧を表示するために「/help」と入力します。
jshell> /help | Type a Java language expression, statement, or declaration. | Or type one of the following commands: | /list [|-all|-start] | list the source you have typed | /edit | edit a source entry referenced by name or id | /drop | delete a source entry referenced by name or id | /save [-all|-history|-start] | Save snippet source to a file. | /open | open a file as source input | /vars [|-all|-start] | list the declared variables and their values | /methods [|-all|-start] | list the declared methods and their signatures | /types [|-all|-start] | list the declared types | /imports | list the imported items | /exit | exit jshell | /env [-class-path ] [-module-path ] [-add-modules < | view or change the evaluation context | /reset [-class-path ] [-module-path ] [-add-modules | reset jshell | /reload [-restore] [-quiet] [-class-path ] [-module-path | reset and replay relevant history -- current or previous ( | /history | history of what you have typed | /help [|] | get information about jshell | /set editor|start|feedback|mode|prompt|truncation|format ... | set jshell configuration information | /? [|] | get information about jshell | /! | re-run last snippet | / | re-run snippet by id | /- | re-run n-th previous snippet | | For more information type '/help' followed by the name of a | command or a subject. | For example '/help /list' or '/help intro'. | | Subjects: | | intro | an introduction to the jshell tool | shortcuts | a description of keystrokes for snippet and command comple | information access, and automatic code generation | context | the evaluation context options for /env /reload and /reset
ステップ3:インポート済みパッケージを確認する
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.*
このように、java.ioやjava.utilなど、よく使われる主要パッケージは最初からインポートされているため、import文を書かずにすぐにコーディングを始められます。
ステップ4:計算を実行してみる
それでは、JShellで算術演算を試してみましょう。式を入力すると、結果が即座に表示されます。
jshell> 3+5 $1 ==> 8 jshell> 8-4 $2 ==> 4 jshell> 2*6 $3 ==> 12 jshell> 9%3 $4 ==> 0 jshell> 8/2 $5 ==> 4
結果には「$1」「$2」といった自動生成された変数名が割り当てられます。これらの変数は後続の計算でも参照できるため、対話的に処理を組み立てていくことが可能です。
ステップ5:JShellを終了する
JShellを終了するには、「/exit」と入力します。
jshell> /exit | Goodbye
まとめ
JShellを使えば、小さなコード片を手軽に検証でき、クラスファイルの作成やコンパイルといった手間を省けます。新しいAPIの挙動確認や構文の学習など、日々の開発業務における強力なツールとなるでしょう。
-
Java 9のJShellで外部ライブラリをインポートする方法を解説
JShellとは何かJShellは、Java言語の学習やJavaコードのプロトタイピングに役立つ対話型ツールです。ユーザーが入力したコマンドをその場で評価し、結果を即座に表示します。このツールはREPL(Read-Evaluate-Print-Loop:読み込み・評価・表示・ループ)という仕組みに基づいて動作しています。デフォルトでインポートされるパッケージJShellセッションを開始すると、便利なJavaパッケージが自動的にインポートされます。/importsコマンドを実行すると、これらのインポート済みパッケージの一覧を確認できます。jshell> /imports| import j
-
Java Stream API入門:ストリームの特徴と基本的な使い方を実例で解説
JavaのStream(ストリーム)は、データソースから取り出したオブジェクトのシーケンス(連続した列)を表し、集計操作(アグリゲート操作)をサポートする仕組みです。Java 8で導入されて以来、コレクション操作を簡潔かつ宣言的に記述できる手段として広く活用されています。ここでは、Streamの主な特徴と、実際のコード例を通じてその使い方を解説します。Streamの主な特徴1. 要素のシーケンスストリームは、特定の型の要素を順番に提供します。要素は必要に応じて取得・計算され、ストリーム自体が要素を保存することはありません。2. データソースストリームは、コレクション(Collections)、