skip to content

What kinds of snippets can JShell evaluate, and what are implicit/scratch result variables?

level: middleimportance: should knowfreq 40%

answer

  1. Three snippet kinds: expression, statement, declaration
  2. Bare expression → implicit `$N` scratch variable
  3. `==>` shows value + auto name
  4. Redefine a name just by typing it again
  5. Semicolon optional for a single expression

basics

~20 s

JShell accepts expressions, statements, and declarations (variables, methods, classes). When you type an expression with no name, JShell stores its value in an automatically named variable like $1 or $2 so you can reuse the result on the next line.

solid answer

~40 s

JShell evaluates three categories of snippet: **expressions** (e.g. `3 * 7`), **statements** (e.g. `for (int i=0;i<3;i++) System.out.println(i);`), and **declarations** of variables, methods, or even classes. When you enter a bare expression that produces a value but isn't assigned to a name, JShell captures it in an **implicit (scratch) result variable** named `$N`—`$1`, `$2`, and so on. JShell echoes something like `$1 ==> 21`, and you can then reference `$1` in later snippets. This lets you build up a session incrementally. You can also redefine a variable, method, or class simply by typing a new declaration with the same name; JShell replaces the old definition and re-evaluates anything that depended on it. Semicolons are optional for a single expression but required to separate multiple statements.

go deeper

for a junior

Knows you can type expressions and that JShell shows results like $1 ==> 4.

for a middle

Can name all three snippet kinds, explain implicit scratch variables $N, and use redefinition to iterate on a method.

for a senior

Understands the persistent-session model—named declarations live on, dependents re-evaluate on redefinition—and uses scratch variables to compose multi-step explorations cleanly.

for a principal

Reasons about JShell's snippet/state model when scripting or driving it programmatically (e.g. feeding snippets via the JShell API) and the implications of replacement semantics for reproducibility.

## Snippets: the unit of work in JShell A **snippet** is any fragment of Java code you type at the `jshell>` prompt. JShell recognizes three broad kinds: ### 1. Expressions An **expression** is code that computes a value. Examples: `2 + 2`, `"abc".length()`, `Math.sqrt(16)`. When you type an expression, JShell evaluates it and shows the result. ### 2. Statements A **statement** is an instruction that does something but does not by itself yield a value to display—e.g. a loop, an `if`, or a call like `System.out.println("hi")`. JShell runs it and shows any side effect (like printed output). ### 3. Declarations A **declaration** introduces a named thing into the session: - a **variable**: `int count = 5;` - a **method**: `int square(int n) { return n*n; }` - a **type**: `class Point { int x, y; }` Declared names persist for the rest of the session and can be used in later snippets. ## Implicit (scratch) result variables When you type a **bare expression**—one whose value you did not assign to a named variable—JShell still needs somewhere to keep that value so you can refer to it again. It creates an **implicit result variable** (also called a **scratch variable**) named `$` followed by a number: `$1`, `$2`, `$3`, … Example session: ``` jshell> 3 * 7 $1 ==> 21 jshell> $1 + 1 $2 ==> 22 ``` The `==>` arrow is JShell telling you the snippet's value and the auto-generated name. The numbering increments across the session. These scratch variables are ordinary variables—you can read them, pass them to methods, etc.—they just have machine-chosen names. ## Redefinition A powerful REPL behavior: you can **redefine** any declaration just by typing it again with the same name. If you defined `int x = 1;` and later type `int x = 99;`, JShell updates `x` to the new definition. The same applies to methods and classes—useful for iterating on a function until it works. ## Semicolons For a **single expression**, the trailing semicolon is **optional** (`2 + 2` works). When you write a full statement or multiple statements on one line, the usual Java semicolon rules apply to separate them. JShell is lenient to keep interaction fast. ## Why result variables matter They turn the REPL into a chain: each result is automatically saved, so you can compose a calculation step by step, inspect intermediate values, and never lose a computed result to the void—exactly the incremental, exploratory workflow a REPL is built for.

  • How do you reuse the value of an expression you didn't name?
    Reference its implicit result variable, e.g. `$1`, which JShell created automatically and showed after `==>`.
  • What happens if you declare `int x` twice with different values?
    JShell redefines `x` to the latest declaration; the new value replaces the old, and dependents are re-evaluated.

saying these in an interview costs you the question

  • Thinking only expressions are allowed (statements and declarations are too)
  • Believing `$1` is a special syntax rather than just an auto-named variable
  • Assuming you cannot redefine a variable or method in a session
  • Claiming a class declaration is not allowed in JShell

context