beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java 9 JShell (Java Shell) – REPL

By Chaitanya Singh | Filed Under: Java 9 Features

JShell stands for java shell. It is also known as REPL (Read Evaluate Print Loop). The purpose of this tool is to provide a easy way to learn Java, but how? Lets look into it. We are aware that we have to write several lines of code to print something on screen, for example – To print “Hello World” on the screen we have to write the following code.

public class HelloWorld {

    public static void main(String[] args) {
        // Prints "Hello, World!" on the screen
        System.out.println("Hello, World!");
    }

}

This code is not easy to grasp if you are a java beginner. To learn java in a fun and interactive way, Oracle corp has come up with this tool called jshell. Such tools are already available in other popular programming languages such as Python, Scala etc.

To display the “Hello World” in JShell, all you have to write is this:

jshell> System.out.println("Hello, World!")
Hello, World!

JShell - Java Shell Hello World

Why use JShell?

The main advantage of using jshell is that you can test your partial code (individual statements, methods etc.) here without writing the complete program and then check the various possible scenarios. Once you are satisfied with the code then you can copy it from jshell to your main program.

This allows you to try multiple scenarios without breaking your main program and gives you an opportunity to try and learn.

How to Start JShell?

To start jshell, you must have java 9 installed on your system, if you have older version of java then you can upgrade it by downloading the latest version from here.

To start the jshell, open command prompt (open terminal if you are on Mac OS X), type jshell and hit Enter.

If you get the error like: jshell: Command Not found (as shown in the screenshot below) then you have to set the path on your System.
jshell Command Not Found

If everything is setup fine then you should see a screen like this.
Starting JShell on your System

How to exit JShell?

To stop JShell, type /exit and hit Enter.
How to stop exit JShell

More Tutorials on JShell

1. JShell – working with variables
2. JShell – working with methods

Reference:

Oracle JDK 9 Documentation

Leave a Reply Cancel reply

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

Java 9 Tutorial

  • Java 9 features
  • JShell
  • Immutable List
  • Immutable Set
  • Immutable Map
  • Private Methods in Interface
  • Try-With-Resources Enhancement
  • Java 9 -Diamond operator Enhancement
  • @SafeVarargs annotation
  • Java 9 - Stream API Enhancements
  • Java 9 Modules

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap