JShell in Five Minutes
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 –
1 2 | jshell |
Or if you want verbose –
1 2 3 4 5 | 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 –
1 2 3 | jshell> int i = 1 ; i ==> 1 | created variable i : int |
Unassigned values are automatically assigned to a variable beginning with $ –
1 2 3 | jshell> "Hello World" $ 1 ==> "Hello World" | created scratch variable $ 1 : String |
This means we can reuse the value later –
1 2 | 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 –
1 2 3 4 | 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 –
1 2 3 4 | jshell> String helloWorld() { ...> return "hello world" ; ...> } | created method helloWorld() |
Then call it –
1 2 | jshell> System.out.println(helloWorld()); hello world |
We can also change methods in our shell, and have methods calling methods that arent defined yet –
1 2 3 4 5 | 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 –
1 2 3 4 5 | jshell> String forwardReferencing() { ...> return "forwardReferencing" ; ...> } | created method forwardReferencing() | update modified method helloWorld() |
Classes
We can also define classes in jshell –
1 2 3 4 5 6 | jshell> class HelloWorld { ...> public String helloWorldClass() { ...> return "helloWorldClass" ; ...> } ...> } | created class HelloWorld |
And assign and access them –
1 | /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 |
Published on Java Code Geeks with permission by Martin Farrell, partner at our JCG program. See the original article here: Introduction to Java Virtual Machine (JVM) Opinions expressed by Java Code Geeks contributors are their own. |