0%

The #1 Java Guide for Serious Programmers: Fully Updated through Java 17

Core Java, Volume I: Fundamentals, Twelfth Edition, is the definitive guide to writing robust, maintainable code. Whatever version of Java you are using--up to and including Java 17--this book will help you achieve a deep and practical understanding of the language and APIs. With hundreds of realistic examples, Cay S. Horstmann reveals the most powerful and effective ways to get the job done.

This book is written for readers with prior programming experience who are looking for in-depth coverage of the Java language and platform. You'll learn about all language features in detail, including the recent improvements in Java 17. The applied chapters and code examples cover the most up-to-date capabilities of the vast Java library. For 25 years, Core Java has prepared serious programmers for serious Java programming.

This first of two volumes offers in-depth coverage of fundamental Java programming, including object-oriented programming, generics, collections, lambda expressions, concurrency, and functional programming. Classic material for Swing UI programming is included for those who need it. This edition's new content covers text blocks, switch enhancements, records, pattern matching for instanceof, sealed classes, and more.

  • Master foundational techniques, idioms, and best practices for writing superior Java code

  • Leverage the power of interfaces, lambda expressions, and inner classes

  • Harden programs through effective exception handling and debugging

  • Write safer, more reusable code with generic programming

  • Improve performance and efficiency with Java's standard collections

  • Explore simple programs with JShell and assemble complex programs with archives and modules

  • Build cross-platform GUIs with the Swing toolkit

  • Fully utilize multicore processors with Java's powerful concurrency model

See Core Java, Volume II: Advanced Features, Twelfth Edition (coming in 2022), for expert coverage of Java 17 enterprise features, the module system, annotations, networking, security, and advanced UI programming.

Register your book for convenient access to downloads, updates, and/or corrections as they become available. See inside book for details.

