O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Java 9 with JShell

Book Description

Explore Java 9 with JShell and UML

About This Book

  • A full account of Java 9’s new features
  • This tutorial emphasises fluency using JShell exercises
  • Get a thorough introduction to contract programming code reuse via Java generics
  • Learn how to use the new module system
  • How to use proper functional programming style inside Java 9

Who This Book Is For

This book can be understood by anyone who is a graduate of computer science or someone who has just begun working as a software engineer. Basically, an understanding of an object-oriented programming language like Python, C++ or indeed, an earlier Java version is sufficient. It would be helpful to have participated in the full product cycle of a software engineering project.

What You Will Learn

  • Engage with object-oriented programming in Java 9, starting with code snippets in JShell
  • Optimize your code, applying functional programming features
  • Discover the advantages of modularity
  • Become very proficient at using JShell itself
  • Learn the new approach to Java programming, which uses the REPL as a prototyping tool

In Detail

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.

Style and approach

You will learn by doing: : using JShell as their prototyping environment, you will take full advantage of the new features of Java 9, in particular the full module system and the functional features of Java 9.. There won’t be any theory, only small and medium-sized exampled enabling the reader to use the new Java features in professional software engineering projects.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Java 9 with JShell
    1. Table of Contents
    2. Java 9 with JShell
    3. Credits
    4. About the Author
    5. Acknowledgement
    6. About the Reviewer
    7. www.PacktPub.com
      1. eBooks, discount offers, and more
        1. Why subscribe?
    8. Customer Feedback
    9. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    10. 1. JShell – A Read-Evaluate-Print-Loop for Java 9
      1. Getting ready for our journey toward OOP with Java 9
      2. Installing the required software on Windows, macOS, or Linux
        1. Understanding the benefits of working with a REPL
        2. Checking default imports and using auto-complete features
      3. Running Java 9 code in JShell
        1. Evaluating expressions
      4. Working with variables, methods, and sources
      5. Editing the source code in our favorite external code editor
      6. Loading source code
      7. Test your knowledge
      8. Summary
    11. 2. Real-World Objects to UML Diagrams and Java 9 via JShell
      1. Identifying objects from applications requirements
      2. Capturing real-world objects
      3. Generating classes to create objects
      4. Recognizing variables and constants
      5. Identifying actions to create methods
      6. Organizing classes with UML diagrams
      7. Using feedback from domain experts
      8. Test your knowledge
      9. Summary
    12. 3. Classes and Instances
      1. Understanding classes and instances in Java 9
      2. Working with object initialization and its customization
      3. Introducing garbage collection
      4. Declaring classes
      5. Customizing constructors and initialization
      6. Understanding how garbage collection works
      7. Creating instances of classes and understanding their scope
      8. Exercises
        1. Exercise 1
        2. Exercise 2
      9. Test your knowledge
      10. Summary
    13. 4. Encapsulation of Data
      1. Understanding members composing a class
      2. Declaring immutable fields
      3. Working with setters and getters
        1. Exploring access modifiers in Java 9
      4. Combining setters, getters, and fields
      5. Transforming values with setters and getters
      6. Using static fields to provide class-level values
      7. Using static methods to provide overridable class-level values
      8. Test your knowledge
      9. Summary
    14. 5. Mutable and Immutable Classes
      1. Creating mutable classes in Java 9
      2. Working with mutable objects in JShell
      3. Building immutable classes in Java 9
      4. Working with immutable objects in JShell
      5. Understanding the differences between mutating and non-mutating objects
      6. Learning the advantages of non-mutating objects when writing concurrent code
      7. Working with instances of the immutable String class
      8. Creating the immutable version of an existing mutable class
      9. Test your knowledge
      10. Summary
    15. 6. Inheritance, Abstraction, Extension, and Specialization
      1. Creating class hierarchies to abstract and specialize behavior
      2. Understanding inheritance
      3. Creating an abstract base class
      4. Declaring classes that inherit from another class
      5. Overriding and overloading methods
      6. Test your knowledge
      7. Summary
    16. 7. Members Inheritance and Polymorphism
      1. Creating concrete classes that inherit from abstract superclasses
      2. Understanding polymorphism
      3. Controlling overridability of members in subclasses
      4. Controlling subclassing of classes
      5. Creating methods that work with instances of different subclasses
      6. Test your knowledge
      7. Summary
    17. 8. Contract Programming with Interfaces
      1. Understanding how interfaces work in combination with classes
      2. Declaring interfaces
      3. Declaring classes that implement interfaces
      4. Taking advantage of the multiple inheritance of interfaces
      5. Combining class inheritance and interfaces
      6. Test your knowledge
      7. Summary
    18. 9. Advanced Contract Programming with Interfaces
      1. Working with methods receiving interfaces as arguments
      2. Downcasting with interfaces and classes
      3. Treating instances of an interface type as a different subclass
      4. Taking advantage of default methods in interfaces in Java 9
      5. Test your knowledge
      6. Summary
    19. 10. Maximization of Code Reuse with Generics
      1. Understanding parametric polymorphism, Java 9 generics, and generic code
      2. Declaring an interface to be used as a type constraint
      3. Declaring a class that conforms to multiple interfaces
      4. Declaring subclasses that inherit the implementation of interfaces
      5. Creating exception classes
      6. Declaring a class that works with a constrained generic type
      7. Using a generic class for multiple compatible types
      8. Test your knowledge
      9. Summary
    20. 11. Advanced Generics
      1. Creating a new interface to be used as a constraint for a second type parameter
      2. Declaring two classes that implement an interface to work with two type parameters
      3. Declaring a class that works with two constrained generic types
      4. Creating instances of a generic class with two generic type parameters
      5. Test your knowledge
      6. Summary
    21. 12. Object-Oriented, Functional Programming, and Lambda Expressions
      1. Understanding functions and methods as first-class citizens
      2. Working with functional interfaces and lambda expressions
      3. Creating a functional version of array filtering
      4. Creating a data repository with generics and interfaces
      5. Filtering collections with complex conditions
      6. Using a map operation to transform values
      7. Combining a map operation with reduce
      8. Chaining many operations with map and reduce
      9. Working with different collectors
      10. Test your knowledge
      11. Summary
    22. 13. Modularity in Java 9
      1. Refactoring existing code to take advantage of object-oriented programming
      2. Organizing object-oriented code with the new modularity in Java 9
      3. Creating modular source code
      4. Compiling multiple modules with the Java 9 compiler
      5. Run modularized code with Java 9
      6. Test your knowledge
      7. Summary
    23. A. Exercise Answers
      1. Chapter 1, JShell – A Read-Evaluate-Print-Loop for Java 9
      2. Chapter 2, Real-World Objects to UML Diagrams and Java 9 via JShell
      3. Chapter 3, Classes and Instances
      4. Chapter 4, Encapsulation of Data
      5. Chapter 5, Mutable and Immutable Classes
      6. Chapter 6, Inheritance, Abstraction, Extension, and Specialization
      7. Chapter 7, Members Inheritance and Polymorphism
      8. Chapter 8, Contract Programming with Interfaces
      9. Chapter 9, Advanced Contract Programming with Interfaces
      10. Chapter 10, Maximization of Code Reuse with Generics
      11. Chapter 11, Advanced Generics
      12. Chapter 12, Object-Oriented, Functional Programming, and Lambda Expressions
      13. Chapter 13, Modularity in Java 9
    24. Index