JShell Tutorial: Java's Interactive REPL for Quick Experiments
JShell lets you type Java expressions and see results instantly, with no class, no main method and no compile step. Learn snippets, $ variables, tab completion, methods and records, the most useful slash commands, and how to use JShell to learn faster.
kvi5hnuCode Sprout team member9 min readOn this page
Writing a complete program just to check what "Java".substring(1, 3) returns is slow: create a file, write a class and a main method, compile, run. JShell, included in every JDK since Java 9, removes all of that. You type a piece of Java, press Enter, and immediately see the result.
JShell is a REPL, short for Read–Evaluate–Print Loop:

- Read a snippet you type.
- Evaluate it: JShell compiles and runs it behind the scenes.
- Print the result.
- Loop back and wait for the next input.
This makes JShell ideal for learning. Whenever this course introduces a method or operator and you wonder "what happens if…?", JShell gives you the answer in seconds.
Starting and Stopping JShell
Open a terminal and type:
jshell
| Welcome to JShell -- Version 25.0.1
| For an introduction type: /help intro
jshell>
jshell> is the prompt: JShell is waiting for input. To quit, type /exit (or press Ctrl+D on macOS/Linux).
If jshell isn't found, the JDK's bin folder isn't on your PATH; see the installation article. Most IDEs also have a built-in JShell console (in IntelliJ IDEA: Tools → JShell Console).
Expressions and the $ Variables
Type an expression, a piece of code that produces a value:
jshell> "Core Java".length()
$1 ==> 9
JShell evaluated the expression and printed the result, 9, the number of characters in "Core Java" (the space counts). Notice:
- You didn't write
System.out.println. JShell prints the value of every expression automatically. - You didn't need a semicolon. JShell adds it for single statements.
- The result was saved in an automatically created variable named
$1.
You can use $1 in later snippets:
jshell> 5 * $1 - 3
$2 ==> 42
Each expression result gets the next number: $1, $2, $3, and so on. The number is really the snippet's ID, so it counts every snippet you've entered. The $ numbers in the examples below jump around for that reason, and yours will differ depending on what you typed before.
Declaring Your Own Variables
For values you'll use repeatedly, choose a meaningful name:
jshell> var answer = 6 * 7
answer ==> 42
jshell> answer = answer + 1
answer ==> 43
jshell> String s = "hello"
s ==> "hello"
jshell> s.toUpperCase()
$9 ==> "HELLO"
var lets Java infer the type from the value (here int), while String s states it explicitly. Both are covered properly in the variables article. Notice that string results are printed with quotes, so you can tell the string "42" from the number 42.
JShell also displays arrays and objects readably:
jshell> int[] a = {3, 1, 2}
a ==> int[3] { 3, 1, 2 }
jshell> Arrays.sort(a)
jshell> a
a ==> int[3] { 1, 2, 3 }
jshell> List.of(1, 2, 3)
$19 ==> [1, 2, 3]
Arrays.sort(a) doesn't produce a value (its return type is void), so nothing is printed. It sorts the array in place, which the next snippet confirms.
Tab Completion: Discover the Library
Type Math. and press Tab. Because there are many possible completions, JShell asks you to press Tab once more, then lists everything you can use from the Math class:
jshell> Math.
E IEEEremainder( PI
TAU abs( absExact(
acos( addExact( asin(
atan( atan2( cbrt(
ceil( ceilDiv( clamp(
...
Type l and press Tab again. The list narrows to methods starting with "l":
jshell> Math.log
log( log10( log1p(
Finish the call by hand:
jshell> Math.log10(0.001)
$5 ==> -3.0
(log₁₀ of 0.001 is −3, because 10⁻³ = 0.001.)
Tab completion works for variable names, classes, methods and even file paths in commands. It's a fast way to explore a class you've never used.
Methods, Records and Classes
You can declare methods directly at the prompt, with no class around them:
jshell> int square(int x) { return x * x; }
| created method square(int)
jshell> square(12)
$7 ==> 144
JShell even accepts forward references: a method may call another method that doesn't exist yet. JShell warns you and lets you define the missing piece later:
jshell> double halve(double v) { return twice(v) / 4; }
| created method halve(double), however, it cannot be invoked until method twice(double) is declared
jshell> double twice(double v) { return 2 * v; }
| created method twice(double)
jshell> halve(10)
$18 ==> 5.0
Records, classes, interfaces and enums work too:
jshell> record Point(int x, int y) {}
| created record Point
jshell> new Point(1, 2)
$11 ==> Point[x=1, y=2]
To replace a definition, just type a new one with the same name. JShell replaces the old one and reports | modified method square(int) (or replaced method square(int) when the change also affects other snippets, for example a new return type).
Multi-line input works naturally: if you press Enter in the middle of a block, JShell shows a continuation prompt ...> until the braces are balanced.
Errors and Exceptions
Mistakes don't end the session. A compile error is explained, and an exception at runtime is reported with its location (the snippet number):
jshell> 10 / 0
| Exception java.lang.ArithmeticException: / by zero
| at (#15:1)
Here #15:1 means snippet 15, line 1. Fix the snippet and try again. Nothing is lost.
Default Imports
To make experimenting convenient, JShell imports classes for you. List the imports with /imports:
jshell> /imports
| import java.base
Since Java 25, JShell starts with import module java.base; (module imports, JEP 511), so every public class in the java.base module, from java.util, java.io, java.time, java.math, java.nio.file and more, works without an import. That's why Arrays.sort and List.of worked above, and why LocalDate.now() works too. Older JShells (Java 9–24) imported only a fixed list of packages (java.io, java.math, java.net, java.nio.file, java.util and a few of its subpackages), so there you'd still type import java.time.*. Classes from other modules, such as java.sql or java.net.http, always need an explicit import.
The Most Useful Commands
Commands start with a slash / to distinguish them from Java code. You can abbreviate them as long as the abbreviation is unique (for example /v for /vars).
| Command | What it does |
|---|---|
/help | Lists commands; /help intro gives a short tutorial; /help /list explains one command |
/vars | Lists declared variables with types and current values |
/methods | Lists declared methods |
/types | Lists declared classes, interfaces, records and enums |
/imports | Lists active imports |
/list | Shows the snippets you entered, numbered |
/edit | Opens a snippet (or all snippets) in an editor window for multi-line editing |
/drop name | Removes a variable, method or type |
/1, /! | Re-runs snippet 1; re-runs the last snippet |
/save file.jsh | Saves your snippets to a file |
/open file.jsh | Loads and runs snippets from a file (or a .java file) |
/reset | Starts over with a clean state |
/history | Shows everything you typed, including commands |
/set feedback verbose | Shows more detail, such as the type of each result |
/exit | Quits JShell |
For example, after a few of the snippets above (your list will contain every $ variable you created):
jshell> /vars
| int $1 = 9
| int $2 = 42
| int answer = 43
| double $5 = -3.0
| String s = "hello"
| int[] a = int[3] { 1, 2, 3 }
jshell> /methods
| int square(int)
| double halve(double)
| double twice(double)
Useful Start-up Options
| Command | Purpose |
|---|---|
jshell -v | Verbose feedback, showing the type of each result (same as /set feedback verbose) |
jshell --enable-preview | Try preview features of your JDK version |
jshell --class-path mylib.jar | Make classes from a JAR available, for experimenting with a library |
jshell PRINTING | Loads a built-in start-up script that defines shortcuts such as println(...) and printf(...) |
jshell myscript.jsh | Runs the snippets in a script file at start-up |
When to Use JShell, and When Not To
Great for:
- checking what a method returns ("Does
substringinclude the end index?"), - testing small expressions while reading this course,
- exploring a new class with tab completion,
- prototyping a method before putting it into a real program,
- quick calculations.
Not meant for:
- real applications. JShell code isn't organised into proper classes, packages and files, and has no tests or build;
- performance measurements, because JShell adds overhead and the JIT hasn't warmed up;
- programs that need interactive keyboard input (it's possible, but awkward).
Common questions
Can I save my JShell session and load it again?
Yes. /save session.jsh writes the current snippets to a file, and /open session.jsh (or jshell session.jsh) runs them again. /save -history saves everything you typed, including commands and failed snippets.
Can I use my own classes or a JAR in JShell?
Yes. Start it with jshell --class-path lib/mylib.jar:out, or set it inside the session with /env --class-path .... Then import the classes as usual.
Can JShell use preview features?
Yes. Start it with jshell --enable-preview. JShell always uses the language level of the JDK it belongs to, so a Java 27 JShell understands Java 27 syntax.
Why does JShell accept code without semicolons when javac doesn't?
JShell adds the semicolon for a single complete statement or expression as a convenience. Inside multi-line blocks such as method bodies, you still need semicolons, as in normal Java. Code you copy back into a .java file must follow the normal rules.
Should I measure performance in JShell? No. Each snippet is compiled and wrapped behind the scenes, and short runs never give the JIT time to warm up. For real measurements, use a benchmark harness such as JMH on a normal program.
Summary
- JShell is Java's REPL (Read–Evaluate–Print Loop), included in the JDK since Java 9. Start it with
jshell, quit with/exit. - Expressions are evaluated and printed immediately; results are stored in
$1,$2, … for reuse. - You can declare variables, methods, records and classes directly, with no
mainor class needed. Forward references are allowed. - Tab completes names and lists available methods, the fastest way to explore the library.
- Common packages such as
java.utilare imported automatically. - Slash commands (
/vars,/methods,/list,/edit,/save,/open,/reset,/help) manage your session.
Practice Questions
In JShell you type 2 + 3 and then $1 * 10. What is printed each time?
$1 ==> 5, then $2 ==> 50. The first result is stored in $1, and the second expression uses it.
Why don't you need System.out.println to see results in JShell?
JShell automatically prints the value of every expression you enter (that's the "Print" in REPL). println is still useful when you want custom formatting or text mixed with values, and inside methods.
You typed a method that calls another method you haven't written yet. What does JShell do?
It accepts the definition and warns that the method can't be invoked until the missing method is declared. Once you declare the missing method, the first one works. Forward references are allowed.
How can you find out which methods the String class offers without opening the documentation?
Type "abc". (or a String variable name followed by a dot) and press Tab twice. JShell lists all available methods. Type a few more letters and press Tab to narrow the list, and press Tab after an opening parenthesis to see parameters and documentation.
You experimented for an hour and want to keep your snippets. How?
Use /save mywork.jsh to save them to a file. Later, start JShell and run /open mywork.jsh (or start with jshell mywork.jsh) to replay them.
Previous: Using a Java IDE: IntelliJ IDEA, VS Code and Eclipse for Beginners
Next: Your First Java Program Explained: Classes, main, Statements and Comments
Related reading
- Using a Java IDE: IntelliJ IDEA, VS Code and Eclipse for Beginners
- How to Compile and Run Java Programs from the Command Line
- How to Install the Java JDK on Windows, macOS and Linux
- Java Collectors.toMap: Collecting Streams into Maps
- Collecting Java Stream Results: toList, toArray, joining and Summary Statistics

More in Core Java I
Java volatile, Atomics, Deadlocks and ThreadLocal: Safe Sharing Between Threads
Share data between Java threads without bugs: memory visibility and volatile, final fields, AtomicInteger and compareAndSet, LongAdder, lazy holder initialization, safe publication, lock-ordering deadlocks, ThreadLocal, InheritableThreadLocal and ScopedValue.
Java Condition Objects and the synchronized Keyword: await, signalAll, wait and notify
Make threads wait for a condition safely with Condition.await and signalAll, then learn Java's built-in alternative: synchronized methods and blocks, intrinsic locks, wait and notifyAll, choosing lock objects, client-side locking, and the monitor concept.
Java Race Conditions and ReentrantLock: Synchronizing Shared Data
Why unsynchronized threads corrupt shared data, shown step by step with a lost-update race, and how to fix it with ReentrantLock: the lock/try/finally idiom, critical sections, reentrancy and hold counts, tryLock, fair locks, and the mistakes that cause bugs.
Java invokeAll, invokeAny and Fork-Join: Running Groups of Tasks
Coordinate many concurrent tasks in Java: invokeAll to wait for every result, invokeAny to take the first success, timeouts, ExecutorCompletionService for results in completion order, structured concurrency, and the fork-join framework with RecursiveTask, RecursiveAction and work stealing.
Discussion
Sign in to ask a question or share what you learned.