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


The following lines create a new ImmutableVector3d instance named vector10 with 100.0, 200.0, and 300.0 for the initial values of x, y, and z. The second lines create a new ImmutableVector3d instance named vector20 with 11.0, 12.0, and 13.0 for the initial values of x, y, and z. Then, the code calls the System.out.println method with vector10 and then with vector20 as an argument. Both calls to the println method will execute the toString method for each ImmutableVector3d instance to display the String representation of the immutable 3D vector. Then, the code calls the add method for vector10 with vector20 as an argument and saves the returned ImmutableVector3d instance in vector30.

The last line calls the println method with vector30 as an argument to print the values of x, y, and z for this instance that has the results of the addition operation between vector10 and vector20. Enter the lines after the code that declares the ImmutableVector3d class...