Java 9 introduces JShell and a Read-Eval-Print-Loop (REPL) for the Java Programming Language. REPL allows you to evaluate code snippets such as declarations, statements, expressions. You can test your code as you create it, and way before you are done with your whole project.
JShell is an interactive tool to execute and evaluate simple java programs like variable declarations, statements, expressions, simple Programs without using the main method of Java.
JShell works similar to Python interpreter or other JVM languages that have a REPL like Scala, Kotlin, and Groovy.
You can input specific JShell commands or Java code (referred to as snippets), in particular:
To start the JShell, you need to check the java version installed in your system. You can check the java version by typing java -version if it is more than or 9 means it will work on your system. Otherwise, you might need to upgrade your Java/JDK version.
bin folder of Java install locationC:\Program Files\Java\jdk-10
jshell -v command to start the Jshell, -v (verbose) for getting more detailed messages about the execution$JAVA_HOME/bin/jshell, it is nothing but I am just providing the Java Path and opening the jshell without navigating to the java folder.You can exit jshell anytime by typing /exit, or you can use CTRL+D if you are using Windows. If you are using Mac, it is cmd-D.
Sometimes we might need to work on jshell, but at the same time we might do not want to lose the code that we type into jshell, So it is better to have a copy of what we typed, and we might load this save code later point of time.
To save the expression history the /save filename command can be used, the file will contain only the commands, not the outputs, The file will be stored in the Java bin folder
You need write and Read access for the folder otherwise you might get this error
To load a previously saved file, we can use the /open filename command
It important to say hello to Mars, so let's say hello mars using System.out.println()
You can perform mathematical calculations on the Jshell.
When we do not assign the result of an expression to a variable, a scratch variable is created in jshell so that the output of the expression can be used later.
These scratch variables are created by the name of $1, $2, $3, and so on. In the above image, you can see the $4, $5, $6 variables got created.
I am trying to use the scratch variable from in below image.
We can declare variables and use them later in Jshell. Variables can be defined with the standard Java declaration syntax; once you do that variables become visible in the scope. The scope of the variable is the same in any program.
If you carefully see the above image, then you might notice that I have not used ; (semicolon), but still Jshell has not thrown any error.
Jshell does not throw error if no semicolon is used with variables
There are few packages available by default with jshell, you can see them by typing /imports command into your terminal as shown below:
Here java.lang.* package is not listed in the above packages list. But as it is a default package, we can use its String class without any external imports.
JShell has a build-in autocomplete feature also called the 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 System.out. and press the Tab key:
You can press Double-Tab to get all the suggestions that jshell can understand during tap
You can also write multi-line control flow statements in the REPL. JShell recognizes multi-line statements and prompts with a ...> symbol to let you enter the next line of the statement.
Example of If-else statement:
Yes, I did not write else statement, because after typing { I types enter so jshell thought I am just trying with if block.
To write if and else block after } symbol you need to write else keyword and hit enter.
In jshell, we can create a method just like in programs. Let's define a method called sayHello(string name) to say a greeting to the user. The method will take a String of username and return the number of letters present in the username.
You can start defining your method right at the command line as if you were defining a method as part of a class; however, there's no need to define a class!
You cannot create a static method in jshell if you try you will get the below error
You can also create private and protected methods in jshell.
You can create classes in JShell just like creating methods, typing them, line-by-line until the class is complete. JShell then notifies you that it created the class.
Creating classes (and methods) in JShell can be painful. There's no formatting and making an error can be frustrating since you won't know you've made an error until you complete the class.
You can call the static method with class reference:
You call non-static methods as well in jshell.
As of now (05 Apr 2019), jshell does not have a command to clear the screen/console. So we have to exit the jshell and use cls (standard terminal clearing command) to clear the screen.
We can get the history of commands that we have typed using /history command in jshell
You can also get the list of commands that you have typed and which are related to your java using /list commands in jshell. This will not show the commands which are related to jshell like /history, /save ..
The list of commands comes with an id(number on the left) for each command
Based on the id of the command we can use the ids to replicate the command using /id
/drop id command will remove a command present at a particular id in the jshell.
/reset command will reset all the things that you have worked on jshell, which means it clears the history
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.