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

Core Java SE 9 for the Impatient, 2nd Edition

Book Description

An Accessible Guide to the Java Language and Libraries

Modern Java introduces major enhancements that impact the core Java technologies and APIs at the heart of the Java platform. Many old Java idioms are no longer needed and new features such as modularization make you far more effective. However, navigating these changes can be challenging.

Core Java® SE 9 for the Impatient, Second Edition, is a complete yet concise guide that includes all the latest changes up to Java SE 9. Written by Cay S. Horstmann–author of the classic two-volume Core Java–this indispensable tutorial offers a faster, easier pathway for learning modern Java. Given Java SE 9’s size and the scope of its enhancements, there’s plenty to cover, but it’s presented in small chunks organized for quick access and easy understanding.

Horstmann’s practical insights and sample code help you quickly take advantage of all that’s new, from Java SE 9’s long-awaited “Project Jigsaw” module system to the improvements first introduced in Java SE 8, including lambda expressions and streams.

  • Use modules to simplify the development of well-performing complex systems
  • Migrate applications to work with the modularized Java API and third-party modules
  • Test code as you create it with the new JShell Read-Eval-Print Loop (REPL)
  • Use lambda expressions to express actions more concisely
  • Streamline and optimize data management with today’s Streams API
  • Leverage modern concurrent programming based on cooperating tasks
  • Take advantage of a multitude of API improvements for working with collections, input/output, regular expressions, and processes

Whether you’re just getting started with modern Java or you’re an experienced developer, this guide will help you write tomorrow’s most robust, efficient, and secure Java code.

Register your product at informit.com/register for convenient access to downloads, updates, and/or corrections as they become available.

