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 mutable objects in JShell


The following lines create a new Vector3d instance named vector1 with 10.0, 20.0, and 30.0 for the initial values of x, y, and z. The second lines create a new Vector3d instance named vector2 with 1.0, 2.0, and 3.0 for the initial values of x, y, and z. Then, the code calls the System.out.println method with vector1 and then with vector2 as an argument. Both calls to the println method will execute the toString method for each Vector3d instance to display the String representation of the mutable 3D vector. Then, the code calls the add method for vector1 with vector2 as an argument. The last line calls the println method again with vector1 as an argument to print the new values of x, y and z after the object mutated with the call to the add method. The code file for the sample is included in the java_9_oop_chapter_05_01 folder, in the example05_01.java file.

Vector3d vector1 = new Vector3d(10.0, 20.0, 30.0);
Vector3d vector2 = new Vector3d(1.0, 2.0, 3...