
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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
- Related Articles
- How to re-execute existing snippets in JShell in Java 9?\n
- 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 load a source code into JShell in Java 9?
- How can we import a gson library in JShell in Java 9?\n
- How can we get an ID of the running process in Java 9?
- JShell in Java 9?
- How to debug JShell in Java 9?
- How to get JShell documentation in Java 9?
- What are the rules we need to follow in JShell in Java 9?
- How to create JShell instance programmatically in Java 9?
- How to reset the JShell session in Java 9?
- How JShell tool works internally in Java 9?\n

Advertisements