Java 9 Repl Tutorial
In this example, I would like to show you how to get started with Java 9 REPL (The Java Shell: Read-Eval-Print Loop). Oraclesite has excellent details of the features.
Here, I present some examples and details to get started along with some of the important features and commands of this useful feature added in Java 9.Jshellis a quick way for developers to test code snippets. More details can be found at JEP222 andjdk.shell site.
As indicated in JEP222, the motivation ofjshellis to interactively test expressions and code within thejshellstate. The variables and methods that are going to be tested do not need to occur within a method/ class. All inputs to jshell must match the Java Language Specification (JLS). Thejshelltool is not intended to be an IDE, hence, graphical support and debugger are not supported.
Table Of Contents
1. Introduction
Java 9 is a major release. While writing this article, JDK 9 is currently available for early access download on the oracle site and is expected to be released on July 27, 2017. This document attempts to summarize details of JDK9 REPL and some of the main features with this new release.
Complete list of features in Java 9 can be viewed at the oraclesite.
You may skip setup sections if JDK 9 is already setup for you and jump directly to thefeatures section below.
2. Getting started with Java 9
To download the currently available early access JDK or JRE 9, visithttp://jdk.java.net/9/.
As shown in the image above, at the site, accept the license agreement and proceed to the download section as shown below.
Please select the appropriate OS and appropriate option for 32/ 64 bits for the OS to download the JDK/ JRE. It is also recommended to download the documentation along with the JDK/ JRE installation.
You may refer tothis article to get started with Java 9 by executing a simple hello world program.
3. What is REPL?
REPL stands for read-eval-print-loop and is a shell interface for users to test code snippets. This shell interface reads the input, evaluates and prints the output (and errors if applicable). This is similar to the REPL tool available in Clojure/ Scala. This is a useful tool for testing small code snippets before moving into writing complete code in IDE.
FromJEP222,jshellaims to provide an interactive tool to evaluate declarations, statements, and expressions of the Java programming language, together with an API so that other applications can leverage this functionality.
Code snippet written injshellmust correspond to any one of the below and must adhere to the Java Language Specification (JLS):
- Expression
- Statement
- Class declaration
- Interface declaration
- Method declaration
- Field declaration
- Import declaration
3.1 Jshell /help
The following sectionJava 9 REPL features has details of the commands onjshell. Before we look at the commands, below is the introduction fromjshellreceived by running/help intro on thejshellprompt.
jshell> /help intro| 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
Here are the shortcuts available injshell:
jshell> /help shortcuts|| shortcuts|| Supported shortcuts include:|| | After entering the first few letters of a Java identifier,| a jshell command, or, in some cases, a jshell command argument,| press the key to complete the input.| If there is more than one completion, then possible completions will be shown.| Will show documentation if available and appropriate.|| Shift- v| After a complete expression, hold down while pressing ,| then release and press "v", the expression will be converted to| a variable declaration whose type is based on the type of the expression.|| Shift- i| After an unresolvable identifier, hold down while pressing ,| then release and press "i", and jshell will propose possible imports| which will resolve the identifier based on the content of the specified classpath.
Also, we can set an evaluation context to thejshell commands.
jshell> /help context|| context|| These options configure the evaluation context, they can be specified when| jshell is started: on the command-line, or restarted with the commands /env,| /reload, or /reset.|| They are:| --class-path | A list of directories, JAR archives,| and ZIP archives to search for class files.| The list is separated with the path separator| (a : on unix/linux/mac, and ; on windows).| --module-path ...| A list of directories, each directory| is a directory of modules.| The list is separated with the path separator| (a : on unix/linux/mac, and ; on windows).| --add-modules [,...]| root modules to resolve in addition to the initial module.| can also be ALL-DEFAULT, ALL-SYSTEM,| ALL-MODULE-PATH.| --add-exports /=(,)*| updates to export to ,| regardless of module declaration.| can be ALL-UNNAMED to export to all| unnamed modules. In jshell, if the is not| specified (no =) then ALL-UNNAMED is used.|| On the command-line these options must have two dashes, e.g.: --module-path| On jshell commands they can have one or two dashes, e.g.: -module-path
All the above can be used for the commands explained in the section below.
4. Java 9 REPL features
4.1 Getting started
To openJShell, go to the JDK installed bin directory and click onjshell:

