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

JShell in Five Minutes

This primer for Java 9's REPL, JShell, will help you get started with variables, My Top Java 9 Features

Martin Farrell user avatar by
Martin Farrell
·
Oct. 13, 17 · Tutorial
Like (10)
Save
Tweet
Share
18.02K Views

Join the DZone community and get the full member experience.

Join For Free

This post builds on my My Top Java 9 Features post by looking more in depth at these features. Here we show you how you can learn JShell in five minutes, and improve your Java 9 development experience.

Getting Started

Assuming you have downloaded and installed Java 9 then you can start the shell by typing:

jshell


Or if you want verbose:

C:\jdk9TestGround>jshell -v
| Welcome to JShell -- Version 9
| For an introduction type: /help intro

jshell>


Variables

Simply type the variable, with or without semi-colons:

jshell> int i = 1;
i ==> 1
| created variable i : int


Unassigned values are automatically assigned to a variable beginning with $:

jshell> "Hello World"
$1 ==> "Hello World"
| created scratch variable $1 : String


This means we can reuse the value later:

jshell> System.out.println($1);
Hello World


Control Flows

The next step in JShell is to use control flows (for, if, while, …). We can do this by entering our condition, using return for each new line:

jshell> if ("Hello World".equals($1)) {
 ...> System.out.println("Woohoo my if condition works");
 ...> }
Woohoo my if condition works


A quick tip is to use TAB for code completion.

Methods

We can declare a method in a similar way as Flow control, and press for each new line:

jshell> String helloWorld() {
 ...> return "hello world";
 ...> }
| created method helloWorld()


Then call it:

jshell> System.out.println(helloWorld());
hello world


We can also change methods in our shell, and have methods calling methods that aren't defined yet:

jshell> String helloWorld() {
 ...> return forwardReferencing();
 ...> }
| modified method helloWorld(), however, it cannot be invoked until method forwardReferencing() is declared
| update overwrote method helloWorld()


Now we fix the method:

jshell> String helloWorld() {
 ...> return forwardReferencing();
 ...> }
| modified method helloWorld(), however, it cannot be invoked until method forwardReferencing() is declared
| update overwrote method helloWorld()


Classes

We can also define classes in JShell:

jshell> class HelloWorld {
 ...> public String helloWorldClass() {
 ...> return "helloWorldClass";
 ...> }
 ...> }
| created class HelloWorld


And assign and access them:

jshell> HelloWorld hw = new HelloWorld();
hw ==> HelloWorld@27a5f880
| created variable hw : HelloWorld

jshell> System.out.println(hw.helloWorldClass());
helloWorldClass


Imports

The current classes in the workspace can be seen using the /imports command.

Additional classes can be added through the –class-path, or –module-path:

jshell --class-path ...


Or you can use /env within the shell:

/env


Useful Commands

Now we’ve got the basics here are some quick commands:

Tab Code completion
/vars list of variables in the current shell
/methods list of methods in current shell
/list All code snippets in jshell session
/imports Current imports in the shell
/methods list of methods in current shell
/types Current classes defined in the shell, in the case above we would see “class HelloWorld”
/edit Lets you edit your session in an editor(default to JEditPad)
/exit close session

Conclusion

The real strength of the JShell REPL editor is you can test out snippets and API’s. This is really useful for saving time, and experimenting when learning Java9. In fact, even if you are not moving to Java 9 straight away its worth installing JDK9 to use JShell as a testing ground.

JShell

Published at DZone with permission of Martin Farrell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DevOps for Developers — Introduction and Version Control
  • 10 Most Popular Frameworks for Building RESTful APIs
  • LazyPredict: A Utilitarian Python Library to Shortlist the Best ML Models for a Given Use Case
  • Metrics Part 2: The DORA Keys

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: