Tutorial

Java REPL - jshell

Published on August 3, 2022
Default avatar

By Rambabu Posa

Java REPL - jshell

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Java REPL or jshell is the new tool introduced in java 9. Today we will look into Java REPL basics and run some test programs in jshell interface.

Java REPL

Let’s first try to understand why REPL support was added in Java, if it was that important then why in so late release. As you know, Scala has become very popular to develop from small to large-scale applications because of it’s features and advantages. It supports multi-paradigm (Object-Oriented and Functional Programming) and REPL. Oracle Corporation is trying to integrate most of Scala features into Java. They have already integrated some functional programming features as part of Java 8, such as lambda expressions. Scala’s one of the best features is REPL (Read-Evaluate-Print-Loop). It’s a command line interface and Scala Interpreter to execute Scala programs. It’s very easy to use Scala REPL to learn basics of scala programming and even run small test code. Because of Scala REPL and it’s benefits in reducing the learning curve and ease of running test code, Java REPL got introduced in java 9.

Java REPL - jshell

Java REPL application name is jshell. JShell stands for Java Shell. jshell is an interactive tool to execute and evaluate java simple programs like variable declarations, statements, expressions, simple Programs etc. Open command prompt and check java version to make sure you have java 9 or above, then only you can use jshell. java repl, jshell Since jshell don’t need any IDEs or extra editors to execute simple java programs, It’s very useful for beginners in core java and experts to use it to learn and evaluate new features and small test code.

Java REPL - jshell basics

We can access Java REPL by using jshell command available as shown in below image. java repl Now, it’s time to execute few simple java examples to get the taste of java REPL tool.

pankaj:~ pankaj$ jshell 
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro

jshell> 

jshell> System.out.println("Hello World");
Hello World

jshell> String str = "Hello JournalDev Users"
str ==> "Hello JournalDev Users"

jshell> str
str ==> "Hello JournalDev Users"

jshell> System.out.println(str)
Hello JournalDev Users

jshell> int counter = 0
counter ==> 0

jshell> counter++
$6 ==> 0

jshell> counter
counter ==> 1

jshell> counter+5
$8 ==> 6

jshell> counter
counter ==> 1

jshell> counter=counter+5
counter ==> 6

jshell> counter
counter ==> 6

jshell> 

java repl example As shown in the above Java REPL examples, it’s very easy to develop “Hello World” program. We don’t need to define “public class” and public static void main(String[] args) method just to print one message. NOTE: We don’t need to use “semicolons” for simple statements as shown in the above diagram.

Java REPL - execute class

We can also define and execute class methods in Java REPL shell.

jshell> class Hello {
   ...> public static void sayHello() {
   ...> System.out.print("Hello");
   ...> }
   ...> }
|  created class Hello

jshell> Hello.sayHello()
Hello
jshell> 

java repl class

Java REPL - Help and Exit

To get jshell tool help section, use /help command. To exit from jshell, use command /exit.

jshell> /help
|  Type a Java language expression, statement, or declaration.
|  Or type one of the following commands:
|  /list [<name or id>|-all|-start]
|  	list the source you have typed
|  /edit <name or id>
...

jshell> /exit
|  Goodbye
pankaj:~ pankaj$ 

We can also use Ctrl + D command to exit from jshell tool. That’s all about Java REPL and jshell tool basics, read more at jshell - java shell. Reference: JEP 222

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Rambabu Posa

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
October 27, 2017

how to set classpath of java 9 . thank you it’svery usefull

- Rajeev Ranjan

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    October 11, 2017

    It’s very Nice… Thank You.!!

    - Vinay

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      January 29, 2017

      Cool Feature it’s lot helpful for beginners to learn concepts

      - Ramesh

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        November 5, 2016

        Loved this tutorial. keep it up.

        - vega

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          October 13, 2016

          I would be interested in knowing how garbage collection would happen in JShell jvm processes. Users might create huge variables and forget about them… how would Java handle that intelligently?

          - Anjaneya

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            August 3, 2016

            Nice Tutorial with hands-on images and well narrated steps.

            - Balamurugan Guruswamy

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              July 18, 2016

              Thank you :)

              - Priya

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                January 30, 2016

                Very nice tutorial. Please postry more tutorials on java 9.

                - Rakesh

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  December 5, 2015

                  Mind-blowing tutorial on Java SE 9. explained very well. Please deliver some more tutorials on Java 9. I want to learn it before release. Thank you so much

                  - Suresh

                    Try DigitalOcean for free

                    Click below to sign up and get $200 of credit to try our products over 60 days!

                    Sign up

                    Join the Tech Talk
                    Success! Thank you! Please check your email for further details.

                    Please complete your information!

                    Get our biweekly newsletter

                    Sign up for Infrastructure as a Newsletter.

                    Hollie's Hub for Good

                    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

                    Become a contributor

                    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                    Welcome to the developer cloud

                    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

                    Learn more
                    DigitalOcean Cloud Control Panel