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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Java
  4. Java 9 (Part 2): JShell Step by Step

Java 9 (Part 2): JShell Step by Step

JShell is finally bringing read-eval-print loops to Java! With it, you can code and run functions without executing javac, and it's a great calculator, too!

Tomer Ben David user avatar by
Tomer Ben David
·
Apr. 22, 17 · Tutorial
Like (39)
Save
Tweet
Share
32.45K Views

Join the DZone community and get the full member experience.

Join For Free

In Part 1 of this Java 9 tutorial series, we covered Java 9 modules. Here in Part 2 of this series, we are going to explore JShell. Any up-to-date programming language has a REPL. In today's world, where you have two-week sprints and you need to learn while coding, it's important to have a fast feedback loop. You don’t want to set up a whole project just to test something in the code, right? The REPL brings that to you. REPLs are common in Scala, Python, Ruby, Shell scripts, and now also in… Java!

Image title


Java 9 has the following new features:

  1. Java 9 REPL (JShell)
  2. Factory methods for immutable List, Set, Map, and Map.Entry.
  3. Private methods in Interfaces.
  4. Java 9 module system.
  5. Process API improvements.
  6. Try With Resources improvement.
  7. CompletableFuture API improvements.
  8. Reactive Streams.

In this tutorial, we are going to go from zero into a working JShell.

Our plan:

  1. Download Java 9 from scratch and install.

  2. Run the shell.

  3. Get help from Java 9's REPL.

  4. Run a few calculations in JShell.

  5. Define a function and use it.

Step 1: Download Java 9

Although we already covered downloading Java 9, we always want to start from the ground floor. We don’t want to force you to go to another tutorial in order to get any step, so here is how to download it into your macOS.

Go to: http://jdk.java.net/9/ and click on the JDK relevant to your favorite and used OS.

And after downloading it, just click it to install (if you are on macOS) and verify you have it installed:

tomerb@tomerb-mac.local:~$ java --version
java 9-ea
Java(TM) SE Runtime Environment (build 9-ea+164)
Java HotSpot(TM) 64-Bit Server VM (build 9-ea+164, mixed mode)


And it’s up and running.

Step 2: Run JShell

Running JShell is just... typing jshell in your Terminal. Let's try it:

$ jshell
|  Welcome to JShell -- Version 9-ea
|  For an introduction type: /help intro

jshell> 


Cool.

Step 3: Get Help

Type /help to get a list of things you can do with JShell:

jshell> /help
|  Type a Java language expression, statement, or declaration.
|  Or type one of the following commands:
|  /list [<name or id>|-all|-start]
|  list the source you have typed
|  /edit <name or id>
|  edit a source entry referenced by name or id
|  /drop <name or id>
|  delete a source entry referenced by name or id
|  /save [-all|-history|-start] <file>
|  Save snippet source to a file.
|  /open <file>
|  open a file as source input
|  /vars [<name or id>|-all|-start]
|  list the declared variables and their values
|  /methods [<name or id>|-all|-start]
|  list the declared methods and their signatures
|  /types [<name or id>|-all|-start]
|  list the declared types
|  /imports 
|  list the imported items


One of the interesting commands above is /imports. Let's see if we have some by default:

jshell> /imports
|    import java.io.*
|    import java.math.*
|    import java.net.*
|    import java.nio.file.*
|    import java.util.*
|    import java.util.concurrent.*
|    import java.util.function.*
|    import java.util.prefs.*
|    import java.util.regex.*
|    import java.util.stream.*


Yes, we do: some regexp, math, and io. Cool.

Step 4: Basic Calculations

Let’s try out some basic calculations and see how our shell works as a nice little calculator (you can uninstall the one you have):

jshell> 1+1
$1 ==> 2

jshell> 5%7
$2 ==> 5

jshell> 13%3
$3 ==> 1

jshell> $2
$2 ==> 5

jshell> $2 // we got variable number two!
$2 ==> 5


So every result was placed in a numeric var starting with $.

Step 5: Let’s Code a Function

Let's define a small function that takes an int and doubles it. Then let's call it.

jshell> int doubleThis(int i) { return i*2; }
|  created method doubleThis(int)

jshell> doubleThis(5)
$7 ==> 10


Amazing, so we are able to quickly define functions and use them from within the shell. In general, everything looks so smooth.

Summary

After using modules in the previous tutorial, we went on to investigate Java 9 by checking out its REPL. We started again from ground zero. We downloaded Java 9 from scratch, then ran JShell, the new REPL provided by Java 9. We then ran some calculations, saw the variable returned, saw the default imports by the shell, and then quickly defined a function and used it, all without executing javac or java within the REPL. Way to go Java!

Java (programming language) JShell

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Set Up and Run Cypress Test Cases in CI/CD TeamCity
  • How To Select Multiple Checkboxes in Selenium WebDriver Using Java
  • File Uploads for the Web (2): Upload Files With JavaScript
  • Best Navicat Alternative for Windows

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: