REPL - Java JShell

Java 9 introduces JShell and a Read-Eval-Print-Loop (REPL) for the Java Programming Language. REPL allows you to evaluate code snippets such as declarations, statements, expressions. You can test your code as you create it, and way before you are done with your whole project.

JShell is an interactive tool to execute and evaluate java simple programs like variable declarations, statements, expressions, simple Programs without using the main method of Java.

JShell works similar to Python interpreter or other JVM languages that have a REPL like Scala, Kotlin and Groovy.

jshell-java

You can input specific JShell commands or Java code (referred as snippets), in particular:

  • Expressions
  • Statements
  • Class declarations
  • Interface declarations
  • Method declarations
  • Field declarations
  • Import declarations

Start JShell

To start the JShell, you need to check the java version installed in your system. You can check the java version by typing java -version if it is more than or 9 means it will work on your system. Otherwise, you might need to upgrade your Java/JDK version.

jshell-repl-java

  • Check whether Jshell is present in bin folder of Java install location
    C:Program Files\\Java\\jdk-10\\bin


    jshell-installed-folder

  • If not Jshell is not present then please upgrade or download the appropriate version of JDK.
  • Notice some error message in below image
    jshell-open-from-installed-folder
  • Yes, You must navigate to the folder where jshell.exe is present to type the jshell -v command to start the Jshell, -v (verbose) for getting more detailed messages about the execution
    After navigating the Jshell folder type jshell command start the jshell, you can see that in the above image.
  • (this step is for your info) You can also open the jshell by typing the $JAVA_HOME/bin/jshell, it is nothing but I am just providing the Java Path and opening the jshell without navigating to java folder.
how to quit/exit jshell:

You can exit jshell anytime by typing /exit, or you can use CTRL+D if you are using Windows. If you are using Mac, it is cmd-D.

exit-quit-jshell-java

Save and Load History in jshell

Sometime we might need to work on jshell but at the same time we might do not want to lose the code that we type into jshell, So it is better to have a copy of what we typed and we might load this save code late point of time.

Save jshell history:

To save the expression history the /save filename command can be used, the file will contain only the commands, not the outputs, The file be stored in Java bin folder

save-history-jshell-java

You need write and Read access for the folder otherwise you might get this error

| File 'commands_typed.java' for '/save' threw exception: java.nio.file.AccessDeniedException: commands_typed.java
Load History from a file in Jshell:

To load a previously saved file we can use the /open filename command

jshell-load-java

Getting Started with Jshell aka Java REPL

As it important to say hello Mars, so lets say hello mars using System.out.println()

hello-world-jshell-java

You can perform mathematical calculation on the Jshell.

mathematical-expressions-jshell-java-repl

JShell – Scratch variable

When we do not assign the result of an expression to a variable, a scratch variable is created in jshell so that the output of expression can be used later.

These scratch variables are created by name of $1, $2, $3 and so on. In the above image, you can see the $4, $5, $6 variables got created.

I am trying to use the scratch variable from in below image.

use-scratch-variable-for-later-use

Variables - Jshell

We can declare variables and use them later in Jshell. Variables can be defined with the normal Java declaration syntax, Once you do that variables become visible in the scope. Scope of the variable is same in any program.

variables-without-semicolon

If you carefully see the above image then you might notice that i have not used ; (semicolon), but still Jshell have not thrown any error.

Jshell does not throw error if no semicolon is used with variables

Default Imports in Jshell :

There are few packages available by default with jshell, you can see them by typing /imports command into your terminal as shown below:

imports-in-jshell

Here java.lang.* package is not listed in the above packages list. But as it is a default package, we can use its String class without any external imports.

JShell Autocomplete

JShell has a build-in autocomplete feature also called Tab-completion feature which allows you to minimize typing by just pressing the Tab key.

Start typing and press the Tab key. This will either complete the word you started typing if only one option is available or show a list of possible options. Look at the example below. We start to type System.out. and press the Tab key:

auto-suggestions-completion-jshell-repl

You can press Double-Tab to get all the suggestions that jshell can understand during tap

double-tab-toget-all-classes

Control flow statements in Jshell:

You can also write multi-line control flow statements in the REPL. JShell recognizes multi-line statements and prompts with a ...> symbol to let you enter the next line of the statement.

Example of If-else statement:

if-block-jshell

Yes, I did not write else statement, because after typing { I types enter so jshell thought I am just trying with if block.

To write if and else block after } symbol you need to write else keyword and hit enter.

if-else-block-jshell

Methods in Jshell - Java REPL:

In jshell, we can create ma ethod just like in programs, Let's define a method called sayHello(string name) to say a greeting to the user. The method will take a String of username and return number of letters present in the username.

You can start defining your method right at the command line, as if you were defining a method as part of a class, however, there's no need to define a class!

methods-in-jshell

You cannot create a static method in jshell, if you try you will get below error

Warning:
| Modifier 'static' not permitted in top-level declarations, ignored

You can also create private and protected methods in jshell.

private-methods-in-jshell

Classes in jshell

You create classes in JShell just like create methods, typing them, line-by-line until the class is complete. JShell then notifies you that it created the class.

create-class-jshell

Creating classes (and methods) in JShell can be painful. There's no formatting and making an error can be frustrating since you won't know you've made an error until you complete the class.

You can call the static method with class reference:

static-method-class-jshell

You call non-static methods as well in jshell.

non-static-method-jshell

Few More commands from jshell

Clear jshell console:

As of now (05 Apr 2019) jshell doenot have a command to clear the screen/console. So we have to exit the jshell and use cls (normal terminal clearing command) to clear the screen.

clear-jshell-console

History of commands in jshell:

We can get the history of commands that we have typed using /history command in jshell

history-of-commands-jshell

You can also get the list of commands that you have typed and which are related to your java using /list commands in jshell. This will not show the commands which are related to jshell like /history, /save ..

The list of commands comes with id(number on left) for the each command

list-of-commands-in-jshell

Bring up specific command using id:

Based on the id of the command we can use the ids to replicate the command using /id

ids-jshell-command

Drop command in jshell:

/drop id command will remove a command present at a particular id in the jshell.

drop-commad-jshel

Reset command in jshell:

/reset command will reset all the things that you have worked on jshell, which means it clears the history

reset-command-jshell

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions