Jshell : Java REPL

A good programmer is always lazy when it comes to writing the code. They always try to complete their functionality in minimum lines of code. The same thing is expected by them when it comes to debugging or learning new features. In order to fulfill this requirement, almost every programming language has a REPL, a tool which has proved to be of great importance, especially for the beginners.

Until 1.8 version, Java provided no such facility, thus, making it a bit hard for the programmers. Just for testing some lines of code, a new class and several methods need to be created, making the task hideous.

But from 1.9 version onwards, now, Java developers also got this exciting feature. All their prayers were answered with the introduction of a Java Shell tool, commonly known as Jshell. So, let’s have a look and see how we can take advantage of this.

How to start and stop the jshell?

jshell

The first and the most simple thing is to start jshell just by typing the command on the terminal. You will be displayed the version which you are currently using.

Knoldus blog

And you can easily stop it by using the command(s) :

/exit

OR

/ex
Knoldus Jshell Blog

Features in Jshell

Now, let’s explore some of the new features provided by jshell.

  1. /var : This command is used to get the list of all the variables used so far. As shown in the image below, while performing the calculations, jshell created implicit variables. As soon as we typed the command /var, we get all the variables declared so far i.e. $1 and $2.
    jshell-ex1
  2. /types [option] : This command displays the type of all the classes, interfaces and enums. The [option] can be a specific name or id, of which you want to see the type. In the screenshot below, we have declared a class named as Demo1, so accordingly, we got the results :
    jshell-ex2
  3. /methods :  This will provide you all the methods declared so far. For example, here we have created a method i.e. addition().
    jshell-ex3
  4. /list : This is one of the most useful command. This provides us all the snippets created so far.
    Jshell-ex4

Similarly, there are many more commands that helps to make programming fun and easy to learn.

References :

https://docs.oracle.com/javase/9/jshell/introduction-jshell.htm

Written by 

Jasmine is a Software Consultant, with an experience of more than 11 months. She is familiar with Object Oriented Programming Paradigms and has a good command over C/C++. Apart from being a programmer, She is also fond of relational database technologies such as PostGreSQL & MySQL. Apart from that, work life balance is one of the most important thing for her. Her hobbies include watching netflix,chatting ,surfing net and writing.

Leave a Reply

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!