This is howjshell prompt looks:
5. REPL examples
Let’s get started with some simple examples to get started withjshell.
5.1 Examples with expressions
Let’s start with basicjava.lang.Math functions andSystem.out.println calls as shown in the snippets below. First we callMath.ceil method, followed byMath.floor method. These are standard methods injava.lang.Math class./vars command lists all the variables set so far. This prints the two variables created when theMathmethods were executed.System.out.printlncalls print the value being printed.
jshell> Math.ceil(10.1)$1 ==> 11.0jshell> Math.floor (11.6)$2 ==> 11.0jshell> /varsdouble $1 = 11.0double $2 = 11.0jshell> System.out.println("Hello world")Hello worldjshell> System.out.println ("with semi colon");with semi colonAs you can see, we can run expressions onjshelland values of variables can be viewed using the/var command.
5.2 Examples with method
Let’s now move on to a complete method onjshell.
We are going to type a simple method that prints “Hello World”. The method is namedprintHelloWorldand makes aSystem.out.println call.
jshell> void printHelloWorld() ...> { ...> System.out.println("Hello World") ...> }| Error:| ';' expected| System.out.println("Hello World")| ^Oops, we forgot a semi-colon! Let’s run it again with the semi-colon in place.
jshell> void printHelloWorld() ...> { ...> System.out.println("Hello World"); ...> }For quick typing, you may also hit tab button to get all possible completions.
Once this new method has been created, we can invoke it to see how it works.
jshell> printHelloWorld()Hello World
5.3 Examples with variables
To test variables, let’s try the below commands that assign a value to variablesi, j and then computes their sum(i+j). This is followed by printingi divided byj (i/j). Then, we assign two double variablesd1andd2and computed1 divided by d2 (d1/d2).
jshell> int i=10i ==> 10jshell> int j=20j ==> 20jshell> i+j$10 ==> 30jshell> i/j$11 ==> 0jshell> double d1 = 10d1 ==> 10.0jshell> double d2=20d2 ==> 20.0jshell> d1/d2$14 ==> 0.5
As you can see,jshellis a simple tool to test out variables and variable assignment.
5.4 Example with class
To test a class onjshell, let’s try the below code that creates a classEmployeewith attributesempId, name, salary. The class has a parameterizedconstructorand overriddentoStringmethod.
jshell> public class Employee...> {...> String empId;...> String name;...> Integer salary;...> public Employee (String empId, String name, Integer salary)...> {...> this.empId=empId;...> this.name = name;...> this.salary = salary;...> }...> public String toString ()...> {...> return "Employee [empId=" + empId + ", name=" + name + ", salary=" + salary + "]";...> }...> }This gives the below output:
| created class Employee
In effect, as we have seen in the sections above, we can test any of the below in REPL: expressions, methods, variables, class.
6. Commands
6.1 /var command
To see all the variables created so far, type/var
6.2 /method command
To see all the methods created so far, type/method
6.3 /import command
To see all imports that are included by default, type/import
6.4 /save command
To save the history, type/save filename
6.5 /list and /history commands
To view the list of all snippets created and history of command input try/list and /history respectively.
6.6 /help command
To view all commands type/help
6.7 /reset command
To reset state, type/reset
6.8 /exit command
To exit, type/exit
7. When to use REPL?
REPLjshellis a great way to get started with JDK 9 without needing eclipse or a complete working environment. Simple expressions, methods and classes can be tested on command line. We expect this tool to be very useful for new developers.
However, whether REPL will replace IDEs like IntelliJ or Eclipse seems unlikely. Nevertheless, for new developers who need to try out some language features this could fit their needs well.
8. Summary
This article aims to provide a start to Java 9 REPL features. JDK 9 has some exciting new features and REPL promises to change how we currently write java code by allowing us to test as we go.
9. References
https://docs.oracle.com/javase/9/whatsnew/toc.htm
https://www.infoq.com/news/2016/09/JavaOne-2016-Keynote-JShell
http://openjdk.java.net/jeps/222

Thank you!
We will contact you soon.



















