JShell (Interactive Java)

Naveen AnamaneniUpdated · 4 min read

Sometimes you only want a quick answer. What does 7 / 2 give in Java? Does "Pune".toUpperCase() work the way you think? Writing a class, saving a file and compiling it just to find out is slow. JShell, included in every JDK since Java 9, lets you type one line of Java and see the result immediately, like a calculator that speaks Java.

Start and Stop JShell

Open a terminal and type:

Terminal
jshell
JShell
|  Welcome to JShell -- Version 25.0.1
|  For an introduction type: /help intro
 
jshell>

The jshell> prompt means it's waiting for you. Type /exit to leave. If jshell isn't found, the JDK's bin folder isn't on your PATH; see "How to Set PATH and JAVA_HOME".

Expressions, Variables and Methods

Type Java and press Enter. You don't need a class, a main method or even a semicolon at the end of a line.

JShell
jshell> int fare = 450
fare ==> 450
 
jshell> fare * 3
$2 ==> 1350
 
jshell> String city = "Pune"
city ==> "Pune"
 
jshell> city.toUpperCase()
$4 ==> "PUNE"
 
jshell> 7 / 2
$5 ==> 3

What you're seeing

  1. int fare = 450 creates a variable, and JShell shows its value with ==>.
  2. fare * 3 is an expression without a variable. JShell stores the result in a scratch variable named $2, which you can use later.
  3. 7 / 2 gives 3, not 3.5: dividing two whole numbers in Java drops the decimal part. That's exactly the kind of thing JShell is good at checking.

You can also define a method and call it straight away:

JShell
jshell> void greet(String name) { System.out.println("Namaste, " + name); }
|  created method greet(String)
 
jshell> greet("Srihitha")
Namaste, Srihitha

Mistakes Are Cheap

If you make an error, JShell explains it and keeps going. Nothing else is lost.

JShell
jshell> int x = "a"
|  Error:
|  incompatible types: java.lang.String cannot be converted to int
|  int x = "a";
|          ^-^

The message is the same one javac would give, so JShell is also a safe place to practice reading compiler errors.

Useful Commands

Commands start with / so they don't clash with Java code.

CommandWhat it does
/varsLists the variables you've created
/methodsLists the methods you've defined
/listShows everything you've typed so far
/importsShows the packages already imported
/save notes.jshSaves your snippets to a file
/open notes.jshLoads snippets from a file
/resetStarts again with nothing defined
/helpLists all commands
/exitLeaves JShell

For example, after the session above, /vars prints:

JShell
jshell> /vars
|    int fare = 450
|    int $2 = 1350
|    String city = "Pune"
|    String $4 = "PUNE"
|    int $5 = 3

Common Mistakes

Typing java commands inside JShell. javac Hello.java is a terminal command, not Java code. Exit JShell first with /exit.

Expecting your work to be saved. Everything is lost when you exit, unless you use /save.

Forgetting that multi-line code needs complete braces. If you start a method with {, JShell waits with a ...> prompt until the matching } arrives. If you get stuck there, finish the braces or press Ctrl + C.

Practice

  1. Easy: In JShell, find out what 10 / 3, 10 % 3 and 10.0 / 3 give. What's the difference between them?
  2. Harder: Define a method int gst(int price) that returns 18% of a price (use price * 18 / 100). Call it for 500 and 1999. Then use /save gst.jsh, exit, start JShell again and /open gst.jsh.

Quick check: JShell

4 questions
1How do you start JShell?
2You type fare * 3 in JShell. Where is the result stored?
3Which command lists the variables you've created?
4What does 7 / 2 give in JShell?

Key Takeaways

Was this tutorial helpful?

Lesson 18 of 482