JShell – Java 9 interpreter (REPL) – Getting Started with Examples and Video Tutorial

Why JShell – Java 9 interpreter (REPL)?

In order to evaluate your statements instantly without creating a project and compiling whole source code, Oracle recently introduced JShell in Java 9. JShell is a similar tool like other compiled language own for statements interpretation, sometimes called REPL (read-eval-print loop). It helps programmer to quickly evaluate code snippets without compiling the whole code.

What is REPL?

A read-eval-print loop (REPL) is known as an interactive or language shell that takes single user inputs, evaluates them and return the result to user. A program written in REPL environment is executed piece wise or in steps. Such interfaces are similar to the classic Lisp machine interactive environment.

How JShell works?

A simple programming environment in the command line that reads user’s input, evaluates it, prints the result, and then repeats the same cycle.  You can write Java language expressions and statements in the JShell, and they will be evaluated on their own without having to wrap them in classes or methods.

It works similar to Scala or Python interpreter or other JVM languages that have a REPL like Kotlin and Groovy.

How to Run JShell?

Once you downloaded and installed jdk9 (download from here), you may locate java installation path and find the jshell executable in bin directory. Type jshell in your terminal to start JShell session. It is suggested to use verbose mode for the first time.

If jshell command is not recognizable by your terminal. Set JAVA_HOME path  and make sure JAVA_HOME/bin is added to the PATH variable.

If it works fine, write the first Hello World example using JShell (REPL) environment.

Default Imports

Variables and Expressions

JShell allows you to declare valid java variables and expressions, and it will tell you the returned value, it’s type and assign it to a variable and result

Creating java valid string variable and concatenating them. Find the example below

Methods

You can define methods and can redefine them with new functionality.

You can define return type, parameters and selection statements as given in the example below.

Class

Creating a class, class methods and creating objects and invoking method in JShell environment.

Commands

Aparat from language syntax you can execute jshell commands. Some of the most useful ones (/help to list all of them) are:
Default Imports

Listing Variables

Listing Methods

List Classes

Save source code in a file
You can save all the source that you have typed in the current JShell session in a file using /save command

You can open all the source code that you have previously typed and saved in a text file.

You can also open a java file in a similar way

Listing History

Edit Method Class or any piece of Code
Running edit command will open your code in a editor.

Tab Option
You can press tab, to check which methods you can call.

Now, for checking what methods are available for use for the above Student object, type s followed by a dot (.), and then press tab

Forward References

JShell supports forward references. Which means you can define functions that refer to other methods or variables that will be defined at a later stage. But you can’t invoke it until reference method is not defined.

Collections

Streams

Video Tutorial

Above mentioned example are recorded in video given below:

Conclusion

JShell is a very useful tool for prototyping and testing Java code snippets. It will help beginners to learn and adopt java language quickly. It will help developers to quickly evaluate code, without writing classes, methods and imports. I highly recommend checking it out. There is also a JShell Java api which allows you to evaluate JShell from java.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.