BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

java 9 JShell – Working with variables

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In the last tutorial we learned about JShell, the newly introduced feature of java 9. In this guide, we will see how to work with variables in JShell.

JShell – Scratch variable

When we do not assign the result of an expression to variable, a scratch variable is created so that the output of expression can be used later. These scratch variables are created by name of $1, $2, $3 and so on. Lets have a look at the following examples to understand it better.

jshell> 1+2
$1 ==> 3

jshell> 2+5
$2 ==> 7

jshell> System.out.println("sum of the two scratch variables is:"+($1+$2))
sum of the two scratch variables is:10

jshell> $1+$2
$4 ==> 10

Here the output of the expression 1+2 is assigned to a newly created scratch variable $1 and similarly the result of second expression is assigned to $2.
As you can see we have used the scratch variables $1 and $2 in the print statement and in another expression ($1+$2).

jshell scratch variables

JShell – variables

We can also name the variables in JShell, just like we do in the actual java program.

jshell> int var = 100
var ==> 100

jshell> int num = var - 90
num ==> 10

jshell> System.out.println(num)
10

jshell> String num
num ==> null

jshell> System.out.println(num)
null

In the above example, we have created a variable var and assigned the value 100 to it.
In the next step, we have created another variable num.

Note: We can also change the type of the variable, as you can see in the above example that we have changed the type of variable num from int to String. Doing this would change the value of the variable to the default value of the changed data type, in our example we have change the num from int to String so that value of num is changed from 10 to null.

This is especially useful when you do not want to create unnecessary variables and reuse the ones which you have already created.
JShell variables

Top Related Articles:

  1. Java 9 Features with Examples
  2. Java 9 – @SafeVarargs Annotation (with examples)
  3. jshell: Command Not Found on Mac OS X
  4. Java 8 – Get Current Date and Time
  5. Can Static Methods be Overloaded or Overridden in Java?

Tags: Java-9

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap