skip to content

JShell (REPL)

JShell is the JDK's REPL: evaluate expressions and declarations without a class or main method, with commands for listing, editing and saving snippets. Interviewers mention it as the quickest way to check a language behavior you are unsure about.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

5

What is JShell, and why would you use it instead of writing a full class with a main method?

level: juniorimportance: should knowfreq 45%

answer

  1. REPL = Read-Eval-Print Loop
  2. Java 9, launch with `jshell`
  3. No class / no main / no compile step
  4. Evaluate expressions, statements, declarations
  5. Learning + experimentation, not production

basics

~20 s

JShell is Java's interactive shell (a REPL) added in Java 9. You type a line of Java, press Enter, and see the result immediately—no class, no main method, no compile-and-run cycle. It's great for quick experiments and learning.

solid answer

~40 s

JShell is the official Read-Eval-Print Loop for Java, shipped with the JDK since Java 9. You launch it by typing `jshell` at a terminal. It lets you evaluate Java fragments—expressions, statements, and declarations—one at a time and prints the result instantly, with no surrounding class or `main` method required. It exists because Java historically forced even the smallest experiment into the full edit-compile-run cycle (write a class, add `public static void main`, `javac`, `java`). JShell removes that ceremony, so it's ideal for trying an API, checking what a snippet returns, prototyping a regex, or teaching beginners the language without first explaining classes and methods. It is a learning and experimentation tool, not a way to run production code.

go deeper

for a junior

Knows JShell is an interactive Java prompt added in Java 9, launched with jshell, that runs code without a class or main method.

for a middle

Can explain the edit-compile-run cycle it removes and name concrete uses (API testing, learning, prototyping) versus what it is not (production runtime).

for a senior

Frames JShell within Java's broader push to reduce ceremony (alongside the single-file source launcher) and can articulate when a quick REPL beats a scratch test class.

for a principal

Discusses REPL ergonomics in onboarding/teaching, embedding the JShell API in tooling, and trade-offs of interactive experimentation versus reproducible, version-controlled scratch tests.

## What "REPL" means A **REPL** stands for **Read-Eval-Print Loop**. It is an interactive program that repeatedly: **Reads** a line you type, **Evaluates** (runs) it, **Prints** the result, and **Loops** back to wait for the next line. Languages like Python and JavaScript have had REPLs for years (the `python` prompt, the browser console). **JShell** is Java's REPL, introduced in **Java 9** (released September 2017). ## The problem it solves Before JShell, the smallest possible Java program still required a lot of boilerplate (repetitive setup code). To print `2 + 2` you had to: 1. Create a file, e.g. `Demo.java`. 2. Write a class: `public class Demo { ... }`. 3. Add the special entry-point method: `public static void main(String[] args) { ... }`. 4. Compile it with `javac Demo.java`. 5. Run it with `java Demo`. That is five steps to evaluate one expression. For learning the language, testing how an API behaves, or checking a quick calculation, this **edit-compile-run cycle** is heavy and slow. ## What JShell does instead You launch it by typing `jshell` in a terminal (it lives in the JDK's `bin` folder). You get a `jshell>` prompt. Now you can type any Java **fragment** (also called a **snippet**) and press Enter: - An **expression** (a piece of code that produces a value), e.g. `2 + 2` → it prints `$1 ==> 4`. - A **statement** (an instruction), e.g. `System.out.println("hi")`. - A **declaration** (introducing a variable, method, or class), e.g. `int x = 10;`. No class wrapper, no `main`, no separate compile step—JShell compiles and runs each snippet for you behind the scenes. ## Why this matters - **Learning:** Beginners can write real Java from the very first line without understanding classes, `static`, or `main` first. - **Experimentation:** Quickly check what a method returns, how a `Stream` behaves, or whether a regex matches—without creating a throwaway project. - **Prototyping:** Sketch out logic interactively, then copy the working snippets into real code. ## What JShell is NOT It is **not** a way to deploy or run production applications, and it is **not** a replacement for build tools, tests, or IDEs. It is a scratchpad. Think of it as the equivalent of a calculator for Java code—fast feedback for small pieces.

  • What does REPL stand for and what are its four steps?
    Read-Eval-Print Loop: it Reads your input, Evaluates it, Prints the result, then Loops to read the next input.
  • In which Java version did JShell arrive?
    Java 9 (2017).

saying these in an interview costs you the question

  • Saying JShell is a build tool or a way to ship production code
  • Claiming it existed before Java 9
  • Thinking you must still write a class and main method inside JShell
  • Confusing the REPL with the `java SingleFile.java` source-launcher (Java 11)

context

open as a page

What are JShell commands like /list, /vars, /edit, /save, /open, and how do you load or save scripts?

level: middleimportance: should knowfreq 35%

basics

~20 s

JShell commands start with a slash and control the session rather than running Java. For example /list shows your snippets, /vars lists variables, /edit opens an editor, /save file writes snippets to a file, and /open file runs a script of snippets.

open as a page

What kinds of snippets can JShell evaluate, and what are implicit/scratch result variables?

level: middleimportance: should knowfreq 40%

basics

~20 s

JShell accepts expressions, statements, and declarations (variables, methods, classes). When you type an expression with no name, JShell stores its value in an automatically named variable like $1 or $2 so you can reuse the result on the next line.

open as a page

What are forward references in JShell, and how does JShell handle a method that calls something not yet defined?

level: seniorimportance: nice to knowfreq 25%

basics

~20 s

A forward reference is when a method you define refers to a variable, method, or type that doesn't exist yet. JShell accepts the definition, marks it as not-yet-runnable, and makes it work automatically once you define the missing piece.

open as a page

When should you reach for JShell versus a scratch class or unit test, and what are its limits?

level: seniorimportance: nice to knowfreq 22%

basics

~20 s

Use JShell for quick, throwaway experiments: trying an API, checking what a snippet returns, or teaching. For anything you need to keep, repeat reliably, or run with your project's dependencies and build, use a scratch class or a unit test instead.

open as a page