For many years, interpreted languages have allowed you to quickly try out some code in your command-line. No need to fire-up IDEs and no need to compile anything — just type your code and see the results immediately. This has been proven to be very helpful with newcomers as they could try out a new programming language without getting their hands dirty with compilers, dependencies and build systems. A frequently overlooked feature surfaced in JDK 9 is JShell: A Read-Evaluate-Print Loop (REPL) environment in which you can try out Java code without invoking a compiler.
What a REPL is
Well, in just a few words, you type some command (Read), it gets interpreted (Evaluate), the result of that command is forwarded back to you (Print), and this sequence continues forever until you quit (Loop). And there you have your R(ead) E(valuate) P(rint) L(oop).
Provided Java JDK 9 or greater is available on your command-line, you can start JShell simply by typing
jshell> prompt that you get, tells you that JShell is currently on its R(ead) mode expecting your input.
For a quick glimpse of available commands, you can issue
Commands, code-completion and references
JShell incorporates code-completion with suggestions using the TAB key. Go right ahead and issue the all-time favourite
System.out.println(“Hello World”) pressing TAB after
In fact, due to the REPL nature, the output of any command you type will be printed back to you, so there is no need to
System.out your commands (nor to terminate them with a
You can use JShell to assign variables and back-reference any previous result using the special
An interesting feature of JShell is that it supports forward references. Forward references are commands which refer to methods, variables, or classes that do not exist yet in any code you have typed in JShell. For example, consider we have a
valueOfStocks method that calculates the total value of the stocks we hold based on the current price of the stock as well as the number of the stocks we have. You can first define the calculation method and later on provide the actual/current value of each stock:
Packages, imports and external libraries
JShell imports some frequently-used Java packages for you by default:
You can import any other package you need simply by issuing an
When it comes to using code from external libraries, such as code in JARs, you need to add the JARs you need into the classpath of JShell and then just import the package you need:
Saving, loading and running
JShell supports importing resources (i.e. JShell scripts) from your local environment as well as saving your JShell session to a file with
/open commands respectively. You can also start JShell by loading a previously saved session at startup:
JShell allows you to quickly test code or effortlessly prototype some algorithm you are developing without having to compile your code. Its REPL-based environment provides you with direct feedback on any command you type. If you want to learn more about JShell, Oracle maintains a comprehensive tutorial on JShell.
On top of quickly testing your Java code with JShell, do you also plan to test your Docker images? The following article is for you then: