
- 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 to create scratch variables in JShell in Java 9?
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>
- Related Articles
- 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?
- How to create a class and object in JShell in Java 9?
- How to debug JShell in Java 9?
- 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 import external libraries in JShell in Java 9?
- How to handle an exception in JShell in Java 9?
- How to implement a String in JShell in Java 9?
- How to initialize an array in JShell in Java 9?
