How to Use JShell in Java 9
How to Use JShell in Java 9
Want to learn more about using JShell in Java 9? Check out this post on using the basic commands in JShell with this quick tutorial.
Join the DZone community and get the full member experience.
Join For FreeJava-based (JDBC) data connectivity to SaaS, NoSQL, and Big Data. Download Now.
In this tutorial, we will learn how to perform the following in Java 9:
- How to start JShell
- Basic commands
- Unassigned and assigned variables
- Classes
- Methods
- If and For loop
- Imports
- JShell autocompletion
Start Console
The JShell can be started by a command:
jshell
The end result should look like the following:
test@test:~$ jshell
| Welcome to JShell -- Version 9-internal
| For an introduction type: /help intro
Basic Commands in JShell
Here, you can find several useful commands while working with JShell:
- /exit — if you want to quit the current session. Another option for Ubuntu Shell is by pressing: CTRL+Z
- /vars — list of all JShell variables — both unassigned and assigned (check section variables). The result should look like the following:
-> /vars
| int x = 10
| String $2 = "Java 9"
| Test ts = Test@42e26948
| int $11 = 10
| int $12 = 10
- /methods — list of JShell methods
-> /methods
| printf (String,Object...)void
| printX ()void
- /imports — all JShell imports
-> /imports
| import java.util.*
| import java.io.*
| import java.math.*
| import java.net.*
| import java.util.concurrent.*
| import java.util.prefs.*
| import java.util.regex.*
| import java.util.Arrays
- /classes — all JShell classes
-> /classes
| class Test
- /list — JShell snippets if any
-> /list
1 : int x = 10;
2 : "Java 9"
3 : System.out.println($2);
4 : class Test {
}
5 : Test ts = new Test();
6 : System.out.println(ts);
7 : void printX(){
System.out.println("X");
}
8 : printX()
9 : import java.util.Arrays;
10 : if(x > 0){
System.out.println(x);
}
11 : 10
Assigned and Unassigned/Temporary Variables
This is how the assigned variable are declared (notice that you don't need to put any semicolons):
- Assigned variable
-> int x = 10
| Added variable x of type int with initial value 10
- Unassigned variable
-> "Java 9"
| Expression value is: "Java 9"
| assigned to temporary variable $2 of type String
In order to access a temporary variable, you need to write: $2
-> System.out.println($2);
Java 9
Define Classes in JShell
If you want to add a new empty class test, then, you need to write only: class Test {}
-> class Test {
>> }
| Added class Test
Creating a new instance in JShell is the same as in Java and the access to the class methods and variables:
-> Test ts = new Test()
| Added variable ts of type Test with initial value Test@42e26948
-> System.out.println(ts);
REPL.$REPL12$Test@42e26948
Method Declaration in JShell
Declaring the new method can be done the same way as a class. You can start with the type and the method line-by-line, using semicolons and enter:
-> void printX(){
>> System.out.println("X");
>> }
| Added method printX()
You need to use semicolons with methods. The following is an executed method in JShell.
-> printX()
X
JShell Autocompletion
If you use TAB, you will find all possible commands starting with the current text:
-> print
printX() printf(
JShell Imports
You can add new imports to JShell by:
import java.util.Arrays;
To view all imports, use the /imports
command:
-> /imports
| import java.util.*
| import java.io.*
| import java.math.*
| import java.net.*
| import java.util.concurrent.*
| import java.util.prefs.*
| import java.util.regex.*
| import java.util.Arrays
JShell For and If
If, in JShell, there is an assigned variable:
-> if(x > 0){
>> System.out.println(x);
>> }
10
Here is the for
loop:
-> for(int i=0;i<5;i++){
>> System.out.println(i);
>> }
0
1
2
3
4
Connect any Java based application to your SaaS data. Over 100+ Java-based data source connectors.
Opinions expressed by DZone contributors are their own.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}