JShell (Interactive Java)
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:
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> 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 ==> 3What you're seeing
int fare = 450creates a variable, and JShell shows its value with==>.fare * 3is an expression without a variable. JShell stores the result in a scratch variable named$2, which you can use later.7 / 2gives3, not3.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> void greet(String name) { System.out.println("Namaste, " + name); }
| created method greet(String)
jshell> greet("Srihitha")
Namaste, SrihithaMistakes Are Cheap
If you make an error, JShell explains it and keeps going. Nothing else is lost.
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.
| Command | What it does |
|---|---|
/vars | Lists the variables you've created |
/methods | Lists the methods you've defined |
/list | Shows everything you've typed so far |
/imports | Shows the packages already imported |
/save notes.jsh | Saves your snippets to a file |
/open notes.jsh | Loads snippets from a file |
/reset | Starts again with nothing defined |
/help | Lists all commands |
/exit | Leaves JShell |
For example, after the session above, /vars prints:
jshell> /vars
| int fare = 450
| int $2 = 1350
| String city = "Pune"
| String $4 = "PUNE"
| int $5 = 3Common 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
- Easy: In JShell, find out what
10 / 3,10 % 3and10.0 / 3give. What's the difference between them? - Harder: Define a method
int gst(int price)that returns 18% of a price (useprice * 18 / 100). Call it for 500 and 1999. Then use/save gst.jsh, exit, start JShell again and/open gst.jsh.
Key Takeaways
Was this tutorial helpful?