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

JShell in Five Minutes

DZone's Guide to

JShell in Five Minutes

This primer for Java 9's REPL, JShell, will help you get started with variables, classes, methods, imports, and some useful commands.

· Java Zone
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

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:

Variables

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

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

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:

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:

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:

Classes

We can also define classes in JShell:

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.

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.

Topics:
jshell ,java ,repl ,command line ,tutorial

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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}