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!
Join the DZone community and get the full member experience.
Join For FreeIn 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!
Java 9 has the following new features:
- Java 9 REPL (JShell)
- Factory methods for immutable List, Set, Map, and Map.Entry.
- Private methods in Interfaces.
- Java 9 module system.
- Process API improvements.
- Try With Resources improvement.
- CompletableFuture API improvements.
- Reactive Streams.
In this tutorial, we are going to go from zero into a working JShell.
Our plan:
Download Java 9 from scratch and install.
Run the shell.
Get help from Java 9's REPL.
Run a few calculations in JShell.
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!
Opinions expressed by DZone contributors are their own.
Comments