jshell: Java without a file
jshell is a read-eval-print loop that ships with the JDK. It reads one Java snippet at a time, compiles it in memory, runs it and prints the value. There is no class, no main and no file to save, which makes it the fastest way to answer a question about the language: what does this expression give, what does this method return, what does this exception say.
Starting jshell
Run jshell with no argument. It prints its version, and the prompt jshell> waits for a line. Each line is a snippet: a declaration, an expression or a statement. A declaration prints the variable and its value, an expression prints the value as $3, the third snippet of the session.
jshell
| Welcome to JShell -- Version 21.0.13.1
| For an introduction type: /help intro
jshell> int a = 6;
a ==> 6
jshell> int b = 7;
b ==> 7
jshell> a * b
$3 ==> 42
jshell> String name = "Java";
name ==> "Java"
jshell> name.toUpperCase()
$5 ==> "JAVA"
jshell> /exit
| Goodbye
The semicolon is optional in jshell, and a declaration still needs its type: the REPL is a compiler, not a guesser. The $3 and $5 names are variables the REPL creates for expressions that are not assigned to one, so an expression you typed can be used by the next line.
Variables and expressions
A snippet sees every variable and method declared before it, and the state survives between lines. A method is a snippet too, and so is a class. The session below declares a method, calls it, changes a variable and calls it again:
int n = 5;
int square(int x) { return x * x; }
square(n)
n = 9;
square(n)
n ==> 5
| created method square(int)
$3 ==> 25
n ==> 9
$5 ==> 81
The output above is what the same lines print in a session started fresh: created method square(int) is jshell confirming a method, and the two values are the calls. A line that fails to compile does not end the session: jshell prints the error and keeps the state it had before the line.
/vars and /methods
A command that starts with a slash is a jshell command, not Java. /vars lists the variables of the session with their types and values, and /methods lists the methods. Both are the answer to "what have I already declared", which is the question a long session raises.
/vars
/methods
/exit
jshell> /vars
| int a = 6
| int b = 7
| int $3 = 42
| String name = "Java"
| String $5 = "JAVA"
jshell> /methods
jshell> /exit
| Goodbye
The first block is the answer to /vars in a session that declared those five variables: the three declared in the session and the two the REPL created for the expressions. /methods prints nothing here because that session declared none; the prompt after it is not a method.
/save and /open
/save writes the snippets of the session to a file, and /open reads a file of snippets back into a session. The file is plain Java, one snippet per line, and it is not a class: there is no main in it and it cannot be compiled by javac on its own.
/save session.jsh
/exit
jshell> /save session.jsh
jshell> /exit
| Goodbye
The file the command wrote holds the two snippets of that session:
int a = 6;
a * a
That listing is a file jshell writes, not a program: it has no class and no main, and /open session.jsh replays it into a new session. The extensions .jsh and .java are both accepted by /open; the conventional one is .jsh.
When a file is better
jshell is a tool for questions, not for programs. It keeps no file, it runs in the unnamed package, and it has no build step to repeat on another machine. A program that will be read, tested and changed is a source file with a class, a package and a build command, which is what every other lesson of this course writes.
The same method in a file looks like this, and it is the shape the rest of the course uses:
The file Square.java:
public class Square {
public static void main(String[] args) {
System.out.println(square(7));
}
static int square(int n) {
return n * n;
}
}
javac -Xlint:all Square.java
java Square
49
The method is the same square(int) that jshell created from one line. The difference is everything around it: a named class, a main the JVM can call, a file another person can open, and a javac command whose result is a class file.
Exercises
- Start jshell and type 2 + 2 * 3. Which value does it print, and which variable name?
- Declare a method int twice(int x) in jshell, call it, and list the methods with /methods.
- Save a session with /save, exit, start a new jshell and open the file with /open. Are the variables back?
- Try to compile a saved .jsh file with javac. What does the compiler say, and why?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is jshell?
Answer
A read-eval-print loop that runs Java snippets without a file
jshell compiles and runs one snippet at a time and keeps the state between them.
-
What does
/varsprint?Answer
The variables of the session with their types and values
/vars is a jshell command that lists the variables declared so far in the session.
-
What does
/save session.jshwrite?Answer
The snippets of the session, one per line
/save writes the snippets so that /open can replay them; the file is not a compilable class.
-
What does this program print?
public class Q1 { public static void main(String[] args) { System.out.println(2 + 2 * 3); } }Answer
8
Multiplication binds tighter than addition, so the value is 2 + 6.
-
What does this program print?
public class Q2 { public static void main(String[] args) { int n = 5; System.out.println(square(n)); } static int square(int x) { return x * x; } }Answer
25
The method returns 5 times 5, and println writes the result.
-
Which line is a whole Java program that javac can compile?
Answer
A class with a main method
javac compiles classes; snippets and saved .jsh files need jshell.
-
What does jshell do with a snippet that does not compile?
Answer
It prints the error and keeps the earlier state
An error is reported as a snippet error; the variables and methods declared before it stay.
-
What does this program print?
public class Q3 { public static void main(String[] args) { String name = "Java"; System.out.println(name.toUpperCase()); } }Answer
JAVA
toUpperCase returns a new string with every letter in upper case.
-
Which commands are jshell commands rather than Java?
Answer
/vars, /methods and /exit
A leading slash marks a jshell command; the rest of the input is Java.
-
Why does every other lesson use a file instead of jshell?
Answer
A file has a class, a main, a package and a build command to repeat
jshell is for questions; a program that is read, tested and shipped is a source file.
The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.
Article author: Arthur Isaev