Fundamentals 11 min read

Master Java’s Interactive Shell: Quick JShell Guide for Rapid Prototyping

This article introduces Java's interactive programming environment, explains how to launch JShell, demonstrates calculations, variable and function definitions, class creation, and covers essential commands like /help, /list, /edit, /drop, /save, /open, /reset, /imports, /types, and /exit for efficient REPL usage.

Programmer DD
Programmer DD
Programmer DD
Master Java’s Interactive Shell: Quick JShell Guide for Rapid Prototyping

What is an interactive programming environment? In such an environment each line of code you enter receives immediate feedback, making it ideal for quick verification and simple calculations, though not suited for complex engineering tasks.

JShell Quick Start

Open a terminal and run jshell:

➜  ~ jshell
|  Welcome to JShell -- version 9
|  Type /help intro for a brief overview.

jshell>

Running calculations

jshell> 1+2
$1 ==> 3

Defining variables

jshell> int a=1, b=2;
a ==> 1
b ==> 2

jshell> int c = a + b
c ==> 3

Defining functions

jshell> int sum(int a, int b){
    return a + b;
}
|  Created method sum(int,int)

jshell> int c = sum(1, 2)
c ==> 3

Defining classes

jshell> public class Calculate {
    public int a;
    public int b;
    public int sum(){
        return a + b;
    }
}
|  Created class Calculate

jshell> Calculate c = new Calculate();
c ==> Calculate@4fca772d

jshell> c.a = 1
$9 ==> 1

jshell> c.b = 2
$10 ==> 2

jshell> c.sum()
$11 ==> 3

You can also use Java 16 records for simpler data carriers.

Viewing commands: /help

Use /help to list available commands such as /list, /edit, /drop, /save, /open, /vars, /methods, /imports, /types, /reset, /exit, etc.

jshell> /help
| Type a Java expression, statement, or declaration.
| Or type one of the following commands:
| /list [name|id|-all|-start]
| /edit <name|id>
| /drop <name|id>
| /save [-all|-history|-start] <file>
| /open <file>
| /vars [name|id|-all|-start]
| /methods [name|id|-all|-start]
| /imports
| /types
| /reset [-class-path <path>] ...
| /exit [integer-expression-snippet]
| ...

Viewing defined methods: /methods

jshell> /methods
|    int sum(int,int)

Viewing defined variables: /vars

jshell> /vars
|    int $1 = 3
|    int b = 2
|    Calculate c = Calculate@4fca772d
|    int $9 = 1
|    int $10 = 2
|    int $11 = 3

Listing source entries: /list

jshell> /list

   1 : 1+2
   3 : int a=1, b=2;
   5 : int sum(int a, int b){
           return a + b;
       }
   7 : public class Calculate {
           public int a;
           public int b;
           public int sum(){
               return a + b;
           }
       }
   8 : Calculate c = new Calculate();
   9 : c.a = 1
  10 : c.b = 2
  11 : c.sum()
  12 : Map a = new HashMap();

Editing source entries: /edit

Use /edit 7 to modify entry 7; an editor window appears.

Dropping source entries: /drop

Use /drop 12 to delete the variable defined at entry 12.

jshell> /drop 12
|  Deleted variable a

Saving to a file: /save

Use /save aaa.txt to write the current session to a file.

Opening a file: /open

Use /open aaa.txt to reload a previously saved session.

Resetting JShell: /reset

Use /reset to clear all previous snippets.

jshell> /reset
|  Resetting state.

Viewing imports: /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.*

Viewing defined types: /types

jshell> /types
|    class Calculate

Exiting JShell: /exit

jshell> /exit
| Goodbye
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javacommand-lineJShellJava 9replinteractive programming
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.