JShell Tutorial: Java's Interactive REPL with Examples
Introduction
Writing a class with a
(Note - JShell comes with every JDK since Java 9. If
Explanation of the session
Declaring variables and methods
You can declare variables and use them in later lines, just as in a program. JShell confirms each declaration.
Methods can be declared too. Type the whole method on one line, or across several lines - JShell shows a
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
Explanation of the session
Useful JShell commands
JShell's own commands start with a
Note - Start JShell with
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
- 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,$2and so on, which you can use in later lines. 10 / 3gives3, but10 / 3.0gives3.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.
| 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
...> 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
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
List,ArrayListandCollectionscome fromjava.util, and were usable without animport.names.add("Nancy")returnedtrue, andCollections.sort()returns nothing, so JShell printed no value for it.Integer.MAX_VALUE + 1gives a negative number. The result is too large for anint, so it overflows and wraps around. We cover this in Java primitive data typesClick to read tutorial: Java Primitive Data Types and Their Ranges with Examples.
/. These are the ones you will use most -/list- lists the code you have entered./varsand/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.jshand/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 introgives 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
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+Tabthenv- after typing an expression, creates a variable for its result, and lets you choose its name.Shift+Tabtheni- after typing a class name, offers to add theimportfor it.UpandDownarrows - move through the lines you have already entered, to run or change them again.
$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.Java Fundamentals Tutorials
How Java Code Runs: Bytecode, Class Loader and JITClick to read tutorial: How Java Code Runs: Bytecode, Class Loader and JIT Java Syntax Basics: Statements, Identifiers, KeywordsClick to read tutorial: Java Syntax Basics: Statements, Identifiers, Keywords What is Java? History, Features, Editions and UsesClick to read tutorial: What is Java? History, Features, Editions and Uses JDK vs JRE vs JVM - Differences Explained with ExamplesClick to read tutorial: JDK vs JRE vs JVM - Differences Explained with Examples 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 Choosing a Java IDE: IntelliJ IDEA vs Eclipse vs VS CodeClick to read tutorial: Choosing a Java IDE: IntelliJ IDEA vs Eclipse vs VS Code Java Hello World Program Explained Line by LineClick to read tutorial: Java Hello World Program Explained Line by Line
How Java Code Runs: Bytecode, Class Loader and JITClick to read tutorial: How Java Code Runs: Bytecode, Class Loader and JIT Java Syntax Basics: Statements, Identifiers, KeywordsClick to read tutorial: Java Syntax Basics: Statements, Identifiers, Keywords What is Java? History, Features, Editions and UsesClick to read tutorial: What is Java? History, Features, Editions and Uses JDK vs JRE vs JVM - Differences Explained with ExamplesClick to read tutorial: JDK vs JRE vs JVM - Differences Explained with Examples 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 Choosing a Java IDE: IntelliJ IDEA vs Eclipse vs VS CodeClick to read tutorial: Choosing a Java IDE: IntelliJ IDEA vs Eclipse vs VS Code Java Hello World Program Explained Line by LineClick to read tutorial: Java Hello World Program Explained Line by Line