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

Exercises


Now that you understand an object's life cycle, it is time to spend some time in JShell creating new classes and instances.

Exercise 1

  1. Create a new Student class with a constructor that requires two String arguments: firstName and lastName. Use the arguments to initialize fields with the same names as the arguments. Display a message with the values for firstName and lastName when an instance of the class is created.

  2. Create an instance of the Student class and assign it to a variable. Check the messages printed in JShell.

  3. Create an instance of the Student class and assign it to a variable. Check the messages printed in JShell.

Exercise 2

  1. Create a function that receives two String arguments: firstName and lastName. Create an instance of the previously defined Student class with the received arguments as parameters for the creation of the instance. Use the instance properties to print a message with the first name followed by a space and the last name. You will be able to create a method...