In this post of Java 9 Jshell Tutorial, we will talk and learn all the concepts related to JSHELL using various examples.
What is JShell in Java 9?
It is an interactive Java Shell tool that allows us to execute Java code from the shell and shows output immediately.JShell is a REPL (Read Evaluate Print Loop) tool and runs from the command line.
Advantages of JShell
Jshell reduces all the efforts that are required to run a Java program and test business logic.
Let’s say if you have to print the “Hello World” message then you have to create a first-class then the main method. Inside that main method, you have to use the println function. Almost you have to write 5 to 6 lines of code to print the “Hello world” message.
If we don’t use Jshell, creating of Java program involves many steps.
How to Start JShell ?
To start Jshell, first, we must have installed Java 9 on our machine then open a command prompt and type jshell, and press Enter. It will open a jshell session with a welcome message to the console.
Print “Hello World” Message:
To display a simple “Hello World” message, write the print function without creating class and method and hit enter.
Note: Putting Semicolon (;) at the end of any statement is optional
We can declare variables and use them anywhere throughout Jshell session. Let’s create some variables.
- The semicolon (;) is optional, we can leave it and it works fine. See, variable x created without using a semicolon
Int x =20
- If we don’t provide a variable name, JShell creates an implicit variable to store the value. These variables start with the $ sign. We can use these variables by specifying implicit variables.
We can type and test any valid Java expression to get instant output.
Create Class & Method:
- To create a class, write source code for the class and call its method by creating an object immediately.
- To test method business logic, create a method and get results immediately.
- By default, 10 packages are imported and can also be imported any package by using the import statement.
- Let’s see, default imported packages, we can use /imports command.
jshell> import java.sql.*;
- Listing import packages and will show available accessible packages.
- Now a number of packages are 11 including a new one sql.*.
If you go to the installation location and look into /jdk-9.x.x/bin folder. You will find the jshell.exe file in here.
- /help [<command>|<subject>]
get information about jshell
exit from jshell terminal
- /set feedback mode
set jshell configuration information
- /list [<name or id>|-all|-start]
list the source you have typed
- /reset [-class-path <path>] [-module-path <path>] [-add-modules <modules>]…
- /edit <name or id>
Use this command to edit any source entry referenced by name or id
- /drop <name or id>
You acn delete a any 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
list the imported items
view or change the evaluation context
history of what you have typed
re-run the last snippet
re-run snippet by id
That’s all about JShell in Java 9
You May Also Like:
If you have any feedback or suggestion please feel free to drop in below comment box.