Java 9 JShell Getting Started

[Updated: Oct 12, 2017, Created: Oct 4, 2017]

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 9
| For an introduction type: /help intro

Declaring variables

jshell>jshell> int x = 99;
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. Whe can change the feedback mode to a different value (default mode is 'normal')

jshell>jshell> /set feedback verbose
| Feedback mode: verbose
jshell>jshell> int x = 99;
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>jshell> Math.random();
$3 ==> 0.24045540661699993
| 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>jshell> System.out.println($3 + 5)
5.240455406617

List of Statements/Expressions/Declared Types

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

jshell>jshell> /list

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>jshell> $3+11
$5 ==> 11.240455406617
| created scratch variable $5 : double
jshell>jshell> /4
System.out.println($3 + 5)
5.240455406617

Declaring methods

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

Calling above method:

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

Creating a class

jshell>jshell> class BonusCalc {
...> 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>jshell> BonusCalc threePercentBonusCalc = new BonusCalc(3);
threePercentBonusCalc ==> BonusCalc@6093dd95
| created variable threePercentBonusCalc : BonusCalc

Using the instance:

jshell>jshell> threePercentBonusCalc.calc(1500);
$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>jshell> /list

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>jshell> /imports
| 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>jshell> import java.time.*;

Now we can use Java Date Time API:

jshell>jshell> LocalTime.now();
$13 ==> 15:29:25.512759800
| created scratch variable $13 : LocalTime

Using /help

jshell>jshell> /help
| Type a Java language expression, statement, or declaration.
| Or type one of the following commands:
| /list [|-all|-start]
| list the source you have typed
| /edit
| edit a source entry referenced by name or id
| /drop
| delete a source entry referenced by name or id
| /save [-all|-history|-start]
| Save snippet source to a file.
| /open
| open a file as source input
| /vars [|-all|-start]
| list the declared variables and their values
| /methods [|-all|-start]
| list the declared methods and their signatures
| /types [|-all|-start]
| list the declared types
| /imports
| list the imported items
| /exit
| exit jshell
| /env [-class-path ] [-module-path ] [-add-modules ] ...
| view or change the evaluation context
| /reset [-class-path ] [-module-path ] [-add-modules ]...
| reset jshell
| /reload [-restore] [-quiet] [-class-path ] [-module-path ]...
| reset and replay relevant history -- current or previous (-restore)
| /history
| history of what you have typed
| /help [|]
| get information about jshell
| /set editor|start|feedback|mode|prompt|truncation|format ...
| set jshell configuration information
| /? [|]
| get information about jshell
| /!
| re-run last snippet
| /
| re-run snippet by id
| /-
| re-run n-th previous snippet
|
| 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
| shortcuts
| a description of keystrokes for snippet and command completion,
| information access, and automatic code generation
| context
| the evaluation context options for /env /reload and /reset

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