JShell is the REPL (Read-Eval-Print Loop) for java introduced as part of JDK 9. It is an interactive command line interface that enables the users to execute a single statement at a time, JShell evaluates and prints the result of the statement. The statement can be any valid java statement ranging from declaration of a variable to a basic arithmetic operation ….. to defining a class.
JShell is a handy tool for the language learners and for experienced java programmers as well to try out something quickly. We all know the pain we went through when we started learning Java; to run a simple
System.out.println, we had to create a Class with main method, compile it and run it. Even the worse when we want to do it in IDE, have to create a Project, then package and class. It’s a kind of turn off for someone who’s experienced in Scala or Python and trying to learn Java. Now with the JShell, it’s a more seamless learning experience for beginners of the Java, you are not obligated to create a class and a main method and even liberated from the semicolon at the end of every statement.
JShell can be found in
bin directory of the JDK installation, can be launched from the command line interface of the operating system. I have used the windows 10. Below screen shot shows the command to launch JShell.
What can we do with JShell
- Perform simple arithmetic operations such as
5%2, Jshell evaluates and prints the result
- Print a string or any value by directly typing
System.out.print. We can declare a variable and then we can print it. The below image shows printing of String directly typed in
System.out.printand declaring a starting variable and then printing it. Once a variable is declared, JShell prints the variable name and value
- Execute a loop statement, print values from 0 to 9 using for loop
- Declare a method and call that method. In the example shown in the image, I have declared a method
printStringthat takes a String argument and prints it, then I called that method with the String value “Calling a method from JShell”; the method is executed and the string value is printed on the console. if we type any semantically wrong code, an error is shown. I have tried inserting an extra double quote inside the println after the variable name and I saw the error as below
- View the history of the statements executed with
- View the variables and the methods declared with
/mcommands$1 is the variable that is created as a result of the arithmetic operation 1+2. When user performs an operation without assigning the result to a variable, JShell creates a variable automatically, $1 and $2 are such variables and str is the variable for the String literal created.
- JShell evaluates the method dependencies, shows a warning if a method definition is not found. If we declare a method,
method1and if it calls an undefined method,
method2, JShell shows warning as shown in the image belowif we list the methods now with
/mcommand, its lists the methods printString and method1 as well it shows the warning for the missing method2. After creating the method2,
/mshows the methods printString, method1 and method2 without warnings.
- Update the value of variable by assigning a new value to it. We can not change the type of a variable without declaring it again with the new type. Below image shows an example of updating an int variable and creating a new variable with the same name and different type; the int variable is removed in this case
- Edit the history of the statements with
/editis performed, JShell Edit Pad (a pop up) comes with the history of the statements executed. We can edit one or more statements or can add/delete a statement. In the example shown below I have added a statement to include a new String variable, blogNow the list of variables shows the newly added String variable, blog
- Save all the statements executed to a file using
/save file_namecommand . File extension can be anything. The saved file can be opened using
- Create a Class, instantiate a class, call a method on the instance.
Jshell is a quick means to test code snippets. With the JShell java programmers are no more obliged to declare variables or methods inside a Class. JShell acts like a Unix shell for Java statements.
Hope this article helped in giving you a good understanding of how to use JShell.