Concepts / Setting Up the Java Development Kit

Setting Up the Java Development Kit

jshell provides a direct command-line way to try Java code.

  • Programming

Trying Java Without a Source File

Suppose you want to check how a small piece of Java behaves. The usual route is to create a source file, write a main program around the code, compile it, and run it. For a single line of experimentation, that preparation can feel like unnecessary overhead. jshell exists for this situation. It provides a command-line setting in which you can enter Java code directly and see what it does.

This article follows one jshell session from start to finish. You will start jshell, learn how it treats statements differently from expressions, watch how a session unfolds one entry at a time, and find out how to get help and leave the session. Along the way, you will see why short, sequential experiments are an effective way to learn and to debug.

Starting a jshell Session

jshell is a standard part of the JDK from Java 8 onward. If your setup can run the javac and java commands, it can also run jshell. To start it, type jshell at the command line and press Enter. jshell then displays its prompt, which is where you enter Java code.

Output
|  Welcome to JShell
jshell>

Everything you type after the jshell> prompt is read by jshell as one entry. Whether that entry is a statement or an expression changes what jshell does with it, and that distinction is the core of the next section.

Statements Versus Expressions

Definition

A statement is an entry that jshell executes. An expression is an entry whose value jshell prints. The two are handled differently, so recognizing which one you have typed tells you what to expect in response.

int total = 2 + 3;entered lineExecuted by jshellstatement path2 + 3entered lineValue 5 printedexpression pathexecutesvalue shown
How does jshell respond differently to a statement such as a declaration or assignment, compared with an expression such as 2 + 3, and where does the value of the expression appear?

The first line in the diagram is a statement: a declaration that gives a name to a value. Its purpose is to make jshell carry out an action. The second line is an expression: 2 + 3 has a value, and jshell shows that value to you. This is the reason the value of an expression appears on screen while a statement does not need to show one in the same way.

java
Output
jshell> 2 + 3
$1 ==> 5
jshell>

What do you think happens?

When you type 2 + 3 at the jshell prompt, what does jshell do with it?

  • It executes the line and prints nothing

  • It prints the value 5

  • It stores 2 and 3 in variables and waits

Reveal answer
Answer:

It prints the value 5

2 + 3 is an expression, and jshell prints the values of expressions. Nothing needs to be assigned for the value to appear.

Tracing a Session One Entry at a Time

A jshell session is a sequence of entries. Each entry gets its own response before the next one is typed. This makes it possible to follow exactly what happened at each point. The diagram below shows the order of events for each entry you type.

Type entryafter jshell> promptRead entryjshell takes the lineExecute or evaluatestatement or expressionPrint expressionvalueexpressions onlyNext jshell> promptready for next entryEnterif expression
What happens in order after I type a line and press Enter: when is it read, evaluated, printed, and when does jshell return to the prompt for the next input?
Worked example

Following a short experiment step by step

Declare a value, then use it in an expression. Predict the response to each entry before reading on.

  1. 1
    Entry 1: a statement

    int total = 2 + 3; is a statement. jshell executes it, which stores the value 5 in total. The line is not an expression, so you are not shown a value from it as the result of this entry.

  2. 2
    Entry 2: an expression that uses the earlier value

    total * 2 is an expression. Its value depends on the statement from Entry 1. jshell evaluates it using the stored value of total and prints the result.

  3. 3
    Entry 3: checking the output

    If the printed value were not 10, you would look back at Entry 1 first, because the expression depends on the value that statement supplied.

Result

The expression total * 2 prints 10, because total holds 5 from the earlier statement.

Output
jshell> int total = 2 + 3;
jshell> total * 2
$2 ==> 10
jshell>

Notice that the session is built from small steps. Each entry depends only on what came before it, so when a result is wrong, you can move back through the session and find the first entry where the response departs from what you expected.

Mistakes When Reading a jshell Session

  • Common mistake

    Expecting a statement to print a value

    What it looks like

    Typing int total = 2 + 3; and looking for the value 5 on screen.

    Why it fails

    A statement is executed rather than having its value printed. The value appears when you type an expression such as total or 2 + 3.

    Do this instead

    Enter an expression, such as total, to see the value.

  • Common mistake

    Rewriting the whole experiment when a result surprises you

    What it looks like

    Deleting every line after a result looks wrong and starting over from scratch.

    Why it fails

    The session is a sequence of small steps, so the problem is usually at one specific point.

    Do this instead

    Compare the expected result with the response after each entry, and stop at the first line whose response differs from your prediction.

  • Common mistake

    Ignoring the earlier statement behind a wrong expression value

    What it looks like

    Seeing 12 printed for total * 2 and rechecking only the expression itself.

    Why it fails

    An expression uses values supplied by earlier statements, so the error may come from the earlier entry.

    Do this instead

    Inspect the earlier statement that provided the value used by the expression.

Getting Help and Leaving the Session

jshell has its own commands, which begin with a slash. These are different from Java code. You do not type them as Java statements or expressions. The /help command supports learning about jshell itself, and the /exit command ends the session and returns you to the command line.

EASY

Start jshell, type 4 * 6 and observe the response. Then type /help to look at what jshell offers, and type /exit to leave the session. Next, start a new session, declare int width = 4; as a statement, then enter width + 6 as an expression. Write down which entry printed a value and which did not.

Hints
  • Expressions are the entries whose values jshell prints.
  • A declaration such as int width = 4; is a statement, so it is executed rather than printed as a value.
  • If width + 6 shows an unexpected result, check the earlier statement first.

Key Takeaways

  • jshell is a command-line setting in which you enter Java code directly, without creating a source file and main program first. It is a standard part of the JDK from Java 8 onward.
  • jshell executes statements and prints the values of expressions, so the kind of entry you type determines what you see in response.
  • Short, sequential experiments make it easier to find the first point where a result departs from your expectation, and you should inspect the earlier statement that supplied any value an expression uses.
  • The /help command helps you learn about jshell, and /exit leaves the session. Slash commands are jshell commands, not Java code.