Earlier we looked into Java REPL i.e. jshell basics. Today we will learn some more features of jshell or java shell.


jshell or Java Shell was introduced in Java 9 to help developers run short programs quickly without going into hassle of creating classes, main method etc. We can simply declare variables, write statements and execute them in jshell. We can also compile class for the current jshell session and then use it in the next statements. We will look into some more jshell features in this tutorial.

jshell features

Java Shell or jshell supports following features:

  1. We can use it as an interactive tool to evaluate declarations, statements, expressions etc. of the java programming language.
  2. We can see the history of our previous commands.
  3. jshell supports Tab-Completion feature.
  4. Java shell adds semicolons automatically so we don’t need to add them explicitly.
  5. We can configure default imports as per our requirements.
  6. We can configure default definitions as per our requirements.

We will explore these features one by one in the coming sections with some useful examples.

Java Shell Arithmetic Operations

We can perform arithmetic operations in jshell, as shown in below example.

If we observe the above image, we can find that it’s very easy to test arithmetic operations in Java Shell. We can write them like simple mathematics expressions and get the results, no need to use semicolons also.

Java Shell Internal Variables

If we go through the previous section image again, we can clearly observe the following code output:

Here jshell is assigning a new internal variable “$1” to hold the result value of ‘+’ arithmetic operation. Further operations results are also assigned to variables $2, $3 and so on. You can access these variable values by typing theie name in java shell.


I have one quick question for you. The java shell internal variables are final or Immutable? i.e. We cannot change it’s value?

Let’s find it out with a simple example.

So these internal variables are mutable, we can change their values as shown in above code. Also notice that we can use these variables in java statements, just like any other variable.

jshell Tab-Completion feature

Java Shell supports tab key in very nice way to avoid typing lot of characters and save developers time. It’s just like IDE support to auto complete of variable, function or class name.

Let’s explore this feature now with REPL. Just type couple of characters and press “Tab” key from your keyboard, it will provide you available hints to select. If there are more hits, it displays all of them as shown below. So type few more characters to avoid conflicts.

Below image shows jshell tab completion feature example.

jshell tab completion

Java Shell Import Types

Java shell automatically imports some packages to help us. We can use /imports command to list out all the packages imported by default.

We can use any class from these packages without importing them explicitly. That’s why we can use Math class without importing it, as shown in below example.

However, if we want to test any other package related class or interface, then we need to use external import. Otherwise we will get error as shown in the below code snippet:

We need to use import statement as shown below to import that whole package or specific class.

Here we are able to create a ByteBuffer object successfully without any issues because we have imported that class manually.

That’s all about jshell or java shell. I have grown fond of this tool because it help me in saving time by running some small test code quickly.

Reference: Oracle Documentation

By admin

Leave a Reply

%d bloggers like this: