What is REPL
Many languages include tools (sometimes called REPL) for statements interpretation. Using these tools you can test code snippets quickly without creating full project.
For example python. Creating project can sometimes take a long time, but using repl you can getting started with the language more quickly. Each entered expression gives you returned value and it’s type – that’s very valuable information.
In java, instead, we have to create a test or main method which prints results and needs to be recompiled every time you make a change.
What is JShell
The JShell tool is a command line tool (which will be introduced in Java 9 realease) that facilitates exploratory programming by providing interactive use of Java Programming Language elements. JShell is a REPL (Read-Evaluate-Print-Loop). It is ideal both for learning the Java language and exploring unfamiliar code (include new Java APIs). Typical Java development means writing a complete program, then compiling it (fixing any errors), the running it, figuring out what is wrong, editing, and repeating. With JShell you can enter program elements one at a time, immediately seeing the result and adjusting accordingly. During development, code can be pasted into JShell, and/or working code pasted from JShell into a program editor.
Starting JShell
First install JDK 9. JShell is included in JDK 9. You can get early access build on http://jdk.java.net/9/.
For this tutorial all examples use verbose mode. To launch the JShell tool in verbose mode, type jshell at the command-line:
1 2 3 |
jshell -v | Welcome to JShell -- Version 9 | For an introduction type: /help intro |
Expressions and statements
JShell accepts Java statements, variable, method, and class definitions, imports, and expressions. For example, you can enter a statement at the prompt, any side-effects will take place and any output displayed:
1 2 |
jshell> System.out.println("www.thecodeexamples.com"); www.thecodeexamples.com |
By default, JShell will give you information about what you entered. Here for example, declaring a variable:
1 2 3 |
jshell> int a = 99 a ==> 99 | created variable a : int |
When an expression is entered that doesn’t have a named variable, a scratch variable is created so that the value can be referenced later :
1 2 3 4 5 6 7 |
jshell> 99 + 1 $10 ==> 100 | created scratch variable $10 : int jshell> $10 $10 ==> 100 | value of $10 : int |
Methods
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
You can define methods and replace them: jshell> void helloJShell(){System.out.println("Hello jshell");} | created method helloJShell() jshell> helloJShell() helloJShell() jshell> helloJShell() Hello jshell jshell> void helloJShell(){System.out.println("Hello jshell!");} | modified method helloJShell() | update overwrote method helloJShell() jshell> helloJShell() Hello jshell! |
commands
JShell has a number of commands for controlling the environment and displaying information. They are distinguished from snippets by a leading slash.
list of variables
1 2 3 4 5 |
jshell> /vars | int $1 = 2 | int a = 99 | int $9 = 100 | int $10 = 100 |
list of methods
1 2 |
jshell> /methods | void helloJShell() |
list of entered snippets
1 2 3 4 5 6 7 8 |
jshell> /list 1 : System.out.println("www.thecodeexamples.com"); 2 : int a = 99; 3 : 99 + 1 4 : $10 5 : helloJShell() 6 : void helloJShell(){System.out.println("Hello jshell!");} 7 : helloJShell() |
exit from jshell
1 2 |
jshell> /exit | Goodbye |
Tab Completion -Commands
Tab completion works for commands and command arguments as well:
1 2 3 4 5 6 7 8 9 |
$ jshell -v | Welcome to JShell -- Version 9 | For an introduction type: /help intro jshell> /<tab> /! /? /drop /edit /env /exit /help /history /imports /list /methods /open /reload /reset /save /set /types /vars <press tab again to see synopsis> |