Java 9 Features : JShell (REPL) Tutorial with example

In this tutorial, we are going to learn about Java 9 Features – JShell (REPL) with examples.

Table of content

  • Overview
  • Java SE 9 : JShell (REPL) Features
  • Java SE 9 : JShell (REPL) Tab Completion Features
  • JAVA SE 9 : JShell (REPL) Import Features
  • Java SE 9 : JShell (REPL) Basic Calculation
  • Java SE 9 : JShell (REPL) Internal Variables
  • Java SE 9 : JShell (REPL) Function Example


In my previous tutorial of this Java 9 tutorial series, we covered a complete guide on JDK 9 installation and configuration of JShell console (REPL).

Java 9 has introduced a new tool called JShell or Java Shell. It stands for Java Shell and known as Read Eval Print Loop (REPL). JShell tool provides an Interactive way to evaluate declarations, statements, and expressions of the Java programming language. We can easily develop, execute and test Java class, interface, object, statement, etc. with the help of JShell. We can only code and run the function without executing of “javac

Now, most of the latest programming languages like Scala, Ruby, Python supports REPL (Read Eval Print Loop) feature because of its simplicity. We do not need to write lots of code to execute a simple statement.

Now a question may come in your mind, why REPL is more popular and why all the latest technologies support this feature?

Let us understand the scenario, where you have two-week sprints and you need to implement some new features, In that time it is more important to test something in the code rather than set up the whole project. REPL brings that to you. It is becoming more popular because of its simplicity and feasibility.

Java SE 9 : JShell (REPL) Features

JShell supports the following features as below.

  • JShell is an interactive tool that provides the easiest way to develop, execute and test the Java Classes, Objects etc.
  • It adds semicolon automatically so we don’t need to use it.
  • It supports to see the history of the previously executed commands.
  • It supports tab completion features that provide the hints to select.
  • It allows us to configure the default import as per our requirement.

Java SE 9 : JShell (REPL) Tab Completion Features

Java 9 REPL comes with the interesting feature, it supports Tab Completion. In this features, we don’t need to type a lot of characters. We need to type a couple of characters and then press “Tab” key. It will provide all the available hints. It will save developers time and avoid conflicts.

Before exploring this feature, we need to check and verify whether the JDK 9 is installed or not in your system?

To check which java version is installed on your system, Open the command prompt and execute this command “java –version“.

C:\Users\Subhankar Adhikary> java –version java version "1.9.0-ea" Java(TM) SE Runtime Environment (build 1.9.0-ea-b93) Java HotSpot(TM) 64-Bit Server VM (build 1.9.0-ea-b93, mixed mode) C:\Users\Subhankar Adhikary>

If you are facing any problem in installation and configuration of JDK 9, please go through my previous tutorial about the complete guide on JDK 9 installation.

 JAVA SE 9 : JShell (REPL) Import Features

Execute “/imports” command to get all the default import as shown below.

jshell> /imports |    import* |    import java.math.* |    import java.nio.file.* |    import* |    import java.util.* |    import java.util.concurrent.* |    import java.util.function.* |    import* |    import java.util.prefs.* |    import java.util.regex.* |    import java.lang.*

Now, We can test the class and interface available in the default package as shown above.

jshell> String name = new String("Subhankar") name ==> " Subhankar " Note: We don’t need to import any external package to successfully execute the above program.

 If you need to import any external package that does not available in the default package, then execute the below command.

jshell> import

Java SE 9 : JShell (REPL) Basic Calculation

Let us try some basic example of JShell.

jshell> 1+1 $1 ==> 2 jshell> 10+1 $2 ==> 11 jshell> 10*10 $3 ==> 100 jshell> 13%3 $4 ==> 1

Java SE 9 : JShell (REPL) Internal Variables

Another interesting feature comes in Java 9 JShell, is Internal variable. Java automatically creates the Internal variables.

If we do not use our own variable then Java automatically add the Internal variable. Let’s see some example.

jshell> 1+1 $1 ==> 2 jshell> 10+1 $2 ==> 11 jshell> 10*10 $3 ==> 100

Note: $1, $2, $3 are the internal variable.

Now, a question may come in your mind, is this Internal variables are Immutable or final (We cannot change it)?

Let us explore this question.

jshell> 1+1 $1 ==> 2 jshell> $1 = 10 $1 ==> 10

The answer is Nope. The Internal variables are mutable means we can change the value of the internal variables. Here in this example, we can see that the value of Internal variable $1 is changed from 2 to 10.

Another question may come in your mind; Can we use our own variables?

The answer is Yes. We can use our own variable.

Let us explore by an example

jshell> int add = 1+1 add ==> 2 jshell> add add ==> 2 jshell> add = 10 add ==> 10

Java SE 9 : JShell (REPL) Function Example

In this example we will see how to define a function and then call it from JShell.

jshell> int test(int num) { return num; } |  created method test(int) jshell> test(5) $1 ==> 5

That is all about the JShell (REPL) or Java Shell. In the next tutorial, we will learn more about Java SE 9 features.

If you like my tutorials or have any issues or suggestions, please drop a comment.

Thank you for reading my tutorials.

Read this tutorial to know about complete features of Java SE 9.

Subhankar Adhikary

I am software professional having more than 4 years of experience in various web technologies mostly in Java. I have strong knowledge in Core Java, Spring, Hibernate, Grails, Web Application, Web Security etc. My hobbies are Developing Software, Learning new technologies, Reading books and Sharing my knowledge through writing articles on PamphletScoop.

2 thoughts on “Java 9 Features : JShell (REPL) Tutorial with example

  • September 16, 2017 at 11:20 am

    Good tutorial. I got the clear idea of JShell.

    • September 16, 2017 at 11:23 am

      Thank you for reading my tutorials.


Leave a Reply

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