JShell is the Java’s new Command Line Tool – REPL, which allows you to execute Java code and get immediate result. JShell is Java’s first REPL tool [Read-Evaluate-Print-Loop] which evaluates declarations, statements, and expressions which are entered and immediately shows the results.
What is the use of Java JShell?
Usually in Java world during development we will be
• Writing the complete code
• Compile it and fix any errors which has occurred
• Run the code , find and fix the missing pieces
• Repeat the above process until the code works properly
Jshell helps you to try the different options in the code while developing your program, We can test individual statements, complex statements, methods, APIs with the JShell.
JShell cannot replace an IDE, it just act as a try out editor during code development.
How to Start JShell ?
JShell is bundled along with Java 9, To start JShell open a command prompt and enter the command jshell.
C:\Users\jip>jshell | Welcome to JShell -- Version 9 | For an introduction type: /help intro
Make sure the JDK 9 is installed in your machine and jdk-9 is added to JAVA_HOME and jdk-9/bin to PATH variable.
Once the JDK is installed and the path is set correctly run the command java –version, it should be pointing to JDK 9.
C:\Users\jip>java -version openjdk version "9" OpenJDK Runtime Environment (build 9+181) OpenJDK 64-Bit Server VM (build 9+181, mixed mode)
Alternatively you can open JShell directly by, In the command prompt browse the JDK 9 installation directory and get into the /bin folder and run the jshell.exe executable.
Say for example if Java installed in “C:\Program Files\Java\jdk-9”, cd into the \bin folder and run jshell.exe
How to Exit JShell ?
In order to exit from the JShell all you need to do is run the command /exit , you will be out of JShell.
JShell Hello World
Let’s write our first Hello World! Program using JShell, type the below code and hit enter
System.out.println(“Hello World!”)
That’s all, we ran our first Hello World program using JShell. We don’t need to create a class and define a main method and print the Hello World inside the main method. Just type print statement, JShell gives you the result immediately.
Variables and JShell Internal Variables
Defining a variable in JShell is similar to that of how you define programmatically. The only difference is that you don’t need to write the class and method here.
jshell> int a = 25 a ==> 25
Just type the variable to get the value assigned to the variable
jshell> a a ==> 25
When you are evaluating an expression, JShell assigns the results to a JShell internal variable which will be prefixed with $, Let’s look into the below snippet
jshell> 10+20 $3 ==> 30
You can also refer the internal variable in the manipulation until the session is killed.
jshell> $3+10 $4 ==> 40
Once the internal variable is created, the type of internal variable cannot be changed. If we try to assign a decimal value to $3 we will get incompatible types error
jshell> $3 = 12.2 | Error: | incompatible types: possible lossy conversion from double to int | $3 = 12.2 | ^--^
We can even concatenate two String like below
jshell> "Hello" + "World" $5 ==> "HelloWorld"
You can test toUpperCase() method on the string like this
jshell> $5.toUpperCase() $6 ==> "HELLOWORLD"
JShell Methods
We can define the methods in JShell the same way we define in the Classes.
jshell> int multiply(int a, int b){ ...> return a * b; ...> } | created method multiply(int,int)
Once the method is created, the method will be available until we quit the session
jshell> multiply(2,3) $7 ==> 6
JShell Creation of Classes and Objects
In JShell, we are not limited to create statement, functions and variables. We can also create class and create object for the class just like a normal java program.
Lets create Multiply class
jshell> class Multiply { ...> private int a; ...> private int b; ...> Multiply () { } ...> Multiply (int a, int b) { ...> this.a = a; ...> this.b = b; ...> } ...> int multiply (int a, int b) { ...> return a * b; ...> } ...> } | created class Multiply
We can create an object for the Multiply class and call the multiply() method.
jshell> Multiply m = new Multiply (); m ==> [email protected]
jshell> m.multiply (2,3) $7 ==> 6
JShell Commands
Lets take a look at some of the Jshell commands
/list – Displays the list of code snippets typed
This command lists the source code which we have typed
jshell> /list 1 : int a = 25; 2 : a 3 : 10+20 4 : $3+10 5 : "Hello" + "World" 6 : $5.toUpperCase() 7 : int multiply (int a, int b) { return a * b; } 8 : multiply ( 2, 3)
/vars – Displays the list of all the variables
This command lists the varaibles which we have used in the current JShell session.
jshell> /vars | int a = 25 | int $3 = 30 | int $4 = 40 | String $5 = "HelloWorld" | String $6 = "HELLOWORLD" | int $8 = 6
/methods
This command displays the list of methods which we have created in the particular session
jshell> /methods | int multiply(int,int)
/history – View History
This command displays the complete history of the jshell session
jshell> /history int multiply(int a, int b) { return a * b; } /methods /history
/save – Saving a snippet to the file
Saves snippet source to a file.
jshell> /save E:\JIP\jshell.txt
For example, The above code creates a new jshell.txt under the specified path, with the below content
int multiply(int a, int b) { return a * b; }
/edit – Editing the code in an External Editor
This launches the Jshell edit pad where you can edit the code snippet and save it
When you have multiple snippets then you specify the snippet number, after the edit command to edit the particular snippet
eg : /edit 2
/drop – Dropping a Snippet
You can drop a particular snippet you have typed by using the command /drop <id>.
jshell> /list 1 : int multiply (int a, int b) { return a * b; } 2 : int add (int a, int b) { return a+b; } jshell> /drop 2 | dropped method add(int,int) jshell> /list 1 : int multiply (int a, int b) { return a * b; }
Tab Completion
This is an interesting feature of JShell, while entering the code you can hit Tab key to autocomplete the remaining or show the list of possibilities. It can even the show the documentation if any
For example, we have method called multiply(), while entering mu just press the Tab key Jshell will automatically completes it for you.
jshell> /methods | int multiply(int,int) jshell> mul <Press Tab Key> multiply( jshell> multiply( multiply( Signatures: int multiply(int a, int b) <press tab again to see documentation> jshell> multiply( int multiply(int a, int b) <no documentation found> <press tab again to see all possible completions; total possible completions: 540>
Leave a Reply