JShell is an interactive shell for evaluating Java code snippets; this includes variables, expressions, statements, classes, methods, and other Java constructs. Java code snippets do not have to be evaluated in any context. For example, variable and method declarations may be created without creating a class. Top-level code snippets are distinguished from code snippets within enums, classes, and interfaces. Some JShell-specific commands are provided for the Java code snippets added, such as the /list command to list all code snippets added, and the /vars command to list all variables. The code snippets added to JShell in a session are evaluated and stored independently and are not appended or otherwise aggregated to form a Java class or some other Java construct.
As an example, a class declaration followed by a method declaration does not add the method to the class. Each code snippet must be added in completeness before Enter is selected to start its evaluation; Enter cannot be used to add partial code snippets to construct a complete code snippet. The variable, class, and other identifiers stored in a given session may be referenced by other code snippets in the same session. All top-level code snippets added in a JShell session are available to all other code snippets in the same session, regardless of any scope modifiers—public, private, protected—used. The “;” at the end of a declaration or expression may be omitted and is added implicitly. In this tutorial, we shall discuss using JShell in NetBeans 9. This article has the following sections:
- Starting JShell
- JShell Commands
- Importing Statements
- Declaring and Using Variables
- Creating a Variable Explicitly
To launch a JShell session, select Tools>Open Java Platform Shell, as shown in Figure 1.
Figure 1: Tools>Open Java Platform Shell
The Java Shell gets started and a command prompt gets displayed, as shown in Figure 2.
Figure 2: Java Shell started
JShell provides some commands, which may be listed with /help and are as follows.
-> /help | Type a Java language expression, statement, or declaration. | Or type one of the following commands: | /list [<name or id>|-all|-start] -- list the source | you have typed | /drop <name or id> -- delete a 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 | /imports -- list the imported items | /exit -- exit jshell | /reset -- reset jshell | /reload [-restore] [-quiet] -- reset and replay relevant | history -- current or previous (-restore) | /classpath <path> -- add a path to the classpath | /history -- history of what you have typed | /help [<command>|<subject>] -- get information | about jshell | /set start|feedback|mode|truncation|format ... -- set jshell | configuration information | /? [<command>|<subject>] -- get information about jshell | /! -- re-run last snippet | /<id> -- re-run snippet by id | /-<n> -- re-run n-th previous snippet
Some Java packages are imported by default when JShell is started. To list the default, import statements run the following command:
The output in Figure 3 lists the import statements included by default.
Figure 3: Import Statements included by Default
A Java code snippet that makes use of a Java type not imported by default would not get evaluated. As an example, add the following statement to JShell.
JFrame jframe=new JFrame("JShell");
The error message shown in Figure 4 gets generated. This indicates that the symbol class JFrame could not be found.
Figure 4: JFrame Class not found
Import the javax.swing package and run the preceding statement again. The JFrame class gets found and an error is not generated.
-> import javax.swing.*; -> JFrame jframe=new JFrame("JShell"); | jframe ==> javax.swing.JFrame[frame0,0,0,0x0,invalid,hidden, | layout=java.awt.BorderLayou ... ->
A variable in JShell is a storage location and has the same connotation as in Java code compiled with javac. A variable may be created implicitly or explicitly. JShell is an interactive tool and provides the functionality of implicitly creating variables from some types of expression snippets to store the expression’s value.
A variable may be created explicitly using a variable declaration as in a Java class source code file, as an example with the following statement.
The variable a is created and stored, as shown by the output in Figure 5.
Figure 5: Creating and Storing an Explicit Variable
The variable ‘a’ may be referenced in other code snippets. A System.out.println statement that prints a outputs 1 to the Output console (see Figure 6). Invoking a in JShell outputs the variable along with its value.
Figure 6: Referencing and Invoking a Variable
A variable type must be included in a variable declaration and is not deduced by JShell. As an example, the following code snippet does not create a variable and instead generates an error message.
-> a=1; | Error: | cannot find symbol | symbol: variable a | a=1; | ^ ->
A variable does not have to be initialized when declared and, as in a Java file code, is initialized to a default value. As an example, the following code snippet creates a variable b and initializes it to the default value for int, which is 0.
-> int b; | b ==> 0
Another value may be subsequently assigned to b.
-> b=5; | b ==> 5
One variable may be assigned to another variable.
-> b=a; | b ==> 1
The /list command is used to list all code snippets added in a JShell session thus far, as shown in Figure 7.
Figure 7: Listing Code Snippets
In this article, we started JShell in NetBeans 9 and used some its features, including importing statements and declaring and using variables explicitly. In a subsequent article, we shall discuss some more JShell features.