In this article, we will discuss jshell(Java Shell) Java 9 feature. We can explore jShell with JDK 9 Early Access Release. As of now the general availability of JDK9 is scheduled to 27th July 2017. The jShell feature is proposed as part of JEP 222. The motivation behind jshell is to provide interactive command line tools to explore the features of Java quickly. It is a very useful tool to get a glimpse of Java features very quickly for new learners. Already Java is incorporating functional programming features from Scala. In the same direction, they want REPL(Read-Eval-Print Loop) interactive shell for Java as Scala, Ruby, JavaScript, Haskell, Clojure, and Python.
The jshell
the tool will be a command-line tool with features like a history of statements with editing, tab completion, automatic addition of needed terminal semicolons, and configurable predefined imports.
After downloading the JDK 9, set the PATH variable to access jshell. Now, we will see how to use jshell. Below is the simple program using jshell. We no need to write a class with public static void main(String[] args) method to run a simple hello world application.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
C:\Users\ABC>jshell | |
| Welcome to JShell — Version 9-ea | |
| For an introduction type: /help intro | |
jshell> System.out.println("Say hello to jshell!!!"); | |
Say hello to jshell!!! | |
jshell> |
Now we will write a method which will add two variables and invoke the method via jshell.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jshell> public class Sample { | |
…> public int add(int a, int b) { | |
…> return a+b; | |
…> } | |
…> } | |
| created class Sample | |
jshell> Sample s = new Sample(); | |
s ==> Sample@49993335 | |
jshell> s.add(10,9); | |
$4 ==> 19 | |
jshell> | |
Now, we will create a static method with StringBuilder class without importing it, as jshell does that for you.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jshell> public class Sample { | |
…> public static void join() { | |
…> StringBuilder sb = new StringBuilder(); | |
…> sb.append("Smart").append(" ").append("Techie"); | |
…> System.out.println("The string is " + sb.toString()); | |
…> } | |
…> } | |
| created class Sample | |
jshell> Sample.join(); | |
The string is Smart Techie | |
jshell> |
I hope you enjoyed jshell feature. In the next article, we will see another JDK 9 feature. Till then, Stay Tuned!!!
nice one….
How would you say this compare to Beanshell?
Beanshell is an alternative. But, now it is dormant. It is still using JDK1.3, i guess. As jshell is part of java, it will be up to date along with java versions.