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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL

Trending

  • Beyond the Wall of Charts: How Advanced Observability Displaced the Telemetry Dashboard
  • Taming Parameter Sniffing in SQL Server 2022 With PSP Optimization
  • One Database, Two Models: Building AI-Friendly Java Applications With MySQL JSON Duality Views
  • Grounding AI Agents in Governed Data

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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!

By 
Tomer Ben David
·
Apr. 22, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
33.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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:

[email protected]:~$ 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.

Related

  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook