Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
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

Loading source code

Of course, we don't have to enter the source code for each example. Auto-completion features are useful, but we will take advantage of a command that allows us to load source code from a file in JShell.

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 again with a verbose feedback:

jshell -v

The following lines show code that declares the latest versions of the calculateRectanglePerimeter and calculateRectangleArea methods. Then, the code declares and initializes two variables of the float type: width and height. Finally, the last two lines call the previously defined methods with width and height as their arguments. The code file for the sample is included in the java_9_oop_chapter_01_01 folder, in the example01_01.java file.

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;
}

float calculateRectangleArea(float width, float height) {
    float area = width * height;
    System.out.printf("Width: %.2f\n", width);
    System.out.printf("Height: %.2f\n", height);
    System.out.printf("Area: %.2f\n", area);
    return area;
}

float width = 120.25f;
float height = 35.50f;
calculateRectangleArea(width, height);
calculateRectanglePerimeter(width, height);

Once you have downloaded the source code for the book in a folder, you can use the /open command in JShell to load and execute one of the files from the source code. Before each code snippet, we always mention where the source code is located.

If the root folder for the source code in Windows is C:\Users\Gaston\Java9, you can run the following command to load and execute the previously shown source code in JShell:

/open C:\Users\Gaston\Java9\java_9_oop_chapter_01_01\example01_01.java

If the root folder for the source code in macOS or Linux is ~/Documents/Java9, you can run the following command to load and execute the previously shown source code in JShell:

/open ~/Documents/Java9/java_9_oop_chapter_01_01/example01_01.java

After we enter the previous command followed by the path based on our configuration and our operating system, JShell will load and execute the previously shown source code and will display the output generated after running the loaded code snippet. The following lines show the output:

Width: 120.25
Height: 35.50
Area: 4268.88
Width: 120.25
Height: 35.50
Perimeter: 311.50

Now, enter the following command in JShell to list the current, active snippets of code, loaded from the source file, that have been executed in the current session so far:

/list

The following lines show the results. Notice that JShell prefaces the different method definitions and expressions with different snippet ids because the loaded source code behaves in the same way as if we were entering one snippet after the other:

   1 : 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;
       }
   2 : float calculateRectangleArea(float width, float height) {
           float area = width * height;
           System.out.printf("Width: %.2f\n", width);
           System.out.printf("Height: %.2f\n", height);
           System.out.printf("Area: %.2f\n", area);
           return area;
       }
   3 : float width = 120.25f;
   4 : float height = 35.50f;
 
  5 : calculateRectangleArea(width, height);
   6 : calculateRectanglePerimeter(width, height);

Tip

Make sure you use the previously explained /open command followed by the path and the file name for the code file that you want to load and execute in JShell whenever you find source code in the book. This way, you won't have to enter each code snippet and you will be able to check the results of executing the code in JShell.

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