Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Java 9 with JShell

You're reading from   Java 9 with JShell Introducing the full range of Java 9's new features via JShell

Arrow left icon
Product type Paperback
Published in Mar 2017
Publisher Packt
ISBN-13 9781787282841
Length 408 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Gaston C. Hillar Gaston C. Hillar
Author Profile Icon Gaston C. Hillar
Gaston C. Hillar
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. JShell – A Read-Evaluate-Print-Loop for Java 9 FREE CHAPTER 2. Real-World Objects to UML Diagrams and Java 9 via JShell 3. Classes and Instances 4. Encapsulation of Data 5. Mutable and Immutable Classes 6. Inheritance, Abstraction, Extension, and Specialization 7. Members Inheritance and Polymorphism 8. Contract Programming with Interfaces 9. Advanced Contract Programming with Interfaces 10. Maximization of Code Reuse with Generics 11. Advanced Generics 12. Object-Oriented, Functional Programming, and Lambda Expressions 13. Modularity in Java 9 A. Exercise Answers 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
You have been reading a chapter from
Java 9 with JShell
Published in: Mar 2017
Publisher: Packt
ISBN-13: 9781787282841
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime