What are the different "/vars" commands in JShell in Java 9?


JShell is an interactive command-line tool introduced in Java 9. It is also called a REPL tool that takes input, evaluates it, and prints output to the user.

In the JShell tool, it's possible to list all variables created by using the internal command "/vars". We have different "/vars" commands available in the JShell tool as listed below.

/vars
/vars [ID]
/vars [Variable_Name]
/vars -start
/vars -all
  • /vars: This command allows to us display the list of all active variables of the current session.
  • /vars [ID]: This command displays the variable and its value, corresponding to the entered ID. This ID corresponds to the name of the variable that JShell assigned to expression ($ 1, $ 2…).
  • /vars [Variable_Name]: This command displays the variable [Variable_Name] and its value.
  • /vars -start: This command allows us to display all variables that we have added to the JShell startup script.
  • /vars - all: This command displays the list of all active, inactive, and loaded variables at startup.


In the below code snippet, created expression and variable. Then we can able to apply different "/vars" commands.

C:\Users\User>jshell
| Welcome to JShell -- Version 9.0.4
| For an introduction type: /help intro

jshell> 2 + 4
$1 ==> 6

jshell> /vars
|   int $1 = 6

jshell> int x = 20
x ==> 20

jshell> /vars
|   int $1 = 6
|   int x = 20

jshell> /vars $1
|   int $1 = 6

jshell> /vars x
|   int x = 20

jshell> /vars -all
|   int $1 = 6
|   int x = 20

jshell> /drop x
| dropped variable x

jshell> /vars -all
|   int $1 = 6|   int x = (not-active)

Updated on: 10-Apr-2020

303 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements