Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
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 2. Real-World Objects to UML Diagrams and Java 9 via JShell FREE CHAPTER 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

Editing the source code in our favorite external code editor

We created a new version of the calculateRectanglePerimeter method. Now, we want to make similar changes to the calculateRectangleArea method. However, this time, we will take advantage of an editor to make it easier to make changes to the existing code.

Enter the following command in JShell to launch the default JShell Edit Pad editor to edit the source code for the calculateRectangleArea method:

/edit calculateRectangleArea

JShell will display a dialog box with JShell Edit Pad and the source code for the calculateRectangleArea method, as shown in the following screenshot:

Editing the source code in our favorite external code editor

JShell Edit Pad lacks most of the features we enjoy from code editors and we cannot even consider it a decent code editor. In fact, it just allows us to easily edit the source code without having to copy and paste from the previous listing. We will learn how to configure a better editor later.

Enter the following code in the JShell Edit Pad to overwrite the method named calculateRectangleArea with a new code that prints the received width and height values and then prints the calculated area with calls to the Sytem.out.printf method. The changes are highlighted here:

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

Click on Accept and then click on Exit. JShell will close the JShell Edit Pad and display the next messages indicating it has modified and overwritten the method named calculateRectangleArea with two arguments of the float type:

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

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

calculateRectangleArea(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, $24, the value assigned to the variable that indicates the result returned by the method, 3250.125, 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
Area: 3250.13
$24 ==> 3250.125
|  created scratch variable $24 : float

The good news is that JShell allows us to easily configure any external editor to edit the code snippets. We just need to grab the complete path to the editor we want to use and run a command in JShell to configure the editor we want to launch whenever we use the /edit command.

For example, in Windows, the default installation path for the popular Sublime Text 3 code editor is C:\Program Files\Sublime Text 3\sublime_text.exe. If we want to use this editor to edit code snippets in JShell, we must run the /set editor command followed by the path enclosed in double quotes. We have to make sure that we replace the backslash (\) with double backslashes (\\) in the path string. For the previously explained path, we must run the following command:

/set editor "C:\\Program Files\\Sublimet Text 3\\sublime_text.exe"

After we enter the previous command, JShell will display a message indicating to us that the editor was set to the specified path:

| Editor set to: C:\Program Files\Sublime Text 3\sublime_text.exe

After we change the editor, we can enter the following command in JShell to launch the new editor to make changes to the source code for the calculateRectangleArea method:

/edit calculateRectangleArea

JShell will launch Sublime Text 3 or any other editor that we might have specified and will load a temporary file with the source code for the calculateRectangleArea method, as shown in the following screenshot:

Editing the source code in our favorite external code editor

Tip

If we save the changes, JShell will automatically overwrite the method as we did when we used the default editor: JShell Edit Pad. After we make the necessary edits, we must close the editor to continue running Java code or JShell commands in JShell.

In any of the platforms, JShell will create a temporary file with the .edit extension. Thus, we can configure our favorite editor to use Java syntax highlighting whenever we open files with the .edit extension.

In macOS or Linux, paths are different than in Windows, and therefore, the necessary steps are different. For example, in macOS, in order to launch the popular Sublime Text 3 code editor when it is installed in the default path, we must run /Applications/Sublime Text.app/Contents/SharedSupport/bin/subl.

If we want to use this editor to edit code snippets in JShell, we must run the /set editor command followed by the complete path enclosed in double quotes. For the previously explained path, we must run the following command:

/set editor "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl"

After we enter the previous command, JShell will display a message indicating to us that the editor was set to the specified path:

|  Editor set to: /Applications/Sublime Text.app/Contents/SharedSupport/bin/subl

After we change the editor, we can enter the following command in JShell to launch the new editor to make changes to the source code for the calculateRectangleArea method:

/edit calculateRectangleArea

JShell will launch Sublime Text 3 on macOS or any other editor that we might have specified and will load a temporary file with the source code for the calculateRectangleArea method, as shown in the following screenshot:

Editing the source code in our favorite external code editor
lock icon The rest of the chapter is locked
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
Banner background image