Java REPL (JShell): Interactive Java Programming at Your Fingertips
Discover Java's REPL (JShell), introduced in Java 9, which allows you to run Java code snippets interactively without the need for compiling a full program. Learn how JShell simplifies testing, evaluating expressions, and experimenting with code directly in the console.
Java - REPL (JShell)
Introduction to REPL (JShell)
REPL stands for Read Evaluate Print Loop. JShell, introduced in Java 9, is an interactive console that allows you to run arbitrary snippets of Java code directly in the console without needing to save and compile a Java file. This feature is particularly useful for quickly testing code, such as evaluating regular expressions or checking the formatting of strings and dates.
JShell reads each line you enter, evaluates it, prints the result, and then gets ready for the next input.
Advantages of Using JShell
JShell offers several advantages for developers:
- No editor is required to write a Java program; JShell itself functions as the editor and executes the Java code.
- There is no need to save a Java file, compile, and execute in a cycle. You can test code directly in JShell without saving.
- Compilation is unnecessary before executing the code.
- If any compile-time or runtime error occurs, you can start fresh without any hassle.
Running JShell
To start JShell, open the command prompt and type:
Syntax
D:\test>jshell
| Welcome to JShell -- Version 20.0.2
| For an introduction type: /help intro
Output
Welcome to JShell -- Version 20.0.2
For an introduction type: /help intro
Create and Invoke Method in JShell
Here's an example of a simple "Hello World" program in JShell. We will create a method greet()
that prints "Hello World!" and then invoke it.
Syntax
jshell> void greet() { System.out.println("Hello World!");}
| created method greet()
jshell> greet()
Output
Hello World!
Creating Variables in JShell
The following snippet demonstrates how to create variables in JShell. The semicolon is optional. You can also create objects. If a variable is not initialized, it gets a default value or null if it is an object reference. Once a variable is created, you can use it as shown in the last statement where we print the value of a string variable.
Syntax
jshell> int i = 20
i ==> 20
jshell> String name = "John";
name ==> "John"
jshell> Date date = new Date()
date ==> Fri Feb 02 14:52:49 IST 2024
jshell> long l
l ==> 0
jshell> List list
list ==> null
jshell> name
name ==> "John"
Output
20
"John"
Fri Feb 02 14:52:49 IST 2024
0
null
"John"
Evaluate Expression in JShell
The snippet below shows how to evaluate an expression in JShell. Here, we use a statement that returns a formatted string. JShell automatically creates a String variable $9
and assigns it the result.
Syntax
jshell> String.format("%d pages read.", 5);
$9 ==> "5 pages read."
jshell> $9
Output
"5 pages read."
JShell Built-In Commands
JShell provides various commands to list created variables, methods, and imports. Some important JShell commands include:
/drop
– Drops code snippets identified by name, ID, or ID range./edit
– Opens an editor./env
– Displays the environment settings./exit
– Exits from the tool./history
– Displays the history of commands./help
– Displays help for commands./imports
– Displays current active imports.
Example: Demonstrating /help Command
You can view all commands using the /help
option.
Syntax
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
| /drop
| delete a source entry
| /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 type declarations
Example: Demonstrating /vars Command
In the following example, we use the /vars
command to print the variables declared during the session.
Syntax
C:\Users\John>jshell
| Welcome to JShell -- Version 20.0.2
| For an introduction type: /help intro
jshell> int i = 20
i ==> 20
jshell> String name="John"
name ==> "John"
jshell> /vars
| int i = 20
| String name = "John"
Example: Demonstrating /imports Command
You can use the /imports
command to check the imports available in JShell, as shown below:
Syntax
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.*
Exiting JShell
We can use the /exit
command to exit JShell, as shown below:
Syntax
jshell> /exit
| Goodbye
Output
Goodbye