Java 9 JShell tutorial

The Java JShell is a command-line tool that is introduced in Java 9 version. It helps to execute/test Java statements in a single line.

It is based on REPL (Read Evaluate Print Loop) that reads a code statement, execute that and print the output, statement after statement. It reads the statement and immediately shows the result in the next line.

If you are familiar with Scala or Python language, then you would know about the REPL tool.

It is a powerful tool that allows executing statements, expressions, class, loop, methods, etc.

We can use it to test our code or logic without writing code into a file and then compile it. It reduces the effort for just checking the code logic.

For example, if we want to print a simple Hello world message, first we need to save code into a file and then compile and execute it. While by using the JShell, we write the print a statement and get the result immediately.

Saving data into a file requires additional steps like compiling and executing the code while JShell does it quickly in a single step.

For more understanding, let’s take an example.

Output

Hello World

Hello World Example Using JShell

JShell

Now, we can easily understand that JShell has more advantages than writing code into a file. But it helps execute instant code only. To create an application, we will have to save source code into a file.

What can we do with JShell?

We can execute code that includes statements, loops, expressions, class, methods, etc.

It also provides several built-in commands to control the JShell environment. For example, to check the created Variables, we can use /vars command. We will discuss these commands later in this article.

How to start and stop JShell?

To start with JShell, open the command prompt in the case of windows and terminal for Linux and Mac OS and then type jshell command.

>jshell

It will open jshell in the same window with a welcome message with Java version. In case of windows, see the below screenshot.

start-jshell

To stop the JShell, just type the /exit command which will bring out to normal command prompt.

jshell> /exit
| Goodbye

Creating variable in JShell

After starting the JShell, let’s create some variables and print their values.

jshell> int a = 10;
a ==> 10
jshell> int b = 20;
b ==> 20
jshell> int multiply = a*b;
multiply ==> 200
jshell> System.out.println(multiply);
200
jshell>

If we don’t provide any variable and enter any value, then jshell will hold the value in a dynamic variable. See the example here. Java automatically creates these variables.

jshell> 30
$1 ==> 30
jshell> 50
$2 ==> 50

JShell as a Calculator

We can use JShell as a calculator to get instant result of numeric calculations. See the example:

jshell> 10+20
$1 ==> 30
jshell> 10*10
$2 ==> 100
jshell> 200/10
$3 ==> 20
jshell> 20+30+50+40
4 ==> 140

Creating Methods in JShell

Like variables, we can create our methods to perform business logic.

jshell> int multiply(int a, int b){
…> return (a*b);
…> }
| created method multiply(int,int)
jshell> int result = multiply(10,20);
result ==> 200

Creating Class in JShell

Like method, we can create class as well by typing class definition.

Here is an simple example of creating a class called Country

jshell> class Country {
…> String name;
…> Country(String name)
…> {
…> this.name=name;
…> }
…>
…> public String getCountryName()
…> {
…> return name;
…> }
…> }
created class Country

You can declare the objects as below:

jshell> Country c1= new Country(“India”);
c1 ==> Country@754ba872

jshell> c1.getCountryName();
$4 ==> “India”

As you can see, we have created a Country object named c1 and called getCountryName() method on it.

Executing Algebraic Expressions in JShell

We can execute any valid expression using the JShell to check the expression’s outcome.

jshell> a = 10
a ==> 10
jshell> b = 20
b ==> 20
jshell> (a*a)+(b*b)+2*a*b; // algebraic expression
$1 ==> 900

Executing Conditional Statements in JShell

We can test conditional statements like if-else to check program control flow instantly in the JShell.

jshell> int a = 20;
a ==> 20
jshell> int b = 50;
b ==> 50
jshell> if(a System.out.println(“A is less than B”);
…> }else{
…> System.out.println(“B is less than A”);
…> }
A is less than B
jshell>

JShell Commands

Apart from executing Java code, JShell provides some built-in commands to handle the code. Some of them we are discussing here.

List the Variables

To check all the created variables for our current session, we can use /vars command. It will populate all the variables.

jshell> /vars
| int sum = 30
| int $1 = 30
| int $2 = 50
| int $3 = 30
| int $4 = 100
| int $5 = 20
| int $6 = 140
| int result = 30
| int $7 = 900
| int a = 20
| int b = 50

List the Methods

To get a list of all the created methods in the current session, use /methods command.

jshell> /methods
| int multipy(int,int) jshell>

Import Java Packages

During starting JShell, it imports some packages by default to provide basic functionalities to the user. We can check these default packages by using the /import command.

jshell> /import
| import java.io.*
| import java.math.*
| import java.net.*
| import java.nio.file.*
| import java.util.*
| import java.util.concurrent.*
| import java.util.function.*
| import java.util.prefs.*
| import java.util.regex.*
| import java.util.stream.*
jshell>

Check history of commands

To check history of commands, we have executed so far, we can use /history command.

jshell> /import
int a=10;
int b=20;
int multiply=a*b;
System.out.println(multiply);
int multiply(int a,int b){
return (a*b);
}
jshell>

Edit code in JShell Edit Pad

If you just need to write few lines of code, you can use inline editor. But when you want to write quite a few line of code, then you can /edit command to change your code.
For example:
Let’s say you want to edit method multiply(), you can use /edit multiply command.

jshell> /edit multiply

You will see below JShell Edit pad where you can make changes.
jshell
Once you are done with changes, you can click Accept button to accept the changes.

Load external Java code to JShell

We can also load code from external file using /open command.
Let’s see with the help of example:
Here is content of file C://divide.java

Now we can use following command to load the file.

jshell> /open C://divide.java

openCommandOutput
As you can see variables x,y and method divide loaded into the jshell.

Drop variables,methods, classes from Jshell

We can drop variables,methods, classes from Jshell using /drop command.

Let’s say you want drop method divide from JShell. You can use following command

Now we can use following command to load the file.

jshell> /drop divide
| dropped method divide(int,int)

As you can see, method divide() dropped from the JShell.

JShell help

We can use /help command to get more information about the command.
Here is the screen shot for the /help command:

Conclusion

This tutorial is all about the Java JShell tool, which is introduced in Java 9. We discussed this tool with the help of several examples. We suggest you try this tool your own and enjoy Java coding in a new way.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *