Running Java 9 code in JShell
Now, we want to run Java 9 code in JShell and understand what happens after we enter each code snippet. Press Ctrl + D to exit the current JShell session. Run the following command in the Windows Command Prompt or in a macOS or Linux Terminal to launch JShell with a verbose feedback:
jshell -v
The verbose feedback will provide us additional details about what JShell does after it executes or evaluates each code snippet. Enter the following code in JShell to create a new method named calculateRectangleArea
. The method receives a width
and a height
for a rectangle and returns the result of the multiplication of both values of type float
:
float calculateRectangleArea(float width, float height) { return width * height; }
After we enter the previous lines, JShell will display the next message indicating it has created a method named calculateRectangleArea
with two arguments of float
type:
| created method calculateRectangleArea(float,float)
Tip
Notice that all the messages written by JShell start with a pipe symbol (|
).
Enter the following command in JShell to list the current active snippets of code that we have typed and executed so far in the current session:
/list
The following lines show the results. Notice that JShell prefaces the code snippet with the snippet id, that is, a unique number that identifies each code snippet. JShell will display the following lines as a result of the previous command. The code snippet that created the calculateRectangleArea
method has been assigned 1
as the snippet id.
1 : float calculateRectangleArea(float width, float height) { return width * height; }
Enter the following code in JShell to create a new float
variable named width
and initialize it with 50
:
float width = 50;
After we enter the previous line, JShell will display the next message indicating it has created a variable named width
of float
type and it assigned the value 50.0
to this variable:
width ==> 50.0 | created variable width : float
Enter the following code in JShell to create a new float
variable named height
and initialize it with 25
:
float height = 25;
After we enter the previous line, JShell will display the next message indicating it has created a variable named height
of the float
type and it assigned the value 25.0
to this variable:
height ==> 25.0 | created variable height : float
Enter float area = ca
and press the Tab key. JShell will complete to float area = calculateRectangleArea(
, that is, it will add lculateRectangleArea
and open parenthesis ((
). This way, we just have to enter the two arguments for the method because there was just one method that started with ca
. Enter width, height);
and press Enter. The following line shows the complete statement:
float area = calculateRectangleArea(width, height);
After we enter the previous line, JShell will display the next message indicating it has created a variable named area
of the float
type and it assigned the result of calling the calculateRectangleArea
method with the previously declared width
and height
variables as arguments. The method returns 1250.0
as a result and it is assigned to the area
variable.
area ==> 1250.0 | created variable area : float
Enter the following command in JShell to list the current active snippets of code that we have typed and executed so far in the current session:
/list
The following lines show the results. Notice that JShell prefaces the code snippet with the snippet id, that is, a unique number that identifies each code snippet. JShell will display the following lines as a result of the previous command:
1 : float calculateRectangleArea(float width, float height) { return width * height; } 2 : float width = 50; 3 : float height = 25; 4 : float area = calculateRectangleArea(width, height);
Enter the following code in JShell to display the values for the width
, height
, and area
variables with a call to System.out.printf
. The first %.2f
in the string we pass as a first argument to System.out.printf
makes the next argument after the string (width
) to be displayed as a floating point number with two decimal places. We repeat %.2f
twice to display the height
and area
variables as floating point numbers with two decimal places.
System.out.printf("Width: %.2f, Height: %.2f, Area: %.2f\n", width, height, area);
After we enter the previous line, JShell will format the output with System.out.printf
and will print the next message followed by the name of a scratch variable:
Width: 50.00, Height: 25.00, Area: 1250.00 $5 ==> java.io.PrintStream@68c4039c | created scratch variable $5 : PrintStream
Evaluating expressions
JShell allows us to evaluate any valid Java 9 expression, as we might do when we use an IDE and the typical expression evaluation dialog box. Enter the following expression in JShell:
width * height;
After we enter the previous line, JShell will evaluate the expression and it will assign the results to a scratch variable whose name starts with $
and continues with a number. JShell displays the scratch variable name, $6
, the value assigned to the variable that indicates the result of evaluating the expression, 1250.0
, and the type for the scratch variable, float
. The next lines show the message displayed in JShell after we enter the previous expression:
$6 ==> 1250.0 | created scratch variable $6 : float
Notice that the name for the scratch variable might be different. For example, instead of $6
, it might be $7
or $8
. We can run a code snippet that uses the previously created scratch variable as we do with any other variable created in the current JShell session. Enter the following code in JShell to display the value for the $6
variable as a floating point number with two decimal places. Make sure you replace $6
with the scratch variable name that JShell generated.
System.out.printf("The calculated area is %.2f", $6);
After we enter the previous line, JShell will format the output with System.out.printf
and will print the next message:
The calculated area is 1250.00
We can also use the previously created scratch variable in another expression. Enter the following code in JShell to add 10.5
(float
) to the value of the $6
variable. Make sure you replace $6
with the scratch variable name that JShell generated.
$6 + 10.5f;
After we enter the previous line, JShell will evaluate the expression and it will assign the results to a new scratch variable whose name starts with $
and continues with a number. JShell displays the scratch variable name, $8
, the value assigned to the variable that indicates the result of evaluating the expression, 1260.5
, and the type for the scratch variable, float
. The next lines show the message displayed in JShell after we enter the previous expression:
$8 ==> 1250.5 | created scratch variable $8 : float
Tip
As happened before, the name for the scratch variable might be different. For example, instead of $8
, it might be $9
or $10
.