You are currently viewing Introduction to JShell in Java

Introduction to JShell in Java

This entry is part 3 of 6 in the series Foundations & Setup

1. Introduction

JShell is an interactive tool introduced in Java 9 that helps developers learn the Java language. It is a Read-Evaluate-Print Loop (REPL) tool that evaluates declarations, statements, and expressions as you enter them in a terminal. In this quick tutorial, you will learn how to launch and use JShell on a Windows workstation. The procedure is very similar to that of other operating systems.

2. Why Use JShell?

As a developer, you sometimes want to quickly check the result of a Java expression. Does "".isEmpty() return true or false? Does "".isBlank() return true or false? JShell offers you a simple way to answer these questions without the need to create a Java project in an IDE. While JShell provides you with an interactive tool to execute Java code, it can not replace a traditional IDE that you will still need to build a Java application.

3. Start and Stop JShell

JShell is available as a binary once you install the Java Development Kit (JDK). To start JShell, type “jshell” in a command prompt as shown below:

jshell

The following screen will then appear:

C:\Users\Noel Kamphoa>jshell
|  Welcome to JShell -- Version 21.0.2
|  For an introduction type: /help intro

jshell>

From there, you can start typing Java expressions in the prompt.
If you are done with JShell, type the following command to exit the prompt:

jshell> /exit
|  Goodbye

4. Using JShell

There are two ways of using JShell: Code Snippets and JShell Commands.

4.1. Code Snippets

4.1.1. Declaring a variable

You may use JShell to run any valid Java expression or declare a variable.

jshell> int myVar = 5

Hit “Enter” and you will see the following output:

jshell> int myVar = 5
myVar ==> 5

As you can see, a variable named “myVar” has been created and assigned the value 5.
It’s also possible to run an expression without explicitly assigning it to a variable. In such a case, Java will create a Scratch variable automatically.

jshell> 5 + 5
$4 ==> 10

The scratch variable $4 is created with the value 10.

After declaring the variable, you can use it in another expression:

jshell> System.out.println(myVar)
5

You can do the same with a scratch variable:

jshell> System.out.println($4);
10

4.1.2. Declaring a method

JShell allows you also to create a method and therefore experiment with whatever you want. Given the following Java method:

void sayHello(String name){
    System.out.println("Hello "+name);
}

You can declare it in the current JShell session by doing for example a copy/paste.

jshell> void sayHello(String name){
   ...>
jshell> void sayHello(String name){e completions; total possible completions: 557>
   ...>     System.out.println("Hello "+name);
   ...> }
|  created method sayHello(String)

As you can see the method is created successfully.
Afterward, you may test the method in this way:

jshell> sayHello("John")
Hello John

You can also update a method by providing a new definition:

jshell> void sayHello(String name){
   ...>     System.out.println("Hi "+name);
   ...> }
|  modified method sayHello(String)

From the output, you can see that the method is updated.

jshell> sayHello("John")
Hi John

Pro-tip: Auto-complexion is active in the JShell terminal. Use <TAB> to automatically complete the current code.

4.2. JShell Commands

Everything in JShell happens within a session. There are a couple of commands that you can use to control your JShell environment and display information about the session. Unlike snippets, JShell commands start with a “/”.
To retrieve information about the current variables, methods, and types, use respectively /vars, /methods, and /types.

jshell> /vars
|    int $1 = 2
|    int myVar = 5
|    int $3 = 2
|    int $4 = 10
jshell> /methods
|    void sayHello(String)
jshell> /types

If you want to display information about all the entered snippets, use /list.

jshell> /list

   1 : 1 + 1
   2 : int myVar = 5;
   3 : 1 + 1
   4 : 5 + 5
   5 : $4
   6 : System.out.println(myVar)
   7 : System.out.println($4);
   9 : sayHello("John")
  10 : void sayHello(String name){
           System.out.println("Hi "+name);
       }
  11 : sayHello("John")

5. Conclusion

In this quick article, you learned how to use the interactive JShell tool to simplify your learning process of the Java Language.

Series Navigation<< Java at a Glance: Key Differences Between JRE, JDK, and JVMCreating Your First Java Application With IntelliJ IDEA >>

Noel Kamphoa

Experienced software engineer with expertise in Telecom, Payroll, and Banking. Now Senior Software Engineer at Societe Generale Paris.

This Post Has One Comment

Comments are closed.