DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. JShell: The Java Shell and the Read-Eval-Print Loop

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.

Siva Prasad Rao Janapati user avatar by
Siva Prasad Rao Janapati
·
Feb. 20, 17 · Tutorial
Like (4)
Save
Tweet
Share
8.27K Views

Join the DZone community and get the full member experience.

Join For Free

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!

JShell Java (programming language) shell

Published at DZone with permission of Siva Prasad Rao Janapati, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • GPT-3 Playground: The AI That Can Write for You
  • The Role of Data Governance in Data Strategy: Part II
  • How To Avoid “Schema Drift”
  • Top 5 PHP REST API Frameworks

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: