JShell is a REPL interactive tool introduced in Java 9 to execute and evaluate simple java programs like variable declarations, statements, expressions, and the programs without using the main() method.
In JShell, any value returned by a snippet automatically saved into a scratch variable. These scratch variables can be represented by $. When we do not assign the result of an expression to a variable, a scratch variable is created in JShell so that an output of expression can be used later.
In the below code snippet, six scratch variables have created
C:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> 3+7 $1 ==> 10 jshell> 9-2 $2 ==> 7 jshell> 4*4 $3 ==> 16 jshell> 12/4 $4 ==> 3 jshell> 19%5 $5 ==> 4 jshell> String.valueOf($2) $6 ==> "7"
In the below code snippet, the "nonScratch" variable has created. It is not a scratch variable because it can't be represented with $.
jshell> String nonScratch = "Tutorialspoint" nonScratch ==> "Tutorialspoint" jshell>
In the below code snippet, "/vars" command can display both scratch and non-scratch variables for that particular session.
jshell> /vars | int $1 = 10 | int $2 = 7 | int $3 = 16 | int $4 = 3 | int $5 = 4 | String $6 = "7" | String name = "Tutorialspoint" jshell>