How to re-execute existing snippets in JShell in Java 9?

JavaObject Oriented ProgrammingProgramming

JShell is the first REPL tool introduced in Java 9. We can able to execute simple snippets in a command-line prompt by using JShell tool. We can start a JShell session by typing "jshell" command, stop the session by typing "/exit" command, and search for particular command by using "/help" command.

The "/reload" command can be used to re-execute all existing snippets in JShell. We can also remove all prior code from a JShell session by using the "/reset" command.

In the below code snippet, we have created a set of snippets.

jshell> 2+10
$1 ==> 12

jshell> String s = "Tutorialspoint"
s ==> "Tutorialspoint"

jshell> System.out.println("Tutorialspoint")
Tutorialspoint

jshell> int num1 = 25
num1 ==> 25

jshell> /1
2+10
$5 ==> 12

jshell> /2
String s = "Tutorialspoint";
s ==> "Tutorialspoint"

jshell> /3
System.out.println("Tutorialspoint")
Tutorialspoint

jshell> /4
int num1 = 25;
num1 ==> 25

In the below code snippet, we can apply the "/reload" command. Jshell tool re-executes all existing snippets and prints it.

jshell> /reload
| Restarting and restoring state.
-: 2+10
-: String s = "Tutorialspoint";
-: System.out.println("Tutorialspoint")
Tutorialspoint
-: int num1 = 25;
-: 2+10
-: String s = "Tutorialspoint";
-: System.out.println("Tutorialspoint")
Tutorialspoint
-: int num1 = 25;
-: int num1 = 25;

In the below code snippet, we can apply the "/reset" command to remove all prior code from a JShell session and prints "Resetting State".

jshell> /reset
| Resetting state.

jshell>
raja
Published on 23-Mar-2020 13:36:57
Advertisements