Java Reference ยท Language Features by Version
JShell, the Java REPL
Reference for JShell, Java's interactive REPL: start it, evaluate expressions and declarations, use commands like /list and /vars, and drive it from code.
JShell is Java's read-evaluate-print loop, added in Java 9. You type a Java expression, statement or declaration, and it is evaluated immediately and the result is printed. There is no class, no main and no ; at the end of a simple expression. It is ideal for trying out an API, testing a regular expression, checking what Math.round(-2.5) returns, or teaching.
A terminal session cannot run in the browser, so the session transcript below is read-only. The runnable examples use the jdk.jshell API, the same engine the tool is built on, to evaluate snippets from a program and show how results and errors are reported.
$ jshell | Welcome to JShell -- Version 25 jshell> 6 * 7 $1 ==> 42 jshell> String greet(String n) { return "Hello, " + n; } | created method greet(String) jshell> greet("Ada") $3 ==> "Hello, Ada" jshell> /vars | int $1 = 42 | String $3 = "Hello, Ada" jshell> /exit
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Useful commands
| Command | Does |
|---|---|
/help |
Lists commands; /help intro gives a tour. |
/vars, /methods, /types |
Shows declared variables, methods and types. |
/list |
Shows every snippet entered (/list -all includes failed ones). |
/edit |
Opens snippets in an editor, /drop name removes one. |
/imports |
Shows the imports in effect. JShell imports java.io, java.math, java.net, java.nio.file, java.util, java.util.concurrent, java.util.function, java.util.prefs, java.util.regex and java.util.stream by default. |
/open file.jsh, /save file.jsh |
Loads or stores a script of snippets. |
/reset, /reload |
Clears state or replays history. |
/set feedback verbose |
Controls how much JShell prints. |
/exit |
Quits (or press Ctrl+D). |
Press Tab for completion and Shift+Tab then v to turn an expression into a variable declaration. You can run a script non-interactively with jshell script.jsh.
Usage notes
- Intermediate results get names like
$1,$2; give your own withvar total = 6 * 7. - Redeclaring a variable, method or class replaces the old one, and dependent snippets are re-evaluated. This is different from a real
.javafile, where duplicates are errors. - Checked exceptions need no
try/catchfor a single statement; JShell wraps snippets for you. - Forward references are allowed for methods and classes, as the example shows.
- Modifiers
public,privateandstaticon top-level snippets are ignored with a warning, since everything lives in an implicit scope. - A JShell session is single-threaded and stateful: snippets run in a remote JVM by default, so a crash in your code does not end the tool.
- It is a learning aid, not a replacement for tests or a build.