Table of Contents

  1. Cover Page
  2. Title Page
  3. Contents
  4. Table of Contents
  5. Preface
  6. Acknowledgments
  7. Chapter 1: An Introduction to Java
    1. 1.1 Java as a Programming Platform
    2. 1.2 The Java “White Paper” Buzzwords
    3. 1.2.1 Simple
    4. 1.2.2 Object-Oriented
    5. 1.2.3 Distributed
    6. 1.2.4 Robust
    7. 1.2.5 Secure
    8. 1.2.6 Architecture-Neutral
    9. 1.2.7 Portable
    10. 1.2.8 Interpreted
    11. 1.2.9 High-Performance
    12. 1.2.10 Multithreaded
    13. 1.2.11 Dynamic
    14. 1.3 Java Applets and the Internet
    15. 1.4 A Short History of Java
    16. 1.5 Common Misconceptions about Java
  8. Chapter 2: The Java Programming Environment
    1. 2.1 Installing the Java Development Kit
    2. 2.1.1 Downloading the JDK
    3. 2.1.2 Setting up the JDK
    4. 2.1.3 Installing Source Files and Documentation
    5. 2.2 Using the Command-Line Tools
    6. 2.3 Using an Integrated Development Environment
    7. 2.4 JShell
  9. Chapter 3: Fundamental Programming Structures in Java
    1. 3.1 A Simple Java Program
    2. 3.2 Comments
    3. 3.3 Data Types
    4. 3.3.1 Integer Types
    5. 3.3.2 Floating-Point Types
    6. 3.3.3 The char Type
    7. 3.3.4 Unicode and the char Type
    8. 3.3.5 The boolean Type
    9. 3.4 Variables and Constants
    10. 3.4.1 Declaring Variables
    11. 3.4.2 Initializing Variables
    12. 3.4.3 Constants
    13. 3.4.4 Enumerated Types
    14. 3.5 Operators
    15. 3.5.1 Arithmetic Operators
    16. 3.5.2 Mathematical Functions and Constants
    17. 3.5.3 Conversions between Numeric Types
    18. 3.5.4 Casts
    19. 3.5.5 Assignment
    20. 3.5.6 Increment and Decrement Operators
    21. 3.5.7 Relational and boolean Operators
    22. 3.5.8 The Conditional Operator
    23. 3.5.9 Switch Expressions
    24. 3.5.10 Bitwise Operators
    25. 3.5.11 Parentheses and Operator Hierarchy
    26. 3.6 Strings
    27. 3.6.1 Substrings
    28. 3.6.2 Concatenation
    29. 3.6.3 Strings Are Immutable
    30. 3.6.4 Testing Strings for Equality
    31. 3.6.5 Empty and Null Strings
    32. 3.6.6 Code Points and Code Units
    33. 3.6.7 The String API
    34. 3.6.8 Reading the Online API Documentation
    35. 3.6.9 Building Strings
    36. 3.6.10 Text Blocks
    37. 3.7 Input and Output
    38. 3.7.1 Reading Input
    39. 3.7.2 Formatting Output
    40. 3.7.3 File Input and Output
    41. 3.8 Control Flow
    42. 3.8.1 Block Scope
    43. 3.8.2 Conditional Statements
    44. 3.8.3 Loops
    45. 3.8.4 Determinate Loops
    46. 3.8.5 Multiple Selections with switch
    47. 3.8.6 Statements That Break Control Flow
    48. 3.9 Big Numbers
    49. 3.10 Arrays
    50. 3.10.1 Declaring Arrays
    51. 3.10.2 Accessing Array Elements
    52. 3.10.3 The “for each” Loop
    53. 3.10.4 Array Copying
    54. 3.10.5 Command-Line Parameters
    55. 3.10.6 Array Sorting
    56. 3.10.7 Multidimensional Arrays
    57. 3.10.8 Ragged Arrays
  10. Chapter 4: Objects and Classes
    1. 4.1 Introduction to Object-Oriented Programming
    2. 4.1.1 Classes
    3. 4.1.2 Objects
    4. 4.1.3 Identifying Classes
    5. 4.1.4 Relationships between Classes
    6. 4.2 Using Predefined Classes
    7. 4.2.1 Objects and Object Variables
    8. 4.2.2 The LocalDate Class of the Java Library
    9. 4.2.3 Mutator and Accessor Methods
    10. 4.3 Defining Your Own Classes
    11. 4.3.1 An Employee Class
    12. 4.3.2 Use of Multiple Source Files
    13. 4.3.3 Dissecting the Employee Class
    14. 4.3.4 First Steps with Constructors
    15. 4.3.5 Declaring Local Variables with var
    16. 4.3.6 Working with null References
    17. 4.3.7 Implicit and Explicit Parameters
    18. 4.3.8 Benefits of Encapsulation
    19. 4.3.9 Class-Based Access Privileges
    20. 4.3.10 Private Methods
    21. 4.3.11 Final Instance Fields
    22. 4.4 Static Fields and Methods
    23. 4.4.1 Static Fields
    24. 4.4.2 Static Constants
    25. 4.4.3 Static Methods
    26. 4.4.4 Factory Methods
    27. 4.4.5 The main Method
    28. 4.5 Method Parameters
    29. 4.6 Object Construction
    30. 4.6.1 Overloading
    31. 4.6.2 Default Field Initialization
    32. 4.6.3 The Constructor with No Arguments
    33. 4.6.4 Explicit Field Initialization
    34. 4.6.5 Parameter Names
    35. 4.6.6 Calling Another Constructor
    36. 4.6.7 Initialization Blocks
    37. 4.6.8 Object Destruction and the finalize Method
    38. 4.7 Records
    39. 4.7.1 The Record Concept
    40. 4.7.2 Constructors: Canonical, Custom, and Compact
    41. 4.8 Packages
    42. 4.8.1 Package Names
    43. 4.8.2 Class Importation
    44. 4.8.3 Static Imports
    45. 4.8.4 Addition of a Class into a Package
    46. 4.8.5 Package Access
    47. 4.8.6 The Class Path
    48. 4.8.7 Setting the Class Path
    49. 4.9 JAR Files
    50. 4.9.1 Creating JAR files
    51. 4.9.2 The Manifest
    52. 4.9.3 Executable JAR Files
    53. 4.9.4 Multi-Release JAR Files
    54. 4.9.5 A Note about Command-Line Options
    55. 4.10 Documentation Comments
    56. 4.10.1 Comment Insertion
    57. 4.10.2 Class Comments
    58. 4.10.3 Method Comments
    59. 4.10.4 Field Comments
    60. 4.10.5 General Comments
    61. 4.10.6 Package Comments
    62. 4.10.7 Comment Extraction
    63. 4.11 Class Design Hints
  11. Chapter 5: Inheritance
    1. 5.1 Classes, Superclasses, and Subclasses
    2. 5.1.1 Defining Subclasses
    3. 5.1.2 Overriding Methods
    4. 5.1.3 Subclass Constructors
    5. 5.1.4 Inheritance Hierarchies
    6. 5.1.5 Polymorphism
    7. 5.1.6 Understanding Method Calls
    8. 5.1.7 Preventing Inheritance: Final Classes and Methods
    9. 5.1.8 Casting
    10. 5.1.9 Pattern Matching for instanceof
    11. 5.1.10 Protected Access
    12. 5.2 Object: The Cosmic Superclass
    13. 5.2.1 Variables of Type Object
    14. 5.2.2 The equals Method
    15. 5.2.3 Equality Testing and Inheritance
    16. 5.2.4 The hashCode Method
    17. 5.2.5 The toString Method
    18. 5.3 Generic Array Lists
    19. 5.3.1 Declaring Array Lists
    20. 5.3.2 Accessing Array List Elements
    21. 5.3.3 Compatibility between Typed and Raw Array Lists
    22. 5.4 Object Wrappers and Autoboxing
    23. 5.5 Methods with a Variable Number of Parameters
    24. 5.6 Abstract Classes
    25. 5.7 Enumeration Classes
    26. 5.8 Sealed Classes
    27. 5.9 Reflection
    28. 5.9.1 The Class Class
    29. 5.9.2 A Primer on Declaring Exceptions
    30. 5.9.3 Resources
    31. 5.9.4 Using Reflection to Analyze the Capabilities of Classes
    32. 5.9.5 Using Reflection to Analyze Objects at Runtime
    33. 5.9.6 Using Reflection to Write Generic Array Code
    34. 5.9.7 Invoking Arbitrary Methods and Constructors
    35. 5.10 Design Hints for Inheritance
  12. Chapter 6: Interfaces, Lambda Expressions, and Inner Classes
    1. 6.1 Interfaces
    2. 6.1.1 The Interface Concept
    3. 6.1.2 Properties of Interfaces
    4. 6.1.3 Interfaces and Abstract Classes
    5. 6.1.4 Static and Private Methods
    6. 6.1.5 Default Methods
    7. 6.1.6 Resolving Default Method Conflicts
    8. 6.1.7 Interfaces and Callbacks
    9. 6.1.8 The Comparator Interface
    10. 6.1.9 Object Cloning
    11. 6.2 Lambda Expressions
    12. 6.2.1 Why Lambdas?
    13. 6.2.2 The Syntax of Lambda Expressions
    14. 6.2.3 Functional Interfaces
    15. 6.2.4 Method References
    16. 6.2.5 Constructor References
    17. 6.2.6 Variable Scope
    18. 6.2.7 Processing Lambda Expressions
    19. 6.2.8 More about Comparators
    20. 6.3 Inner Classes
    21. 6.3.1 Use of an Inner Class to Access Object State
    22. 6.3.2 Special Syntax Rules for Inner Classes
    23. 6.3.3 Are Inner Classes Useful? Actually Necessary? Secure?
    24. 6.3.4 Local Inner Classes
    25. 6.3.5 Accessing Variables from Outer Methods
    26. 6.3.6 Anonymous Inner Classes
    27. 6.3.7 Static Inner Classes
    28. 6.4 Service Loaders
    29. 6.5 Proxies
    30. 6.5.1 When to Use Proxies
    31. 6.5.2 Creating Proxy Objects
    32. 6.5.3 Properties of Proxy Classes
  13. Chapter 7: Exceptions, Assertions, and Logging
    1. 7.1 Dealing with Errors
    2. 7.1.1 The Classification of Exceptions
    3. 7.1.2 Declaring Checked Exceptions
    4. 7.1.3 How to Throw an Exception
    5. 7.1.4 Creating Exception Classes
    6. 7.2 Catching Exceptions
    7. 7.2.1 Catching an Exception
    8. 7.2.2 Catching Multiple Exceptions
    9. 7.2.3 Rethrowing and Chaining Exceptions
    10. 7.2.4 The finally Clause
    11. 7.2.5 The try-with-Resources Statement
    12. 7.2.6 Analyzing Stack Trace Elements
    13. 7.3 Tips for Using Exceptions
    14. 7.4 Using Assertions
    15. 7.4.1 The Assertion Concept
    16. 7.4.2 Assertion Enabling and Disabling
    17. 7.4.3 Using Assertions for Parameter Checking
    18. 7.4.4 Using Assertions for Documenting Assumptions
    19. 7.5 Logging
    20. 7.5.1 Basic Logging
    21. 7.5.2 Advanced Logging
    22. 7.5.3 Changing the Log Manager Configuration
    23. 7.5.4 Localization
    24. 7.5.5 Handlers
    25. 7.5.6 Filters
    26. 7.5.7 Formatters
    27. 7.5.8 A Logging Recipe
    28. 7.6 Debugging Tips
  14. Chapter 8: Generic Programming
    1. 8.1 Why Generic Programming?
    2. 8.1.1 The Advantage of Type Parameters
    3. 8.1.2 Who Wants to Be a Generic Programmer?
    4. 8.2 Defining a Simple Generic Class
    5. 8.3 Generic Methods
    6. 8.4 Bounds for Type Variables
    7. 8.5 Generic Code and the Virtual Machine
    8. 8.5.1 Type Erasure
    9. 8.5.2 Translating Generic Expressions
    10. 8.5.3 Translating Generic Methods
    11. 8.5.4 Calling Legacy Code
    12. 8.6 Restrictions and Limitations
    13. 8.6.1 Type Parameters Cannot Be Instantiated with Primitive Types
    14. 8.6.2 Runtime Type Inquiry Only Works with Raw Types
    15. 8.6.3 You Cannot Create Arrays of Parameterized Types
    16. 8.6.4 Varargs Warnings
    17. 8.6.5 You Cannot Instantiate Type Variables
    18. 8.6.6 You Cannot Construct a Generic Array
    19. 8.6.7 Type Variables Are Not Valid in Static Contexts of Generic Classes
    20. 8.6.8 You Cannot Throw or Catch Instances of a Generic Class
    21. 8.6.9 You Can Defeat Checked Exception Checking
    22. 8.6.10 Beware of Clashes after Erasure
    23. 8.7 Inheritance Rules for Generic Types
    24. 8.8 Wildcard Types
    25. 8.8.1 The Wildcard Concept
    26. 8.8.2 Supertype Bounds for Wildcards
    27. 8.8.3 Unbounded Wildcards
    28. 8.8.4 Wildcard Capture
    29. 8.9 Reflection and Generics
    30. 8.9.1 The Generic Class Class
    31. 8.9.2 Using Class<T> Parameters for Type Matching
    32. 8.9.3 Generic Type Information in the Virtual Machine
    33. 8.9.4 Type Literals
  15. Chapter 9: Collections
    1. 9.1 The Java Collections Framework
    2. 9.1.1 Separating Collection Interfaces and Implementation
    3. 9.1.2 The Collection Interface
    4. 9.1.3 Iterators
    5. 9.1.4 Generic Utility Methods
    6. 9.2 Interfaces in the Collections Framework
    7. 9.3 Concrete Collections
    8. 9.3.1 Linked Lists
    9. 9.3.2 Array Lists
    10. 9.3.3 Hash Sets
    11. 9.3.4 Tree Sets
    12. 9.3.5 Queues and Deques
    13. 9.3.6 Priority Queues
    14. 9.4 Maps
    15. 9.4.1 Basic Map Operations
    16. 9.4.2 Updating Map Entries
    17. 9.4.3 Map Views
    18. 9.4.4 Weak Hash Maps
    19. 9.4.5 Linked Hash Sets and Maps
    20. 9.4.6 Enumeration Sets and Maps
    21. 9.4.7 Identity Hash Maps
    22. 9.5 Copies and Views
    23. 9.5.1 Small Collections
    24. 9.5.2 Unmodifiable Copies and Views
    25. 9.5.3 Subranges
    26. 9.5.4 Checked Views
    27. 9.5.5 Synchronized Views
    28. 9.5.6 A Note on Optional Operations
    29. 9.6 Algorithms
    30. 9.6.1 Why Generic Algorithms?
    31. 9.6.2 Sorting and Shuffling
    32. 9.6.3 Binary Search
    33. 9.6.4 Simple Algorithms
    34. 9.6.5 Bulk Operations
    35. 9.6.6 Converting between Collections and Arrays
    36. 9.6.7 Writing Your Own Algorithms
    37. 9.7 Legacy Collections
    38. 9.7.1 The Hashtable Class
    39. 9.7.2 Enumerations
    40. 9.7.3 Property Maps
    41. 9.7.4 Stacks
    42. 9.7.5 Bit Sets
  16. Chapter 10: Graphical User Interface Programming
    1. 10.1 A History of Java User Interface Toolkits
    2. 10.2 Displaying Frames
    3. 10.2.1 Creating a Frame
    4. 10.2.2 Frame Properties
    5. 10.3 Displaying Information in a Component
    6. 10.3.1 Working with 2D Shapes
    7. 10.3.2 Using Color
    8. 10.3.3 Using Fonts
    9. 10.3.4 Displaying Images
    10. 10.4 Event Handling
    11. 10.4.1 Basic Event Handling Concepts
    12. 10.4.2 Example: Handling a Button Click
    13. 10.4.3 Specifying Listeners Concisely
    14. 10.4.4 Adapter Classes
    15. 10.4.5 Actions
    16. 10.4.6 Mouse Events
    17. 10.4.7 The AWT Event Hierarchy
    18. 10.5 The Preferences API
  17. Chapter 11: User Interface Components with Swing
    1. 11.1 Swing and the Model-View-Controller Design Pattern
    2. 11.2 Introduction to Layout Management
    3. 11.2.1 Layout Managers
    4. 11.2.2 Border Layout
    5. 11.2.3 Grid Layout
    6. 11.3 Text Input
    7. 11.3.1 Text Fields
    8. 11.3.2 Labels and Labeling Components
    9. 11.3.3 Password Fields
    10. 11.3.4 Text Areas
    11. 11.3.5 Scroll Panes
    12. 11.4 Choice Components
    13. 11.4.1 Checkboxes
    14. 11.4.2 Radio Buttons
    15. 11.4.3 Borders
    16. 11.4.4 Combo Boxes
    17. 11.4.5 Sliders
    18. 11.5 Menus
    19. 11.5.1 Menu Building
    20. 11.5.2 Icons in Menu Items
    21. 11.5.3 Checkbox and Radio Button Menu Items
    22. 11.5.4 Pop-Up Menus
    23. 11.5.5 Keyboard Mnemonics and Accelerators
    24. 11.5.6 Enabling and Disabling Menu Items
    25. 11.5.7 Toolbars
    26. 11.5.8 Tooltips
    27. 11.6 Sophisticated Layout Management
    28. 11.6.1 The Grid Bag Layout
    29. 11.6.2 Custom Layout Managers
    30. 11.7 Dialog Boxes
    31. 11.7.1 Option Dialogs
    32. 11.7.2 Creating Dialogs
    33. 11.7.3 Data Exchange
    34. 11.7.4 File Dialogs
  18. Chapter 12: Concurrency
    1. 12.1 What Are Threads?
    2. 12.2 Thread States
    3. 12.2.1 New Threads
    4. 12.2.2 Runnable Threads
    5. 12.2.3 Blocked and Waiting Threads
    6. 12.2.4 Terminated Threads
    7. 12.3 Thread Properties
    8. 12.3.1 Interrupting Threads
    9. 12.3.2 Daemon Threads
    10. 12.3.3 Thread Names
    11. 12.3.4 Handlers for Uncaught Exceptions
    12. 12.3.5 Thread Priorities
    13. 12.4 Synchronization
    14. 12.4.1 An Example of a Race Condition
    15. 12.4.2 The Race Condition Explained
    16. 12.4.3 Lock Objects
    17. 12.4.4 Condition Objects
    18. 12.4.5 The synchronized Keyword
    19. 12.4.6 Synchronized Blocks
    20. 12.4.7 The Monitor Concept
    21. 12.4.8 Volatile Fields
    22. 12.4.9 Final Variables
    23. 12.4.10 Atomics
    24. 12.4.11 Deadlocks
    25. 12.4.12 Why the stop and suspend Methods Are Deprecated
    26. 12.4.13 On-Demand Initialization
    27. 12.4.14 Thread-Local Variables
    28. 12.5 Thread-Safe Collections
    29. 12.5.1 Blocking Queues
    30. 12.5.2 Efficient Maps, Sets, and Queues
    31. 12.5.3 Atomic Update of Map Entries
    32. 12.5.4 Bulk Operations on Concurrent Hash Maps
    33. 12.5.5 Concurrent Set Views
    34. 12.5.6 Copy on Write Arrays
    35. 12.5.7 Parallel Array Algorithms
    36. 12.5.8 Older Thread-Safe Collections
    37. 12.6 Tasks and Thread Pools
    38. 12.6.1 Callables and Futures
    39. 12.6.2 Executors
    40. 12.6.3 Controlling Groups of Tasks
    41. 12.6.4 The Fork-Join Framework
    42. 12.7 Asynchronous Computations
    43. 12.7.1 Completable Futures
    44. 12.7.2 Composing Completable Futures
    45. 12.7.3 Long-Running Tasks in User Interface Callbacks
    46. 12.8 Processes
    47. 12.8.1 Building a Process
    48. 12.8.2 Running a Process
    49. 12.8.3 Process Handles
  19. Appendix
3.15.147.53