Java 9 JShell Example

This tutorial explains how to use the JShell tool in Java 9

The JShell tool, also called REPL (Read Evaluate Print Loop), allows you to execute Java code, getting immediate results. You can quickly evaluate expressions or short algorithms without creating a new project, compile or build it. With help of JShell you can execute expressions, use imports, define classes, methods and variables and use imports. Please note JShell is part of Java 9 JDK, not the JRE.

 

Start JShell

Open console or terminal (depending on your OS). Browse to your Java 9 JDK installation folder and cd into /bin folder. Run the jshell executable – jshell.exe if you are running on Windows OS

Start JShell

Start JShell




 

Simple Arithmetic Operations

With JShell you can easily test arithmetic operation by typing them in as Mathematics expressions. Following Example demonstrates this concept. You do not need to put semicolon (;) at end of line

JShell arithmetic operations

JShell arithmetic operations

Note the output of 37 / 2 = 18. This happens because 37 is treated as Integer by JShell so the result is cast to (int). Adding a decimal point to 37 will produce a Double result

 

JShell Internal Variables

JShell gives you immediate feedback by printing the result of your input. For example:

jshell> 1 + 3
$1 ==> 4

The expression 1 + 3 results in 4. JShell also assigns this value to an internal variable $1 . Further commands are assigned to incremental $ variables like $2, $3 etc.

Following screenshot demonstrates the usage of JShell internal variables:

jshell internal variables

jshell internal variables

You can print the value of a internal variable by typing its name like this:

jshell> $1
$1 ==> 4

You can assign a new value to your internal variable:

jshell> $1 = 20
$1 ==> 20

The expression 1 + 3 generates an Integer, right? So what will happen if you try to assign a value to internal variable of different type? Lets try to assign a double value of 15.8 to our $1 variable

jshell> $1 = 15.8

As you can see on the screenshot above this resolves into an “incompatible types error”. Once the internal variable is created, the type can not be changed anymore.

Internal variables are not only used to store primitive types. They work also with objects. In following example we create an internal variable of type String:

jshell> new String("Hello")
$4 ==> "Hello"

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 “Str” and press the Tab key:

jshell autocomplete function

jshell autocomplete function

 

JShell Custom Imports

By default following packages are imported in JShell:

  • java.io.*
  • java.math.*
  • java.net.*
  • java.nio.file.*
  • java.util.*
  • java.util.concurrent.*
  • java.util.function.*
  • java.util.prefs.*
  • java.util.regex.*
  • java.util.stream.*

Hint: you can list the imports by using the /imports command

You can easily import packages, which are part of Java SE. If you are looking for a package names, check the official Java 9 SE javadoc site

Following example demonstrates how to import java.time.LocalDate to create a new LocalDate object

jshell custom import

jshell custom import

 

JShell Example Program

A typical Java class to test a List reverse method will look like this:

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ListUtils {
	public static void main(String[] args) {
		List<String> list = Arrays.asList("A", "B", "C");
		System.out.println(reverse(list));
	}
	
	public static List<String> reverse(List<String> list) {
		List<String> listCopy = list.subList(0, list.size());
		Collections.reverse(listCopy);
		return listCopy;
	}
}

The code shown above has imports, main method and System.out.println() to show the results. We can skip all 3 in JShell. This is how we can test the same functionality with JShell:

jshell example

jshell example

As you will notice in the example above, we create the class as usual. Further, our input list is created outside of the class. See the immediate output after creating the list, showing it’s values

list ==> [A, B, C]

Now we just call the utility method in the class

jshell> ListUtils.reverse(list)

and the result output is displayed:

$3 ==> [C, B, A]

 

How to Exit JShell

You can exit jshell anytime by typing

jshell> /exit

or by pressing Ctrl+C

 

Many of the tutorials you find here are based on the book Mastering Java 9 written by Dr. Edward Lavieri and Peter Verhas. This book will provide you with complete and deep understanding of the new Java 9 concepts and tools.

 

Please drop me a comment if you like my post or have any issues/suggestions/type errors.

Thank you for reading my tutorials.

Happy Java SE 9 Learning!

5 1 vote
Article Rating
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Navneet
Navneet
4 years ago

Hi,

You have explained it very well.

Thanks for sharing.

Fal
Fal
2 years ago

really helpful