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

Java 9 (Part 2): JShell Step by Step

DZone's Guide to

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!

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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!

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

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

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}