Table of Contents

  1. Title Page
  2. Copyright Page
  3. Dedication Page
  4. Contents
  5. Preface
  6. Acknowledgments
  7. About the Author
  8. Chapter 1. Fundamental Programming Structures
    1. 1.1 Our First Program
      1. 1.1.1 Dissecting the “Hello, World” Program
      2. 1.1.2 Compiling and Running a Java Program
      3. 1.1.3 Method Calls
      4. 1.1.4 JShell
    2. 1.2 Primitive Types
      1. 1.2.1 Signed Integer Types
      2. 1.2.2 Floating-Point Types
      3. 1.2.3 The char Type
      4. 1.2.4 The boolean Type
    3. 1.3 Variables
      1. 1.3.1 Variable Declarations
      2. 1.3.2 Names
      3. 1.3.3 Initialization
      4. 1.3.4 Constants
    4. 1.4 Arithmetic Operations
      1. 1.4.1 Assignment
      2. 1.4.2 Basic Arithmetic
      3. 1.4.3 Mathematical Methods
      4. 1.4.4 Number Type Conversions
      5. 1.4.5 Relational and Logical Operators
      6. 1.4.6 Big Numbers
    5. 1.5 Strings
      1. 1.5.1 Concatenation
      2. 1.5.2 Substrings
      3. 1.5.3 String Comparison
      4. 1.5.4 Converting Between Numbers and Strings
      5. 1.5.5 The String API
      6. 1.5.6 Code Points and Code Units
    6. 1.6 Input and Output
      1. 1.6.1 Reading Input
      2. 1.6.2 Formatted Output
    7. 1.7 Control Flow
      1. 1.7.1 Branches
      2. 1.7.2 Loops
      3. 1.7.3 Breaking and Continuing
      4. 1.7.4 Local Variable Scope
    8. 1.8 Arrays and Array Lists
      1. 1.8.1 Working with Arrays
      2. 1.8.2 Array Construction
      3. 1.8.3 Array Lists
      4. 1.8.4 Wrapper Classes for Primitive Types
      5. 1.8.5 The Enhanced for Loop
      6. 1.8.6 Copying Arrays and Array Lists
      7. 1.8.7 Array Algorithms
      8. 1.8.8 Command-Line Arguments
      9. 1.8.9 Multidimensional Arrays
    9. 1.9 Functional Decomposition
      1. 1.9.1 Declaring and Calling Static Methods
      2. 1.9.2 Array Parameters and Return Values
      3. 1.9.3 Variable Arguments
    10. Exercises
  9. Chapter 2. Object-Oriented Programming
    1. 2.1 Working with Objects
      1. 2.1.1 Accessor and Mutator Methods
      2. 2.1.2 Object References
    2. 2.2 Implementing Classes
      1. 2.2.1 Instance Variables
      2. 2.2.2 Method Headers
      3. 2.2.3 Method Bodies
      4. 2.2.4 Instance Method Invocations
      5. 2.2.5 The this Reference
      6. 2.2.6 Call by Value
    3. 2.3 Object Construction
      1. 2.3.1 Implementing Constructors
      2. 2.3.2 Overloading
      3. 2.3.3 Calling One Constructor from Another
      4. 2.3.4 Default Initialization
      5. 2.3.5 Instance Variable Initialization
      6. 2.3.6 Final Instance Variables
      7. 2.3.7 The Constructor with No Arguments
    4. 2.4 Static Variables and Methods
      1. 2.4.1 Static Variables
      2. 2.4.2 Static Constants
      3. 2.4.3 Static Initialization Blocks
      4. 2.4.4 Static Methods
      5. 2.4.5 Factory Methods
    5. 2.5 Packages
      1. 2.5.1 Package Declarations
      2. 2.5.2 The jar Command
      3. 2.5.3 The Class Path
      4. 2.5.4 Package Access
      5. 2.5.5 Importing Classes
      6. 2.5.6 Static Imports
    6. 2.6 Nested Classes
      1. 2.6.1 Static Nested Classes
      2. 2.6.2 Inner Classes
      3. 2.6.3 Special Syntax Rules for Inner Classes
    7. 2.7 Documentation Comments
      1. 2.7.1 Comment Insertion
      2. 2.7.2 Class Comments
      3. 2.7.3 Method Comments
      4. 2.7.4 Variable Comments
      5. 2.7.5 General Comments
      6. 2.7.6 Links
      7. 2.7.7 Package, Module, and Overview Comments
      8. 2.7.8 Comment Extraction
    8. Exercises
  10. Chapter 3. Interfaces and Lambda Expressions
    1. 3.1 Interfaces
      1. 3.1.1 Declaring an Interface
      2. 3.1.2 Implementing an Interface
      3. 3.1.3 Converting to an Interface Type
      4. 3.1.4 Casts and the instanceof Operator
      5. 3.1.5 Extending Interfaces
      6. 3.1.6 Implementing Multiple Interfaces
      7. 3.1.7 Constants
    2. 3.2 Static, Default, and Private Methods
      1. 3.2.1 Static Methods
      2. 3.2.2 Default Methods
      3. 3.2.3 Resolving Default Method Conflicts
      4. 3.2.4 Private Methods
    3. 3.3 Examples of Interfaces
      1. 3.3.1 The Comparable Interface
      2. 3.3.2 The Comparator Interface
      3. 3.3.3 The Runnable Interface
      4. 3.3.4 User Interface Callbacks
    4. 3.4 Lambda Expressions
      1. 3.4.1 The Syntax of Lambda Expressions
      2. 3.4.2 Functional Interfaces
    5. 3.5 Method and Constructor References
      1. 3.5.1 Method References
      2. 3.5.2 Constructor References
    6. 3.6 Processing Lambda Expressions
      1. 3.6.1 Implementing Deferred Execution
      2. 3.6.2 Choosing a Functional Interface
      3. 3.6.3 Implementing Your Own Functional Interfaces
    7. 3.7 Lambda Expressions and Variable Scope
      1. 3.7.1 Scope of a Lambda Expression
      2. 3.7.2 Accessing Variables from the Enclosing Scope
    8. 3.8 Higher-Order Functions
      1. 3.8.1 Methods that Return Functions
      2. 3.8.2 Methods That Modify Functions
      3. 3.8.3 Comparator Methods
    9. 3.9 Local and Anonymous Classes
      1. 3.9.1 Local Classes
      2. 3.9.2 Anonymous Classes
    10. Exercises
  11. Chapter 4. Inheritance and Reflection
    1. 4.1 Extending a Class
      1. 4.1.1 Super- and Subclasses
      2. 4.1.2 Defining and Inheriting Subclass Methods
      3. 4.1.3 Method Overriding
      4. 4.1.4 Subclass Construction
      5. 4.1.5 Superclass Assignments
      6. 4.1.6 Casts
      7. 4.1.7 Final Methods and Classes
      8. 4.1.8 Abstract Methods and Classes
      9. 4.1.9 Protected Access
      10. 4.1.10 Anonymous Subclasses
      11. 4.1.11 Inheritance and Default Methods
      12. 4.1.12 Method Expressions with super
    2. 4.2 Object: The Cosmic Superclass
      1. 4.2.1 The toString Method
      2. 4.2.2 The equals Method
      3. 4.2.3 The hashCode Method
      4. 4.2.4 Cloning Objects
    3. 4.3 Enumerations
      1. 4.3.1 Methods of Enumerations
      2. 4.3.2 Constructors, Methods, and Fields
      3. 4.3.3 Bodies of Instances
      4. 4.3.4 Static Members
      5. 4.3.5 Switching on an Enumeration
    4. 4.4 Runtime Type Information and Resources
      1. 4.4.1 The Class Class
      2. 4.4.2 Loading Resources
      3. 4.4.3 Class Loaders
      4. 4.4.4 The Context Class Loader
      5. 4.4.5 Service Loaders
    5. 4.5 Reflection
      1. 4.5.1 Enumerating Class Members
      2. 4.5.2 Inspecting Objects
      3. 4.5.3 Invoking Methods
      4. 4.5.4 Constructing Objects
      5. 4.5.5 JavaBeans
      6. 4.5.6 Working with Arrays
      7. 4.5.7 Proxies
    6. Exercises
  12. Chapter 5. Exceptions, Assertions, and Logging
    1. 5.1 Exception Handling
      1. 5.1.1 Throwing Exceptions
      2. 5.1.2 The Exception Hierarchy
      3. 5.1.3 Declaring Checked Exceptions
      4. 5.1.4 Catching Exceptions
      5. 5.1.5 The Try-with-Resources Statement
      6. 5.1.6 The finally Clause
      7. 5.1.7 Rethrowing and Chaining Exceptions
      8. 5.1.8 Uncaught Exceptions and the Stack Trace
      9. 5.1.9 The Objects.requireNonNull Method
    2. 5.2 Assertions
      1. 5.2.1 Using Assertions
      2. 5.2.2 Enabling and Disabling Assertions
    3. 5.3 Logging
      1. 5.3.1 Using Loggers
      2. 5.3.2 Loggers
      3. 5.3.3 Logging Levels
      4. 5.3.4 Other Logging Methods
      5. 5.3.5 Logging Configuration
      6. 5.3.6 Log Handlers
      7. 5.3.7 Filters and Formatters
    4. Exercises
  13. Chapter 6. Generic Programming
    1. 6.1 Generic Classes
    2. 6.2 Generic Methods
    3. 6.3 Type Bounds
    4. 6.4 Type Variance and Wildcards
      1. 6.4.1 Subtype Wildcards
      2. 6.4.2 Supertype Wildcards
      3. 6.4.3 Wildcards with Type Variables
      4. 6.4.4 Unbounded Wildcards
      5. 6.4.5 Wildcard Capture
    5. 6.5 Generics in the Java Virtual Machine
      1. 6.5.1 Type Erasure
      2. 6.5.2 Cast Insertion
      3. 6.5.3 Bridge Methods
    6. 6.6 Restrictions on Generics
      1. 6.6.1 No Primitive Type Arguments
      2. 6.6.2 At Runtime, All Types Are Raw
      3. 6.6.3 You Cannot Instantiate Type Variables
      4. 6.6.4 You Cannot Construct Arrays of Parameterized Types
      5. 6.6.5 Class Type Variables Are Not Valid in Static Contexts
      6. 6.6.6 Methods May Not Clash after Erasure
      7. 6.6.7 Exceptions and Generics
    7. 6.7 Reflection and Generics
      1. 6.7.1 The Class<T> Class
      2. 6.7.2 Generic Type Information in the Virtual Machine
    8. Exercises
  14. Chapter 7. Collections
    1. 7.1 An Overview of the Collections Framework
    2. 7.2 Iterators
    3. 7.3 Sets
    4. 7.4 Maps
    5. 7.5 Other Collections
      1. 7.5.1 Properties
      2. 7.5.2 Bit Sets
      3. 7.5.3 Enumeration Sets and Maps
      4. 7.5.4 Stacks, Queues, Deques, and Priority Queues
      5. 7.5.5 Weak Hash Maps
    6. 7.6 Views
      1. 7.6.1 Small Collections
      2. 7.6.2 Ranges
      3. 7.6.3 Unmodifiable Views
    7. Exercises
  15. Chapter 8. Streams
    1. 8.1 From Iterating to Stream Operations
    2. 8.2 Stream Creation
    3. 8.3 The filter, map, and flatMap Methods
    4. 8.4 Extracting Substreams and Combining Streams
    5. 8.5 Other Stream Transformations
    6. 8.6 Simple Reductions
    7. 8.7 The Optional Type
      1. 8.7.1 How to Work with Optional Values
      2. 8.7.2 How Not to Work with Optional Values
      3. 8.7.3 Creating Optional Values
      4. 8.7.4 Composing Optional Value Functions with flatMap
      5. 8.7.5 Turning an Optional Into a Stream
    8. 8.8 Collecting Results
    9. 8.9 Collecting into Maps
    10. 8.10 Grouping and Partitioning
    11. 8.11 Downstream Collectors
    12. 8.12 Reduction Operations
    13. 8.13 Primitive Type Streams
    14. 8.14 Parallel Streams
    15. Exercises
  16. Chapter 9. Processing Input and Output
    1. 9.1 Input/Output Streams, Readers, and Writers
      1. 9.1.1 Obtaining Streams
      2. 9.1.2 Reading Bytes
      3. 9.1.3 Writing Bytes
      4. 9.1.4 Character Encodings
      5. 9.1.5 Text Input
      6. 9.1.6 Text Output
      7. 9.1.7 Reading and Writing Binary Data
      8. 9.1.8 Random-Access Files
      9. 9.1.9 Memory-Mapped Files
      10. 9.1.10 File Locking
    2. 9.2 Paths, Files, and Directories
      1. 9.2.1 Paths
      2. 9.2.2 Creating Files and Directories
      3. 9.2.3 Copying, Moving, and Deleting Files
      4. 9.2.4 Visiting Directory Entries
      5. 9.2.5 ZIP File Systems
    3. 9.3 HTTP Connections
      1. 9.3.1 The URLConnection and HttpURLConnection Classes
      2. 9.3.2 The HTTP Client API
    4. 9.4 Regular Expressions
      1. 9.4.1 The Regular Expression Syntax
      2. 9.4.2 Finding One Match
      3. 9.4.3 Finding All Matches
      4. 9.4.4 Groups
      5. 9.4.5 Splitting along Delimiters
      6. 9.4.6 Replacing Matches
      7. 9.4.7 Flags
    5. 9.5 Serialization
      1. 9.5.1 The Serializable Interface
      2. 9.5.2 Transient Instance Variables
      3. 9.5.3 The readObject and writeObject Methods
      4. 9.5.4 The readResolve and writeReplace Methods
      5. 9.5.5 Versioning
    6. Exercises
  17. Chapter 10. Concurrent Programming
    1. 10.1 Concurrent Tasks
      1. 10.1.1 Running Tasks
      2. 10.1.2 Futures
    2. 10.2 Asynchronous Computations
      1. 10.2.1 Completable Futures
      2. 10.2.2 Composing Completable Futures
      3. 10.2.3 Long-Running Tasks in User-Interface Callbacks
    3. 10.3 Thread Safety
      1. 10.3.1 Visibility
      2. 10.3.2 Race Conditions
      3. 10.3.3 Strategies for Safe Concurrency
      4. 10.3.4 Immutable Classes
    4. 10.4 Parallel Algorithms
      1. 10.4.1 Parallel Streams
      2. 10.4.2 Parallel Array Operations
    5. 10.5 Threadsafe Data Structures
      1. 10.5.1 Concurrent Hash Maps
      2. 10.5.2 Blocking Queues
      3. 10.5.3 Other Threadsafe Data Structures
    6. 10.6 Atomic Counters and Accumulators
    7. 10.7 Locks and Conditions
      1. 10.7.1 Locks
      2. 10.7.2 The synchronized Keyword
      3. 10.7.3 Waiting on Conditions
    8. 10.8 Threads
      1. 10.8.1 Starting a Thread
      2. 10.8.2 Thread Interruption
      3. 10.8.3 Thread-Local Variables
      4. 10.8.4 Miscellaneous Thread Properties
    9. 10.9 Processes
      1. 10.9.1 Building a Process
      2. 10.9.2 Running a Process
      3. 10.9.3 Process Handles
    10. Exercises
  18. Chapter 11. Annotations
    1. 11.1 Using Annotations
      1. 11.1.1 Annotation Elements
      2. 11.1.2 Multiple and Repeated Annotations
      3. 11.1.3 Annotating Declarations
      4. 11.1.4 Annotating Type Uses
      5. 11.1.5 Making Receivers Explicit
    2. 11.2 Defining Annotations
    3. 11.3 Standard Annotations
      1. 11.3.1 Annotations for Compilation
      2. 11.3.2 Annotations for Managing Resources
      3. 11.3.3 Meta-Annotations
    4. 11.4 Processing Annotations at Runtime
    5. 11.5 Source-Level Annotation Processing
      1. 11.5.1 Annotation Processors
      2. 11.5.2 The Language Model API
      3. 11.5.3 Using Annotations to Generate Source Code
    6. Exercises
  19. Chapter 12. The Date and Time API
    1. 12.1 The Time Line
    2. 12.2 Local Dates
    3. 12.3 Date Adjusters
    4. 12.4 Local Time
    5. 12.5 Zoned Time
    6. 12.6 Formatting and Parsing
    7. 12.7 Interoperating with Legacy Code
    8. Exercises
  20. Chapter 13. Internationalization
    1. 13.1 Locales
      1. 13.1.1 Specifying a Locale
      2. 13.1.2 The Default Locale
      3. 13.1.3 Display Names
    2. 13.2 Number Formats
    3. 13.3 Currencies
    4. 13.4 Date and Time Formatting
    5. 13.5 Collation and Normalization
    6. 13.6 Message Formatting
    7. 13.7 Resource Bundles
      1. 13.7.1 Organizing Resource Bundles
      2. 13.7.2 Bundle Classes
    8. 13.8 Character Encodings
    9. 13.9 Preferences
    10. Exercises
  21. Chapter 14. Compiling and Scripting
    1. 14.1 The Compiler API
      1. 14.1.1 Invoking the Compiler
      2. 14.1.2 Launching a Compilation Task
      3. 14.1.3 Reading Source Files from Memory
      4. 14.1.4 Writing Byte Codes to Memory
      5. 14.1.5 Capturing Diagnostics
    2. 14.2 The Scripting API
      1. 14.2.1 Getting a Scripting Engine
      2. 14.2.2 Bindings
      3. 14.2.3 Redirecting Input and Output
      4. 14.2.4 Calling Scripting Functions and Methods
      5. 14.2.5 Compiling a Script
    3. 14.3 The Nashorn Scripting Engine
      1. 14.3.1 Running Nashorn from the Command Line
      2. 14.3.2 Invoking Getters, Setters, and Overloaded Methods
      3. 14.3.3 Constructing Java Objects
      4. 14.3.4 Strings in JavaScript and Java
      5. 14.3.5 Numbers
      6. 14.3.6 Working with Arrays
      7. 14.3.7 Lists and Maps
      8. 14.3.8 Lambdas
      9. 14.3.9 Extending Java Classes and Implementing Java Interfaces
      10. 14.3.10 Exceptions
    4. 14.4 Shell Scripting with Nashorn
      1. 14.4.1 Executing Shell Commands
      2. 14.4.2 String Interpolation
      3. 14.4.3 Script Inputs
    5. Exercises
  22. Chapter 15. The Java Platform Module System
    1. 15.1 The Module Concept
    2. 15.2 Naming Modules
    3. 15.3 The Modular “Hello, World!” Program
    4. 15.4 Requiring Modules
    5. 15.5 Exporting Packages
    6. 15.6 Modules and Reflective Access
    7. 15.7 Modular JARs
    8. 15.8 Automatic Modules and the Unnamed Module
    9. 15.9 Command-Line Flags for Migration
    10. 15.10 Transitive and Static Requirements
    11. 15.11 Qualified Exporting and Opening
    12. 15.12 Service Loading
    13. 15.13 Tools for Working with Modules
    14. Exercises
  23. Index