JShell is a powerful tool for learning and experimenting with Java, introduced in Java 9. It’s Java’s version of a REPL (read-eval-print loop), which is simply a tool that allows one to type lines of code and have it run instantly, without having to first write a class, save it, compile it, and then FINALLY run it.
(Note: you can get started right away with an online JShell at https://tryjshell.org/)
First let’s determine if JShell is installed. If you’ve installed JDK 9 or after, you should already have it. (For beginners, JDK stands for Java Development Kit, which is all of the stuff you need to write, compile, and run Java programs.) If you don’t know what JDK you have installed, you can test if JShell is installed by opening a terminal and trying to run it. (On MacOS you can get a terminal by running /Applications/Utilities/Terminal; on Windows 8 and above you can press the Windows key and type “cmd”.)
If JShell is installed, you’ll see something like this:
If that’s not what you see, you probably need to install the latest JDK. You can search “JDK 10 download” to get it. At the time of this writing, the link was here.
The page looks something like the below. Remember to click “Accept License Agreement” – Oracle hides those buttons for some reason!
After running the installer, you should be able to run jshell from a terminal as shown earlier.
First, try typing “/help intro”. You’ll see that JShell gives you a (very) short introduction to what JShell does.
Try entering what the intro text suggests:
As you see, JShell executes the code we enter immediately! Note that “x ==> 8” means that we’ve assigned the variable “x” the value 8. The line “$2 ==> 16” means that the second commaned we evaluated yielded the value 16. Let’s try some more.
Great! JShell remembers our earlier variables and lets us use them to assign to other variables.
The command “/list” shows us everything we’ve done so far:
We can also look at all the variables that have been defined:
This is just a very small sampling of what you can do with JShell. For more tutorials on a beginner level, you should check out https://jshelltutorial.com/.