- 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 to create scratch variables in JShell in Java 9?
85 Lectures 6 hours
JShell is a REPL interactive tool introduced in Java 9 to execute and evaluate simple java programs like variable declarations, statements, expressions, and the programs without using the main() method.
In JShell, any value returned by a snippet automatically saved into a scratch variable. These scratch variables can be represented by $. When we do not assign the result of an expression to a variable, a scratch variable is created in JShell so that an output of expression can be used later.
In the below code snippet, six scratch variables have created
C:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> 3+7 $1 ==> 10 jshell> 9-2 $2 ==> 7 jshell> 4*4 $3 ==> 16 jshell> 12/4 $4 ==> 3 jshell> 19%5 $5 ==> 4 jshell> String.valueOf($2) $6 ==> "7"
In the below code snippet, the "nonScratch" variable has created. It is not a scratch variable because it can't be represented with $.
jshell> String nonScratch = "Tutorialspoint" nonScratch ==> "Tutorialspoint" jshell>
In the below code snippet, "/vars" command can display both scratch and non-scratch variables for that particular session.
jshell> /vars | int $1 = 10 | int $2 = 7 | int $3 = 16 | int $4 = 3 | int $5 = 4 | String $6 = "7" | String name = "Tutorialspoint" jshell>
- Using Variables in JShell of Java 9
- How to create JShell instance programmatically in Java 9?
- How to define expressions, variables, and methods in JShell in Java 9?
- How to create a thread in JShell in Java 9?
- How to create wrapper objects in JShell in Java 9?
- JShell in Java 9?
- How to debug JShell in Java 9?
- How to create a class and object in JShell in Java 9?
- How to get JShell documentation in Java 9?
- How to reset the JShell session in Java 9?
- How to implement java.time.LocalDate using JShell in Java 9?
- How to implement JShell using JavaFX in Java 9?
- How JShell tool works internally in Java 9?
- How to import external libraries in JShell in Java 9?
- How to handle an exception in JShell in Java 9?