- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we execute snippets by ID in JShell in Java 9?
JShell is an interactive tool (REPL) introduced in Java 9. We can execute snippets like expressions, variables, methods, classes, and etc without a main () method in the JShell tool.
We can execute any prior snippet by simply typing /id, which indicates the snippet’s ID. For instance, if we type "/1", then JShell can display the first snippet that we have entered, executes it, and shows the result. We can re-execute the last snippet that we have entered (whether it is valid or invalid) by using "/!".
In the below code snippet, we have created a set of snippets, and execute those snippets by using /1, /2, /3, and /4.
C:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro 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 able to re-execute the last snippet by using "/!" command.
jshell> 2+5 $1 ==> 7 jshell> 10-6 $2 ==> 4 jshell> /1 2+5 $3 ==> 7 jshell> /! 2+5 $4 ==> 7
- How to re-execute existing snippets in JShell in Java 9?
- How to print previously typed snippets in JShell in Java 9?
- How can we implement a map in JShell in Java 9?
- How can we avoid compilation errors in JShell in Java 9?
- How can we customize the start of JShell in Java 9?
- How can we import a gson library in JShell in Java 9?
- How can we load a source code into JShell in Java 9?
- JShell in Java 9?
- How can we get an ID of the running process in Java 9?
- How to debug JShell in Java 9?
- How JShell tool works internally in Java 9?
- How to get JShell documentation in Java 9?
- Using Variables in JShell of Java 9
- Package Imports in JShell of Java 9
- How to create JShell instance programmatically in Java 9?