JShell: Java without the ceremony

A line of Java with no class, no main and no compile step — the fastest way to check what something does.

7 min read🔧 Setting Up Java

For most of Java's life, trying one line meant creating a file, declaring a class, writing a main, compiling and running. JShell removed all of that, and it is the fastest way to answer "what does this actually do".

What it looks like

plaintext
$ jshell
jshell> int x = 2 + 3
x ==> 5
 
jshell> x * 10
$2 ==> 50
 
jshell> "hello".toUpperCase()
$3 ==> "HELLO"
 
jshell> /exit

No class. No main. No semicolon required at the end of a line. Each expression is evaluated and its result printed — and given a name ($2, $3) so you can use it in the next line.

That is the whole idea: Java as a conversation, the way the terminal is a conversation with the machine.

What it is for

Checking what a method does. Rather than reading the documentation for String.split and guessing:

plaintext
jshell> "a,b,,,".split(",").length
$1 ==> 2
 
jshell> "a,b,,,".split(",", -1).length
$2 ==> 5

Two lines, and you now know something that catches people out in production.

Trying an API before committing to it. Import and use it; if it is awkward in JShell it will be awkward in your code.

Settling an argument. "Does Integer.valueOf(128) == Integer.valueOf(128) return true?" is a question with an answer, and it takes eight seconds to get.

Teaching and learning. Every example in this course that prints something is a thing you can paste and watch.

The commands worth knowing

Anything starting with / is JShell itself rather than Java:

CommandDoes
/listshow what you have entered so far
/varsthe variables you have defined
/importswhat is imported (a useful set already is)
/editopen a multi-line editor for something you are building up
/save file.jshwrite your session out
/open file.jshread one back in
/helpall of them
/exitleave

/list is the one you will use. A session drifts, and seeing what you actually typed is often the answer.

What it will not do

It is a scratchpad, not a development environment. It has no project, no dependency management (you can add a classpath with --class-path, and it is fiddly), and no persistence beyond /save.

It is also not how you should learn program structure. A language where everything is a top-level expression is not Java, and a beginner who lives in JShell arrives at their first class confused about why anything needs a main. Use it to check a fact, then go and write the file.

Under the hood: how JShell actually evaluates what you type

Nothing about JShell is a separate, smaller Java. Under the "wraps what you type" line above sits a real mechanism, defined when JShell was introduced (JEP 222): each entry you type is analysed and wrapped into something compilable, then handed to the same Compiler API (javax.tools.JavaCompiler, the programmatic interface javac itself is built on) that a build tool would use. That is why a syntax mistake in JShell produces the same kind of compiler diagnostic a file would — it is going through the same front door.

What makes it feel conversational is what happens to the result. A variable you declare in one entry is not a local variable that vanishes when that "statement" finishes — JShell keeps it as part of the session's persistent state, the way a field on an object persists across separate method calls, which is exactly why x from your first line is still there for your second. And by default your code does not even run inside the JShell process itself: JShell starts a separate JVM to execute your snippets and talks to it over a local connection, specifically so that a snippet which calls System.exit(), overflows the stack, or otherwise takes its process down does not take your whole session down with it.

This mirrors — in miniature — that same idea: a value you set in one step is still visible in the next, because it is being kept as shared state rather than as a local variable that goes out of scope.

java
class Session {
    static int x;
}
Session.x = 5;
System.out.println("x = " + Session.x);
Session.x += 10;
System.out.println("x = " + Session.x);

Try it yourself

Predict the session's state, then check it

Before running the snippet above, write down what you expect both printed lines to be, and why the second line is not simply x = 10.

Why the value from the first line survives

Session.x is a static field, not a local variable — its lifetime is the class's, not any one statement's. The second block does not start from nothing; it reads whatever x currently holds and adds 10 to it. That is the mechanism this section describes in miniature: JShell keeps what you have already typed as persistent state rather than throwing each entry away once it finishes, which is why a variable from three lines ago is still usable now, in a way a local variable inside an ordinary method never would be.

Misconceptions

  • "JShell is a different Java." Same language, same library, same JVM. It wraps what you type in the machinery Java needs and hides it.
  • "Semicolons are optional in Java now." In JShell, at the end of an entry. In a file, no.
  • "It is for beginners." It is quickest for people who already know what they are checking. Beginners get the most from it alongside real files, not instead of them.
  • "A crashing snippet crashes your JShell session." By default it does not — JShell runs your code in a separate process precisely so a System.exit(), an infinite loop the OS eventually kills, or a StackOverflowError in the snippet does not take the tool you are typing into down with it.
  • "Imports work differently in JShell." They are the same imports; JShell simply starts a session with a small default set already active (/imports shows it), which is why some common classes resolve without you typing anything first.

Sources

  • JEP 222: "jshell: The Java Shell (Read-Eval-Print Loop)" — the specification that introduced JShell.
  • The jdk.jshell module documentation (JDK Javadoc).
  • Oracle, "Java Shell User's Guide."
Progress is saved on this device and to your account when signed in.