skip to content

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