Java 9: Getting started with Jshell in JAVA 9

Table of contents
Reading Time: 3 minutes

Java Shell tool (JShell) has been introduced as a part of JAVA 9. It is a Read-Evaluate-Print Loop (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results. The tool is run from the command line. In this blog, we will learn about this interactive tool that can be used for learning the Java programming language and prototyping Java code.

Why Use JShell?
Using JShell, you can enter program elements one at a time, immediately see the result, and make adjustments as needed.

Java program development typically involves the following process:

  • Write a complete program.
  • Compile it and fix any errors.
  • Run the program.
  • Figure out what is wrong with it.
  • Edit it.
  • Repeat the process.

Starting Jshell from the command line:

~$ jshell -v
|  Welcome to JShell -- Version 9.0.4

We have used -v flag here to start our Jshell in verbose mode. Verbose mode is recommended when you have just started using Jshell else just writing Jshell would also work.
Existing Jshell:

jshell> /exit
|  Goodbye

This would close the Jshell for you.

Trying Out Snippets:
Terminating semicolons are automatically added to the end of a complete snippet if not entered. When you enter snippets, use the Tab key to automatically complete the item. If the item can’t be determined from what was entered, then possible options are provided.

  1. Creating expressions:
    When an expression is entered that doesn’t have a named variable, a scratch variable is created so that the value can be referenced later.
    jshell> 2+2
    $1 ==> 4
    |  created scratch variable $1 : int
  2. Creating variables:
    jshell> int sum = 3 + 3
    sum ==> 6
    |  created variable sum : int
  3. Creating methods:
    The example also shows the continuation prompt (…>) that is used when a snippet requires more than one line of input to complete:
    jshell> int sum(int num1, int num2) {
       ...> return num1 + num2;
       ...> }
    |  created method sum(int,int)
  4. Changing method definitions:
    The definition is easily changed by entering a new one, which overwrites the previous definition.
    jshell> int sum(int num1, int num2) {
       ...> return num1 * num2;
       ...> }
    |  modified method sum(int,int)
    |    update overwrote method sum(int,int)
  5. Creating Classes:
    Classes can be created in the following manner.
    jshell> class Student {
       ...> String name;
       ...> String getName(String name) {
       ...> return name;
       ...> }
       ...> }
    |  created class Student
    jshell> Student s = new Student();
    s ==> Student@5700d6b1
    |  created variable s : Student
    
    jshell> s.getName("test name");
    $11 ==> "test name"
    |  created scratch variable $11 : String
  6. Forward References:
    JShell accepts the undefined method definitions that reference methods, variables, or classes. This is done to support exploratory programming and because some forms of programming require it. In f below example, we have created a method for the calculation area of a circle that uses a variable PI and a method square that are still not defined. JShell allows the definition but warns of what is yet to be defined. The definition can be referenced, but if the execution is attempted, then it fails until all of the required elements are defined.
    jshell> float areaCircle(float radius) {
       ...> return PI * sqaure(radius);
       ...> }
    |  created method areaCircle(float), however, it cannot be invoked until variable PI, and method sqaure(float) are declared
    
    jshell> float PI = 3.14f;
    PI ==> 3.14
    |  created variable PI : float
    
    jshell> float sqaure(float num) {
       ...> return num * num;
       ...> }
    |  created method sqaure(float)
    |    update modified method areaCircle(float)
    
    jshell> areaCircle(
    areaCircle(   
    
    jshell> areaCircle(10);
    $4 ==> 314.0
    |  created scratch variable $4 : float
  7.  Tracing Exceptions:
    In an exception backtrace, feedback identifies the snippet and the location within the snippet where the exception occurred.

    The location within the code entered into JShell is displayed as #ID:line-number, where snippet ID is the number displayed by the /list command, and line-number is the line number within the snippet.

    jshell> int i =1/0;
    | java.lang.ArithmeticException thrown: / by zero
    | at (#1:1)
    
    jshell> /list
    1 : int i =1/0;
  8. Jshell imports:
    You can add new imports to JShell by directly typing the import statement. This would remain valid for that Jshell session.
    import java.util.Arrays;

Some Jshell commands that may come handy:

  • /help: To see all the possible methods available
  • /methods: To see all declared methods.
  • /imports: To see all the imports available in the session.
  • /list: List the source you have typed.
  • /vars: List the declared variables and their values.

Perhaps the greatest usage of JShell is to check quick code snippets and their execution in the shortest possible time. The tool will be very helpful for every programmer.

I hope, you have liked my blog. If you have any doubts or suggestions to make please drop a comment.
Thanks!


References:
Oracle docs on JDK 9

knoldus-blog-footer-banner

 

 

Written by 

Vinisha Sharma is a software consultant having more than 6 months of experience. She thrives in a fast pace environment and loves exploring new technologies. She has experience with the languages such as C, C++, Java, Scala and is currently working on Java 8. Her hobbies include sketching and dancing. She believes Optimism and a learning attitude is the key to achieve success in your life