Sub topic 4
How JShell reads input: snippets and optional semicolons
This chapter explains how JShell splits what you type into units, how it decides whether a unit is finished, and when it supplies the semicolon for you. It assumes you can start JShell, evaluate expressions, and declare variables, as covered earlier. It also assumes you know basic Java syntax.
What a snippet is
JShell does not compile a whole file. It reads input and cuts it into snippets. A snippet is one complete piece of Java that JShell can evaluate on its own. Every snippet is one of these kinds:
| Kind | Example | What JShell reports |
|---|---|---|
| Expression | `6 * 7` | `$1 ==> 42` |
| Assignment | `count = 3` | `count ==> 3` |
| Variable declaration | `int count = 0` | `count ==> 0` |
| Statement | `System.out.println("hi")` | The program output only |
| Method declaration | `int sq(int x) { return x * x; }` | `created method sq(int)` |
| Type declaration | `record Point(int x, int y) {}` | `created record Point` |
| Import | `import java.time.*` | Nothing in the default feedback mode |
A snippet is the unit that JShell numbers, lists with /list, and removes with /drop. It is also the unit that can be replaced when you declare the same name again. Imports are snippets too.
Optional semicolons
In a normal Java file, nearly every statement ends with ;. In JShell, a snippet that is otherwise complete does not need its final semicolon. JShell adds it for you.
jshell> int count = 5
count ==> 5
jshell> count * 2
$2 ==> 10
jshell> System.out.println("count is " + count)
count is 5The first line has no semicolon, and JShell accepts it. Typing count * 2; with a semicolon would also work.
This rule applies only to the end of a snippet. Inside a snippet, Java's normal rules still apply. A statement inside a method body, a block, or a class body needs its semicolon.
jshell> int twice(int x) { return x * 2 }
| Error:
| ';' expected
| (source line and a caret marker follow)The method declaration as a whole needs no trailing semicolon. The return statement inside it does.
Incomplete input and the continuation prompt
JShell checks whether what you typed is a complete snippet. If the input is not complete, for example because a brace or parenthesis is still open, JShell waits for more. It shows a continuation prompt instead of an error.
jshell> int add(int a, int b) {
...> return a + b;
...> }
| created method add(int,int)The same happens with a loop whose body is a block:
jshell> for (int i = 0; i < 3; i++) {
...> System.out.println("i = " + i);
...> }
i = 0
i = 1
i = 2If you realize you made a mistake in an unfinished snippet, press Ctrl+C. This discards the pending input and returns you to the normal prompt. The session state is not changed.
Several snippets on one line
JShell can split a single line into several snippets when each part is complete. The semicolons between them mark the boundaries.
jshell> int a = 1; int b = a + 1
a ==> 1
b ==> 2This creates two snippets, and /list shows them with separate numbers. The final semicolon is optional, as before. Without the separator, the line is a syntax error:
jshell> int a = 1 int b = 2
| Error:
| ';' expectedStatements do not leave variables behind
Only a top-level variable declaration creates a session variable. A variable declared inside a larger statement exists only inside that statement.
jshell> for (int i = 0; i < 2; i++) { int doubled = i * 2; }
jshell> doubled
| Error:
| cannot find symbol
| symbol: variable doubledThe whole for loop is one statement snippet. The variables i and doubled belong to it, as they would in compiled Java. To keep a value, declare the variable first, then assign to it inside the loop:
jshell> int last = 0
last ==> 0
jshell> for (int i = 0; i < 4; i++) { last = i * 2; }
jshell> last
last ==> 6Modifiers at the top level
Because snippets are not members of a class you wrote, access and storage modifiers make little sense on them. If you add public or static to a top-level declaration, JShell accepts the snippet but ignores the modifier and prints a warning.
jshell> public int triple(int x) { return x * 3; }
| Warning:
| Modifier 'public' not permitted in top-level declarations, ignored
| (source line and a caret marker follow)
| created method triple(int)The warning is harmless, but it is a sign that you are writing a compiled-style declaration. You can drop the modifier.
Rejected snippets
When a snippet fails to compile, JShell prints the error and does not add the snippet to the session. A plain /list does not show it. The command /list -all also shows startup snippets, which have ids starting with s, and rejected snippets, which have ids starting with e.
A snippet that compiles but throws an exception at run time is different. The exception is reported, but the declaration itself is already part of the session. The earlier chapter on exceptions covers this in detail.
Common errors
- Stuck at
...>. A brace, parenthesis, or text block is still open. Close it, or press Ctrl+C to discard the input. ';' expectedon a one-line method. Statements inside the braces need semicolons. Only the end of the whole snippet is optional.- Missing separator between two snippets on one line. Put
;between them. - Variable not found after a loop. The variable was local to the loop. Declare it before the loop.
- Pasting code that has a
/at the start of a line. A line that begins with a slash is read as a command. A comment such as// noteor/* note */is not mistaken for one, but a bare/followed by a word is.
Checking your understanding
Exercise
You type int total = 0; for (int n = 1; n <= 3; n++) { total += n } on one line and get an error. What is wrong, and what is the corrected line?
Show answer
The statement inside the braces, total += n, is missing its semicolon. Only the final semicolon of a whole snippet is optional. The corrected line is int total = 0; for (int n = 1; n <= 3; n++) { total += n; }. JShell splits it into two snippets, and total then evaluates to 6.
Exercise
After running for (int i = 0; i < 3; i++) { int sq = i * i; }, you type sq and get "cannot find symbol". Why, and how do you keep the last square?
Show answer
The loop is a single statement snippet, so sq is local to its body and is gone when the loop ends. Declare the variable first, for example int sq = 0, and then assign to it in the loop with sq = i * i;.