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.
Java Shell or jshell supports following features:
- We can use it as an interactive tool to evaluate declarations, statements, expressions etc. of the java programming language.
- We can see the history of our previous commands.
- jshell supports Tab-Completion feature.
- Java shell adds semicolons automatically so we don’t need to add them explicitly.
- We can configure default imports as per our requirements.
- 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>
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.
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.
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