JB Header

JShell Tutorial: Java's Interactive REPL with Examples

Introduction Writing a class with a main() method, compiling it and running it, just to check what one line of code does, takes time. JShell, Java's interactive REPL (Read-Evaluate-Print Loop), lets you type Java code and see the result immediately. This tutorial explains how to use JShell with examples. We will start by launching JShell and evaluating expressions. We will then declare variables and methods, and use JShell to explore Java's standard library. Next we will look at the JShell commands which list, edit, save and load your work. Lastly, we will see the keyboard shortcuts which make JShell faster to use.

(Note - JShell comes with every JDK since Java 9. If jshell is not recognized, see How to Install the JDK and Set JAVA_HOME and PATHClick to read tutorial: How to Install the JDK and Set JAVA_HOME and PATH.) Starting JShell and evaluating expressions Open a terminal and type jshell. JShell shows a welcome message and a jshell> prompt. Type any Java expression and press Enter to see its value. Type /exit to leave.
|  Welcome to JShell -- Version 24.0.1
|  For an introduction type: /help intro

jshell> 2 + 3 * 4
$1 ==> 14

jshell> 10 / 3
$2 ==> 3

jshell> 10 / 3.0
$3 ==> 3.3333333333333335

jshell> "Java".toUpperCase()
$4 ==> "JAVA"

jshell> Math.max(42, 17)
$5 ==> 42

jshell> /exit
|  Goodbye
Explanation of the session
  • For each expression, JShell prints the result in the form name ==> value. Results which you did not give a name are stored in automatically created variables named $1, $2 and so on, which you can use in later lines.
  • 10 / 3 gives 3, but 10 / 3.0 gives 3.3333333333333335 - dividing two whole numbers in Java throws away the remainder. Little surprises like this are exactly what JShell is great for discovering. See Java arithmetic operatorsClick to read tutorial: Java Arithmetic, Relational and Logical Operators for the details.
  • You do not need a semicolon at the end of a line in JShell, and you do not need a class or a main() method.
Declaring variables and methods You can declare variables and use them in later lines, just as in a program. JShell confirms each declaration.
|  Welcome to JShell -- Version 24.0.1
|  For an introduction type: /help intro

jshell> int price = 250
price ==> 250

jshell> int quantity = 3
quantity ==> 3

jshell> int total = price * quantity
total ==> 750

jshell> String message = "Total: " + total
message ==> "Total: 750"

jshell> message.length()
$5 ==> 10

jshell> /exit
|  Goodbye
Methods can be declared too. Type the whole method on one line, or across several lines - JShell shows a ...> continuation prompt until the method is complete.
|  Welcome to JShell -- Version 24.0.1
|  For an introduction type: /help intro

jshell> int square(int x) { return x * x; }
|  created method square(int)

jshell> square(7)
$2 ==> 49

jshell> square(square(3))
$3 ==> 81

jshell> /exit
|  Goodbye
Note - If you declare a variable or method again with the same name, the new declaration replaces the old one. JShell also accepts code which uses a method you have not written yet, and tells you it can be used once that method is defined - handy when trying ideas out of order. Exploring the Java library JShell is a great way to learn how library classes behave. The most common packages, such as java.util, java.io and java.math, are already imported. Press Tab after a dot to see the methods available - for example, type "hello". and press Tab to list every String method.
|  Welcome to JShell -- Version 24.0.1
|  For an introduction type: /help intro

jshell> List<String> names = new ArrayList<>(List.of("Tom", "Harry", "Ethan"))
names ==> [Tom, Harry, Ethan]

jshell> names.add("Nancy")
$2 ==> true

jshell> Collections.sort(names)

jshell> names
names ==> [Ethan, Harry, Nancy, Tom]

jshell> Integer.MAX_VALUE
$5 ==> 2147483647

jshell> Integer.MAX_VALUE + 1
$6 ==> -2147483648

jshell> /exit
|  Goodbye
Explanation of the session
Useful JShell commands JShell's own commands start with a /. These are the ones you will use most -
  • /list - lists the code you have entered.
  • /vars and /methods - list the variables and methods you have declared, with their types.
  • /imports - lists the packages that are imported.
  • /edit name - opens a variable or method in an editor window, to change it.
  • /save file.jsh and /open file.jsh - save your code to a file, and load it again later.
  • /reset - clears everything and starts fresh.
  • /help - lists all commands. /help intro gives a short introduction.
|  Welcome to JShell -- Version 24.0.1
|  For an introduction type: /help intro

jshell> double rate = 0.18
rate ==> 0.18

jshell> double withTax(double amount) { return amount + amount * rate; }
|  created method withTax(double)

jshell> withTax(100)
$3 ==> 118.0

jshell> /vars
|    double rate = 0.18
|    double $3 = 118.0

jshell> /methods
|    double withTax(double)

jshell> /list

   1 : double rate = 0.18;
   2 : double withTax(double amount) { return amount + amount * rate; }
   3 : withTax(100)

jshell> /exit
|  Goodbye
Note - Start JShell with jshell -v for verbose feedback, which describes each action and the type of each result in more detail. Keyboard shortcuts
  • Tab - completes class, method and variable names, and lists the options when there are several.
  • Shift+Tab then v - after typing an expression, creates a variable for its result, and lets you choose its name.
  • Shift+Tab then i - after typing a class name, offers to add the import for it.
  • Up and Down arrows - move through the lines you have already entered, to run or change them again.
Summary In this tutorial we first started JShell and evaluated expressions, and saw how JShell names results $1, $2 and so on. We then declared variables and methods and used them in later lines. Next we used JShell to explore the Java library, discovering integer division and overflow on the way. We then went through the most useful JShell commands, and lastly the keyboard shortcuts which make JShell quicker to use. Keep JShell open as you work through this series - it is the fastest way to try out every new idea. Next, we move on to the basic syntax of the Java languageClick to read tutorial: Java Syntax Basics: Statements, Identifiers, Keywords.