JShell is a command-line shell for running Java code. In an earlier article, “Using JShell in Java 9 in NetBeans 9.0, Part 1,” we discussed starting JShell in NetBeans 9. We tested some code snippets for importing classes and creating and using variables explicitly. In this continuation article, we shall discuss creating and using variables implicitly, performing String comparisons, and running Java statements. This article has the following sections:
- Declaring and Using Variables (continued)
- String Comparison
- Using Statements
- Resetting the Java Shell
In the first of two articles, we discussed declaring and using variables explicitly. Next, we shall discuss declaring and using variables implicitly.
Creating a Variable Implicitly
Creating a variable implicitly and storing it in a JShell session is a feature unique to JShell because a Java source code file does not support specifying expressions that are implicitly stored as variables. As an example, add the following code snippet, which is just the number 1.
A variable gets created implicitly, as shown in Figure 1. By default, an implicitly created variable has the name $x, in which x is the variable identifier. Adding 1 creates the variable $1, as shown in Figure 1, but the variable identifier is arbitrary to some extent because the first choice may already be in use by another variable.
Figure 1: Creating a Variable Implicitly
The new variable $1 may be invoked in JShell to output its value, as shown in Figure 2.
Figure 2: Invoking Implicit Variable
Any expression that yields a value is stored in a variable. As an example, adding two numbers creates a variable implicitly.
-> 1+0 | $4 ==> 1
The implicit variable may be invoked in JShell.
-> $4 | $4 ==> 1
The implicit variable may be used in another code snippet. As an example, add the two implicit variables $1 and $4, and a third implicit variable gets created.
-> $1+$4 | $8 ==> 2
The output from the preceding commands is shown in Figure 3.
Figure 3: Using Implicit Variables
Invoking class constants such as Math.PI also creates an implicit variable, as shown in Figure 4.
Figure 4: Creating an Implicit Variable by invoking a Class Constant
A String literal also creates an implicit variable (see Figure 5). The implicit variable created may be used in another code snippet; as an example, in a System.out.println statement. The implicit variable also may be invoked directly.
Figure 5: Creating an Implicit Variable from a String literal
Narrowing and widening primitive conversions are performed if needed in JShell. As an example, adding an int with a decimal number performs a widening conversion and creates an implicit variable with a decimal number, as shown in Figure 6.
Figure 6: Performing Widening Primitive Conversion
String concatenation also creates implicit variables. As an example, concatenate two String literals.
An implicit variable gets created. Any of the String class methods, such as length() and substring(int,int), may be invoked on the implicitly created variable, as shown in Figure 7.
Figure 7: Invoking Methods on an Implicit Variable
Variables of any type may be created in JShell. As an example, create variables of type char, float, and double in addition to the int type.
-> int a=1; | a ==> 1 -> int b=5; | b ==> 5 -> char c='a'; | c ==> 'a' -> float f=1.0f; | f ==> 1.0 -> double d=5.0d; | d ==> 5.0
Whereas the /list command lists all code snippets, the /vars command in JShell lists just the variables.
-> /vars | int a = 1 | int b = 5 | char c = 'a' | float f = 1.0 | double d = 5.0
Earlier, we had discussed an expression in which an implicit widening primitive conversion is made in JShell. Narrowing and widening primitive conversions are also made on assignment. As an example of a narrowing primitive conversion, assign the variable c of type char an int value.
-> c=5; | c ==> ' 05'
Certain modifiers are not permitted at the top level. Using static in a variable declaration at the top level generates a warning; the variable is created nevertheless, but is not static.
-> static int a =5; | Warning: | Modifier 'static' not permitted in top-level declarations, | ignored | static int a =5; | ^----^ | a ==> 5 -> a | a ==> 5 ->
Similarly, a final variable is not permitted at the top level. The following code snippet at the top level generates a message that finalis not permitted in top-level declarations.
-> final int a=1; | Warning: | Modifier 'final' not permitted in top-level declarations, | ignored | final int a=1; | ^---^ | a ==> 1
The variable a gets created, but is not final and may be assigned a new value.
-> a | a ==> 1 -> a=2; | a ==> 2 ->
String comparison is performed the same as in a Java class file. Two String literals with the same constant value are interned, stored as the same Object. As an example, create two String literals with the same value of “JShell”.
-> String str1="JShell"; | str1 ==> "JShell" -> String str2="JShell"; | str2 ==> "JShell"
The equals() method comparison on the two String variables str1 and str2 yields true.
-> str1.equals(str2) | $11 ==> true
The == operator comparison also returns true.
-> str1==str2 | $2 ==> true
Comparison of two String objects created with the new String(String) invocation is also the same as in a Java application compiled with javac and run with the java application. Create two Stringvariables with the same String literal value “JShell” using new String().
-> String str3=new String("JShell"); | str3 ==> "JShell" -> String str4=new String("JShell"); | str4 ==> "JShell"
Compare the two String variables with equals() and true is returned.
-> str3.equals(str4) | $15 ==> true
Comparing the two String variables with == returns false because the two String variables reference different String objects.
-> str3==str4 | $4 ==> false ->
JShell may be used to evaluate all types of statements, which include expression statements, declaration statements, and control flow statements. We have already discussed some expression statements and declaration statements. Assignment expressions and Object creation expressions are expression statements. The two other categories of expression statements are expression statements that make use of increment (++) and decrement (--) operators and method invocations.
As an example of using the postfix increment operator, create an int type variable with value 1 and invoke the postfix increment operator on the variable. Because the expression in a postfix increment evaluates to the original value, the implicit variable created after evaluating the postfix increment is the same as the original value, which is 1.
-> int a =1 | a ==> 1 -> a++ | $1 ==> 1
The variable a has the incremented value of 2.
-> a | a ==> 2
The output from the preceding statements is shown in Figure 8.
Figure 8: Postfix Increment
If the variable a is printed with System.out.println, the value output is 2, as shown in the Output console in Figure 9. As another example, use the prefix increment operator on variable a. The increment expression evaluates to 3 and is stored in a new implicit variable. The variable a also has the value, 3.
-> ++a | $3 ==> 3 -> a | a ==> 3
Perform a postfix decrement on a and the expression evaluates to the original value, which is 3, and is stored in a new implicit variable. The variable a gets decremented to 2.
-> a-- | $6 ==> 3 -> a | a ==> 2
Perform a prefix decrement and the expression evaluates to the decremented value of 1. The variable a is also decremented by 1.
-> --a | $9 ==> 1 -> a | a ==> 1 ->
The output from the previous statements is shown in Figure 9.
Figure 9: Using Increment and Decrement Operators
The code snippets added in a given JShell session get accumulated and may be used in the same session. A JShell session may be reset with Reset Java Shell, as shown in Figure 10.
Figure 10: Reset Java Shell
The JShell starts to load and initialize again (see Figure 11).
Figure 11: Restarting and re-initializing JShell
The JShell gets reset/reinitialized, as shown in Figure 12.
Figure 12: Reinitialized JShell
Listing the code snippets with /list does not list any code snippets, as shown in Figure 13.
Figure 13: No Code Snippets listed
In two articles, we introduced Java Shell (JShell), a new interactive tool in Java 9. JShell may be used for testing and debugging code snippets before using them in an application. In subsequent articles, we shall discuss some other features of using JShell.