jshell – Java Shell

Filed Under: Java

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

jshell

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.


jshell> 10+5
$1 ==> 15

jshell> 10/5
$2 ==> 2

jshell> 10/3
$3 ==> 3

jshell> 10.0/3
$4 ==> 3.3333333333333335

jshell> 10*5
$5 ==> 50

jshell>

jshell, java shell

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:


jshell> 10+5
$1 ==> 15

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.

jshell internal variable

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.


jshell> $1
$1 ==> 15

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

jshell> System.out.print("$1 value now = "+$1)
$1 value now = 20
jshell> 

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.


jshell> /imports
|    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>

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.


jshell> Math.max(5,10)
$11 ==> 10

jshell> Math.min(5,10)
$12 ==> 5

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:


jshell> ByteBuffer buf = ByteBuffer.allocate(48);
|  Error:
|  cannot find symbol
|    symbol:   class ByteBuffer
|  ByteBuffer buf = ByteBuffer.allocate(48);
|  ^--------^
|  Error:
|  cannot find symbol
|    symbol:   variable ByteBuffer
|  ByteBuffer buf = ByteBuffer.allocate(48);
|                   ^--------^
jshell>

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


jshell> import java.nio.ByteBuffer

jshell> ByteBuffer buf = ByteBuffer.allocate(48);
buf ==> java.nio.HeapByteBuffer[pos=0 lim=48 cap=48]

jshell> 

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

Comments

  1. Rajeev Ranjan says:

    tab is not working in my system when we using jshell

  2. Vaibhav Jain says:

    Good Explanation. superb

  3. Deva says:

    Your tutorials superb. gud work dude.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages