Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

JShell: The Java Shell and the Read-Eval-Print Loop

DZone's Guide to

JShell: The Java Shell and the Read-Eval-Print Loop

With Java 9 (hopefully) near, let's take a quick look at what you can do with the JShell command line tool and how it incorporates a more functional approach to Java.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

Let's talk about JShell. We can explore it with the JDK 9 Early Access Release. As of now, the general availability of JDK9 is scheduled for 27 July, 2017, and the JShell feature was proposed as part of JEP 222. The motivation behind it is to provide interactive command line tools to quickly explore the features of Java.

From what I've seen, it is a very useful tool to get a glimpse of Java features very quickly, which is especially useful for new learners. Already, Java is incorporating functional programming features from Scala. Consider the move to a REPL (Read-Eval-Print Loop) interactive shell for Java, just like Scala, Ruby, JavaScript, Haskell, Clojure, and Python.

JShell 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 JDK 9, set the PATH variable to access JShell. Below is a simple program using made using JShell. See how we don't need to write a class with the public static void main(String[] args) method to run a simple hello world application?

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 that will add two variables and invoke a method via JShell.

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.

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've enjoyed this glance at JShell. In my next article, we will see another JDK 9 feature. Until then, stay tuned!

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. 

Topics:
repl ,jshell ,java 9 ,java ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}