Java Code Prototyping with JShell

Nassos Michas
Sep 22, 2019 · 4 min read
Image for post
Image for post

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).

Starting JShell

Provided Java JDK 9 or greater is available on your command-line, you can start JShell simply by typing jshell:

Image for post
Image for post

The 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 /help:

Image for post
Image for post

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 System.out.:

Image for post
Image for post

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 ;):

Image for post
Image for post

You can use JShell to assign variables and back-reference any previous result using the special $ notation:

Image for post
Image for post

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:

Image for post
Image for post

Packages, imports and external libraries

JShell imports some frequently-used Java packages for you by default:

Image for post
Image for post

You can import any other package you need simply by issuing an import command:

Image for post
Image for post

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:

Image for post
Image for post

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 /save and /open commands respectively. You can also start JShell by loading a previously saved session at startup:

Image for post
Image for post

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:

The Startup

Medium's largest active publication, followed by +705K people. Follow to join our community.

Nassos Michas

Written by

Software engineer | Cert. Scrum master | Cert. Professional for Requirements Engineering | CTO at European Dynamics

The Startup

Medium's largest active publication, followed by +705K people. Follow to join our community.

Nassos Michas

Written by

Software engineer | Cert. Scrum master | Cert. Professional for Requirements Engineering | CTO at European Dynamics

The Startup

Medium's largest active publication, followed by +705K people. Follow to join our community.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store