Java Statements and Expressions
jshell provides a direct command-line way to try Java code.
Why Try Java Code Directly
When you want to check how a small piece of Java behaves, the usual route is to create a source file, write a main method, compile it, and run it. That process is reasonable for a full program, but it can feel like far too much preparation for a single line you want to test. jshell exists for this situation. It provides a command-line setting in which you can enter Java code directly and see what happens.
jshell is a standard part of the JDK after Java 8. If your setup can already use the javac and java commands, it can also use jshell. Nothing extra needs to be installed to follow this article beyond the JDK you already use for Java work.
Starting a jshell Session
jshell is started from the command line. Open a terminal, type jshell, and press Enter. The session begins and shows a prompt where you can type Java. Each entry you make at the prompt is handled by jshell on its own, so you can build up a sequence of small experiments one line at a time.
- 1Open a terminal or command prompt on a machine where the JDK is installed.
- 2Type jshell and press Enter.
- 3Wait for the jshell prompt to appear, then type a Java statement or expression.
- 4Press Enter after each entry and read jshell's response before typing the next one.
- 5When you are finished, type /exit to leave the session.
Statements Versus Expressions
A statement is a piece of Java that performs an action. jshell executes a statement. An expression is a piece of Java that produces a value. jshell prints the value of an expression.
This difference is the key to reading jshell's responses. When you enter a statement, you are asking jshell to carry out an action, such as declaring a variable and giving it a starting value. When you enter an expression, you are asking jshell to compute something, and the computed value comes back to you on screen. The diagram below shows the two paths side by side.
Predicting jshell's Response
Before reading the output, try to predict it. Given the distinction above, ask yourself whether each entry is a statement or an expression, and what jshell should therefore do with it. The following short session uses one entry of each kind.
x ==> 5
$2 ==> 5
$3 ==> 20- int x = 5; is a statement: it declares a variable and gives it a value
- 2 + 3 is an expression: its computed value is printed
- x * 4 is an expression that uses the variable x from the earlier entry
What do you think happens?
In the session above, what does jshell print after you enter 2 + 3?
Reveal answer
A computed value, shown with a $ name and an arrow, here $2 ==> 5
2 + 3 is an expression. jshell prints the value an expression produces, so it shows the result instead of staying silent.
Tracing the session line by line
Follow what happens to the variable x across the three entries in the session.
- 1Entry one: int x = 5;
This is a statement. jshell executes it, so x now exists with the value 5.
- 2Entry two: 2 + 3
This is an expression that does not involve x. jshell prints its value, 5, and gives that value the name $2.
- 3Entry three: x * 4
This expression refers to x from entry one. Because x holds 5, the result is 20, which jshell prints as $3.
The later entry depends on the earlier statement, so the value printed for x * 4 is only correct because the statement ran first.
What Happens at Each Prompt
jshell works in a repeating cycle. You type an entry, jshell evaluates it, jshell prints a response when there is one, and then the prompt returns so you can enter the next line. The diagram below traces that cycle for one entry. Each pass through the loop handles exactly one thing you typed.
Using /help and /exit
jshell accepts two kinds of input. Java code is the first kind: statements and expressions that jshell executes or evaluates. Slash commands are the second kind, and they control the session itself rather than running Java. A slash command begins with a forward slash, which tells jshell that the line is not Java code.
The /help command supports learning. Type /help at the prompt to see the commands jshell provides, and use it whenever you are unsure what a command does. The /exit command ends the session and returns you to the terminal. Both commands are part of how jshell supports learning and session control.
Lists the available jshell commands, then ends the session and returns to the terminal prompt.- /help asks jshell to describe its commands
- /exit leaves the jshell session
Finding the First Unexpected Result
A useful habit is to find the first point at which the observed result differs from the expected result. Short jshell sessions make this easy, because you can check each entered line and its response one step at a time. If the value from an expression is not what you expected, look back at the earlier statement that supplied the value that expression used.
If a result surprises you, do not immediately rewrite the whole experiment. Compare your prediction with jshell's response after each entry. The first response that differs from your prediction is the best place to investigate.
Mistakes Beginners Make in jshell
Common mistake Expecting a silent result from every entry
What it looks likeTyping 2 + 3 and waiting for an action to occur
Why it fails2 + 3 is an expression, and jshell prints the values of expressions.
Do this insteadRead the printed value for expressions, and expect execution without a printed value only for statements that perform an action.
Common mistake Rewriting the whole experiment after one surprise
Why it failsA single unexpected line can be found quickly, and rewriting everything hides where the difference began.
Do this insteadCompare each response with your prediction and investigate the first line that differs.
Common mistake Typing slash commands inside Java code
What it looks likeWriting int a = 1; /exit on one line
Why it failsSlash commands control the session and are not part of Java statements.
Do this insteadPut /help or /exit on its own line at the prompt.
Practice Session
Start jshell and enter these three lines one at a time: int a = 7; then a + 3 then a * a. Before each entry, predict whether jshell will execute it or print a value. Compare your predictions with the responses. When you finish, type /help to look at the available commands and then type /exit.
Hints
- int a = 7; is a statement, so look for it to be executed rather than printed as a computed value.
- a + 3 and a * a are expressions, so jshell should print their values.
- If a response differs from your prediction, check the earlier line that supplied the value.
Key Takeaways
- jshell is a command-line tool, included in the JDK after Java 8, for entering and trying Java code directly without writing a full program.
- A statement performs an action and jshell executes it, while an expression produces a value and jshell prints that value.
- The /help command helps you learn jshell's commands, and /exit ends the session; slash commands are separate from Java code.
- When an experiment gives an unexpected result, compare each response with your prediction and investigate the first line that differs.