Close

Java 9 JShell Getting Started

[Last Updated: Nov 20, 2018]

Java 9 JShell is an interactive command line tool which helps us to quickly evaluates declarations (variable, method, classes), statements, and expressions as they are entered. JShell is also called Read-Evaluate-Print Loop (REPL).

In this getting started tutorial, we will learn how to use expression, statements and declare types.

Starting JShell

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

Declaring variables

jshell> int x = 99;Enter
x ==> 99

The use of semicolon (;) is optional at the end of a statement.

Feedback mode

In the above output, the second line 'x ==> 99' is the feedback about the results. In this case, it says that variable x has been assign a value of 99. We can change the feedback mode to a different value (default mode is 'normal')

jshell> /set feedback verboseEnter
| Feedback mode: verbose
jshell> int x = 99;Enter
x ==> 99
| modified variable x : int
| update overwrote variable x : int

Above feedback shows that that variable has been modified and overwritten (we declared the same variable before changing the feedback mode).

Calling methods

jshell> Math.random();Enter
$3 ==> 0.37787313137838363
| created scratch variable $3 : double

Scratch variables

As seen in the last output, when an expression is entered that doesn't assigned to a named variable, a scratch variable is created for it, so that it can be referenced later. Let's use the scratch variable which was created above.

jshell> System.out.println($3 + 5)Enter
5.377873131378384

List of Statements/Expressions/Declared Types

We can see the list of everything which we have entered so far:

jshell> /listEnter

2 : int x = 99;
3 : Math.random();
4 : System.out.println($3 + 5)

We can use the numbers (on the left) of above list to access the variable by using $ (e.g. $3) or run an snippet by using / (e.g. /4)

jshell> $3+11Enter
$5 ==> 11.377873131378383
| created scratch variable $5 : double
jshell> /4Enter
System.out.println($3 + 5)
5.377873131378384

Declaring methods

jshell> long factorial (int n) { return n==1 ? 1: n * factorial(n-1); }Enter
| created method factorial(int)

Calling above method:

jshell> factorial(6);Enter
$8 ==> 720
| created scratch variable $8 : long

Creating a class

jshell> class BonusCalc {Enter
...> private int rate;
...>
...> BonusCalc(int rate) {
...> this.rate = rate;
...> }
...>
...> int calc(int salary) {
...> return salary + (salary * rate) / 100;
...> }
...> }
| created class BonusCalc

As seen above, if code is incomplete then hitting enter will not submit the code but the editing will continue on the next line.

Creating an instance of our class:

jshell> BonusCalc threePercentBonusCalc = new BonusCalc(3);Enter
threePercentBonusCalc ==> BonusCalc@d2cc05a
| created variable threePercentBonusCalc : BonusCalc

Using the instance:

jshell> threePercentBonusCalc.calc(1500);Enter
$11 ==> 1545
| created scratch variable $11 : int

Use of Tab Key

While typing the reference variables, we can use 'tab' key which does autocompletion (or give the suggestions) which is very helpful. Tab key works with almost everything including commands like '/list'.


Let's see what our /list have now:

jshell> /listEnter

2 : int x = 99;
3 : Math.random();
4 : System.out.println($3 + 5)
5 : $3+11
6 : System.out.println($3 + 5)
7 : long factorial (int n) { return n==1 ? 1: n * factorial(n-1); }
8 : factorial(6);
9 : class BonusCalc {
private int rate;

BonusCalc(int rate) {
this.rate = rate;
}

int calc(int salary) {
return salary + (salary * rate) / 100;
}
}
10 : BonusCalc threePercentBonusCalc = new BonusCalc(3);
11 : threePercentBonusCalc.calc(1500);

Importing packages

By using /imports command, we can see what packages are imported by default.

jshell> /importsEnter
| import java.io.*
| import java.math.*
| import java.net.*
| import java.nio.file.*
| import java.util.*
| import java.util.concurrent.*
| import java.util.function.*
| import java.util.prefs.*
| import java.util.regex.*
| import java.util.stream.*

We can import other packages ourselves:

jshell> import java.time.*;Enter

Now we can use Java Date Time API:

jshell> LocalTime.now();Enter
$13 ==> 00:12:52.282766900
| created scratch variable $13 : LocalTime

Using /help

jshell> /helpEnter
| Type a Java language expression, statement, or declaration.
| Or type one of the following commands:
| /list [<name or id>|-all|-start]
| list the source you have typed
| /edit <name or id>
| edit a source entry
| /drop <name or id>
| delete a source entry
| /save [-all|-history|-start] <file>
| Save snippet source to a file
| /open <file>
| open a file as source input
| /vars [<name or id>|-all|-start]
| list the declared variables and their values
| /methods [<name or id>|-all|-start]
| list the declared methods and their signatures
| /types [<name or id>|-all|-start]
| list the type declarations
| /imports
| list the imported items
| /exit [<integer-expression-snippet>]
| exit the jshell tool
| /env [-class-path <path>] [-module-path <path>] [-add-modules <modules>] ...
| view or change the evaluation context
| /reset [-class-path <path>] [-module-path <path>] [-add-modules <modules>]...
| reset the jshell tool
| /reload [-restore] [-quiet] [-class-path <path>] [-module-path <path>]...
| reset and replay relevant history -- current or previous (-restore)
| /history [-all]
| history of what you have typed
| /help [<command>|<subject>]
| get information about using the jshell tool
| /set editor|start|feedback|mode|prompt|truncation|format ...
| set configuration information
| /? [<command>|<subject>]
| get information about using the jshell tool
| /!
| rerun last snippet -- see /help rerun
| /<id>
| rerun snippets by ID or ID range -- see /help rerun
| /-<n>
| rerun n-th previous snippet -- see /help rerun
|
| For more information type '/help' followed by the name of a
| command or a subject.
| For example '/help /list' or '/help intro'.
|
| Subjects:
|
| intro
| an introduction to the jshell tool
| id
| a description of snippet IDs and how use them
| shortcuts
| a description of keystrokes for snippet and command completion,
| information access, and automatic code generation
| context
| a description of the evaluation context options for /env /reload and /reset
| rerun
| a description of ways to re-evaluate previously entered snippets

Help can mostly be useful to know the available commands (the ones starting with /). In this tutorial, we have seen some of them already. We will be exploring others in this series of tutorials.

See Also