Book Image

Java 9 with JShell

By : Gaston C. Hillar
Book Image

Java 9 with JShell

By: Gaston C. Hillar

Overview of this book

The release of Java 9 has brought many subtle and not-so-subtle changes to the way in which Java programmers approach their code. The most important ones are definitely the availability of a REPL, known as JShell, which will make experiments and prototyping much more straightforward than the old IDE-based project-led approach. Another, more subtle change can be seen in the module system, which will lead to more modularized, maintainable code. The techniques to take full advantage of object-oriented code, functional programming and the new modularity features in Java 9 form the main subjects of this book. Each chapter will add to the full picture of Java 9 programming starting out with classes and instances and ending with generics and modularity in Java.
Table of Contents (23 chapters)
Java 9 with JShell
Credits
About the Author
Acknowledgement
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
Index

Working with variables, methods, and sources


So far, we have been creating many variables, and JShell created a few scratch variables after we entered expressions and they were successfully evaluated. Enter the following command in JShell to list the type, name, and value of the current active variables that have been created so far in the current session:

/vars

The following lines show the results:

|    float width = 50.0
|    float height = 25.0
|    float area = 1250.0
|    PrintStream $5 = java.io.PrintStream@68c4039c
|    float $6 = 1250.0
|    float $8 = 1260.5

Enter the following code in JShell to assign 80.25 (float) to the previously created width variable:

width = 80.25f;

After we enter the previous line, JShell will display the next message indicating it has assigned 80.25 (float) to the existing variable named width of the float type:

width ==> 80.25
|  assigned to width : float

Enter the following code in JShell to assign 40.5 (float) to the previously created height variable:

height = 40.5f;

After we enter the previous line, JShell will display the next message indicating it has assigned 40.5 (float) to the existing variable named height of the float type:

height ==> 40.5
|  assigned to height : float

Enter the following command in JShell again to list the type, name, and value of the current active variables:

/vars

The following lines show the results that reflect the new values we have assigned to the width and height variables:

|    float width = 80.25
|    float height = 40.5
|    float area = 1250.0
|    PrintStream $5 = java.io.PrintStream@68c4039c
|    float $6 = 1250.0
|    float $8 = 1260.5

Enter the following code in JShell to create a new method named calculateRectanglePerimeter. The method receives a width variable and a height variable for a rectangle and returns the result of the multiplication by 2 of the sum of both values of the float type.

float calculateRectanglePerimeter(float width, float height) {
    return 2 * (width + height);
}

After we enter the previous lines, JShell will display the next message indicating it has created a method named calculateRectanglePerimeter with two arguments of the float type:

|  created method calculateRectanglePerimeter(float,float)

Enter the following command in JShell to list the name, parameter types, and return the type of the current active methods that have been created so far in the current session:

/methods

The following lines show the results.

|    calculateRectangleArea (float,float)float
|    calculateRectanglePerimeter (float,float)float

Enter the following code in JShell to print the results of calling the recently created calculateRectanglePerimeter with width and height as the arguments:

calculateRectanglePerimeter(width, height);

After we enter the previous line, JShell will call the method 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, $16, the value assigned to the variable that indicates the result returned by the method, 241.5, and the type for the scratch variable, float. The next lines show the message displayed in JShell after we enter the previous expression that called a method:

$16 ==> 241.5
|  created scratch variable $16 : float

Now, we want to make changes to the recently created calculateRectanglePerimeter method. We want to add a line to print the calculated perimeter. Enter the following command in JShell to list the source code for the method:

/list calculateRectanglePerimeter

The following lines show the results:

  15 : float calculateRectanglePerimeter(float width, float height) {
           return 2 * (width + height);
       }

Enter the following code in JShell to overwrite the method named calculateRectanglePerimeter with a new code that prints the received width and height values and then prints the calculated perimeter with calls to the System.out.printf method that works in the same way as the built-in printf method. We can copy and paste the pieces from the previously listed source code. The changes are highlighted here:

float calculateRectanglePerimeter(float width, float height) {
    float perimeter = 2 * (width + height);
    System.out.printf("Width: %.2f\n", width);
    System.out.printf("Height: %.2f\n", height);
    System.out.printf("Perimeter: %.2f\n", perimeter);
    return perimeter;
}

After we enter the previous lines, JShell will display the next messages indicating it has modified and overwritten the method named calculateRectanglePerimeter with two arguments of the float type:

|  modified method calculateRectanglePerimeter(float,float)
|    update overwrote method calculateRectanglePerimeter(float,float)

Enter the following code in JShell to print out the results of calling the recently modified calculateRectanglePerimeter with width and height as the arguments:

calculateRectanglePerimeter(width, height);

After we enter the previous line, JShell will call the method and it will assign the results to a scratch variable whose name starts with $ and continues with a number. The first lines display the output generated by the three calls to System.out.printf that we added to the method. Finally, JShell displays the scratch variable name, $19, the value assigned to the variable that indicates the result returned by the method, 241.5, and the type for the scratch variable, float.

The next lines show the messages displayed in JShell after we enter the previous expression that called the new version of the method:

Width: 80.25
Height: 40.50
Perimeter: 241.50
$19 ==> 241.5
|  created scratch variable $19 : float