JShell is the name of the new Java 9 REPL. Install Java 9, and you'll have access to a shiny new JShell. If you're using an early-access version on Mac OS X like I am, you'll find JShell in the library hidden away in a path similar to the following.
Running JShell on any platform presents you with with a 'jshell>' prompt. If this is your first time using JShell, I recommend you start by typing '/help' to get a list of valid commands. Here are the results of typing '/help' in JShell.
| Type a Java language expression, statement, or declaration.
| Or type one of the following commands:
| /list [<name or id>|-all|-start] -- list the source you have typed
| /edit <name or id> -- edit a source entry referenced by name or id
| /drop <name or id> -- delete a source entry referenced by name or id
| /save [-all|-history|-start] <file> -- Save snippet source to a file.
| /open <file> -- open a file as source input
| /vars [<name or id>|-all|-start] -- list the declared variables and their values
| /methods [<name or id>|-all|-start] -- list the declared methods and their signatures
| /types [<name or id>|-all|-start] -- list the declared types
| /imports -- list the imported items
| /exit -- exit jshell
| /reset -- reset jshell
| /reload [-restore] [-quiet] -- reset and replay relevant history -- current or previous (-restore)
| /classpath <path> -- add a path to the classpath
| /history -- history of what you have typed
| /help [<command>|<subject>] -- get information about jshell
| /set editor|start|feedback|mode|prompt|truncation|format ... -- set jshell configuration information
| /retain editor|start|feedback|mode -- retain jshell configuration information for subsequent sessions
| /? [<command>|<subject>] -- get information about jshell
| /! -- re-run last snippet
| /<id> -- re-run snippet by id
| /-<n> -- re-run n-th previous snippet
| For more information type '/help' followed by the name of command or a subject.
| For example '/help /list' or '/help intro'. Subjects:
| intro -- an introduction to the jshell tool
| shortcuts -- a description of shortcuts
Seeing all the nifty commands you can issue is great and all, but some readers are probably wondering, "What on the Web is a JShell? And, why do I care?"
To answer that, keep reading.
JShell is the long anticipated Java REPL. REPL stands for Read, Evaluate, Print, Loop. And that is exactly what a REPL does. It reads in command snippets from the programmer, evaluates the command snippets, and then prints out any results of the commands, and then loops back to the read phase to start the process over again.
REPL's are run from the console/terminal/shell and allow you to execute commands directly. Traditionally in Java, if you wanted to test or run a snippet of code, you needed to write an entire program around that code. To test a single line of Java code, you needed to write a class with a main() method, compile the code, and then run the code. That's a lot of work. All that work meant Java almost never got used for simple tasks that like doing some quick math, because it was too hard just to get the code into a runnable state.
Now, all you need to do to run a small chunk of code is type it into a JShell REPL and hit enter.
Another great use for JShell's are that you can learn to program Java in the JShell. Also if you want to figure out some odd behavior, you can check how Java behaves by using the JShell, instead of writing massive amounts of code.
Nearly any declaration, statement, or expression is valid in a JShell.
For fun, type '/help intro' into the JShell. You get back the following.
jshell> /help intro
| The jshell tool allows you to execute Java code, getting immediate results.
| You can enter a Java definition (variable, method, class, etc), like: int x = 8
| or a Java expression, like: x + x
| or a Java statement or import.
| These little chunks of Java code are called 'snippets'.
| There are also jshell commands that allow you to understand and
| control what you are doing, like: /list
| For a list of commands: /help
Notice that the '/help' command was followed by the 'intro' subject. Currently, the valid subjects are 'intro' and 'shortcuts'. Expect that list to expand in time.
You can also follow '/help' with a command you want more information on. For example, type '/help /exit'. You get the following results printing to the JShell.
jshell> /help /exit
| Leave the jshell tool. No work is saved.
| Save any work before using this command
When you are done using the JShell, just type '/exit' to terminate your session.
I have written other JShell tutorials, so be sure to check them out here.