Sub topic 3
Declaring and Redefining Variables
Prerequisites and the JShell variable model
This chapter assumes that you know basic Java types, variables, expressions, and terminal commands. You also need a JDK 9 or later with jshell available on your path.
A JShell session keeps declarations in memory. A variable declaration gives a value a name and a type. Later input can read or change that variable.
An expression without a variable name receives an automatic scratch variable such as $1 or $2:
jshell> 6 * 7
$1 ==> 42
jshell> $1 + 8
$2 ==> 50Scratch variables are useful for quick calculations, but named variables are easier to read and reuse.
Declaring and assigning variables
Use a declaration to create a named variable:
jshell> int temperature = 21
temperature ==> 21
jshell> temperature = temperature + 3
temperature ==> 24The first input declares temperature as an int. The second input assigns a new value to the existing variable. Assignment does not change the variable's type.
A declaration can use objects and generic types too:
jshell> String language = "Java"
language ==> "Java"
jshell> List<String> tools = List.of("jshell", "javac")
tools ==> [jshell, javac]JShell imports several common packages automatically, including java.util, so List works here without an import command.
| Input | Effect |
|---|---|
| `int count = 1` | Declares a variable named `count` |
| `count = 2` | Assigns another value of the same type |
| `count++` | Increases the current value by one |
| `String count = "two"` | Redefines the name with a different type |
Redefining a variable
In ordinary Java source, declaring the same local variable twice in one scope causes a compile-time error. JShell is designed for experimentation, so it lets you redefine a name:
jshell> String greeting = "Hello"
greeting ==> "Hello"
jshell> greeting.toUpperCase()
$2 ==> "HELLO"
jshell> int greeting = 5
greeting ==> 5
jshell> greeting + 10
$3 ==> 15The current greeting is now an int. The earlier String declaration is no longer the active definition for new input.
This behavior makes it easy to correct an experiment, but it can also hide mistakes. If later code expects the old type, JShell reports an error because the name now refers to the replacement variable.
Inspecting the current variables
Use /vars to inspect active variables and their values:
jshell> /vars
| int $1 = 42
| int $2 = 50
| int greeting = 5The output includes scratch variables and named variables. It is useful when a session has become difficult to follow.
Use /drop when you no longer need a variable:
jshell> /drop greeting
| dropped variable greetingDropping a variable removes its active name from the session. It does not turn the session into an ordinary Java source file, and it does not affect unrelated variables.
Exercise
In a JShell session, what are the type and value of value after these inputs? String value = "7" followed by int value = 7 followed by value + 1.
Show answer
value is an int with value 7, and value + 1 evaluates to 8. The second declaration replaces the active String definition.