Core Java Volume I (Cay S. Horstmann)(Z-Library)
Java
No Description
151
Views
0
Downloads
0.00
Total Donations
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Page
1
(This page has no text content)
Page
2
(This page has no text content)
Page
3
Core Java Volume I: Fundamentals Fourteenth Edition Cay S. Horstmann
Page
4
. Cover image: emotionPicture/stock.adobe.com Figure 1.1: Sourceforge Figures 2.2, 3.2-3.5, 4.9, 5.4, 7.2, 10.5, 10.6, 11.1: Oracle Corporation Figures 2.3-2.5, 12.2: Eclipse Foundation AISBL Figure 4.2: Violet UML Editor Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. Please contact us with concerns about any potential bias at pearson.com/en- us/report-bias.html. Author websites are not owned or managed by Pearson. Visit us on the Web: informit.com Library of Congress Control Number: 2025945021 Copyright © 2026 Pearson Education, Inc. Hoboken, New Jersey Portions copyright © 1996-2013 Oracle and/or its affiliates. All Rights Reserved. Oracle America Inc. does not make any representations or warranties as to the accuracy, adequacy or completeness of any information contained in this work, and is not responsible for any errors or omissions. The views expressed in this book are those of the author and do not necessarily reflect the views of Oracle. Microsoft and/or its respective suppliers make no representations about the suitability of the information contained in the documents and related graphics published as part of the services for any purpose. All such documents and related graphics are provided "as is" without warranty of any kind. Microsoft and/or its respective suppliers hereby disclaim all warranties and conditions with regard to this information, including all warranties and conditions of merchantability, whether express, implied or statutory, fitness for a particular purpose, title and non-infringement. In no event shall Microsoft and/or its respective suppliers be liable for any special, indirect or consequential damages
Page
5
or any damages whatsoever resulting from loss of use, data or profits, whether in an action of contract, negligence or other tortious action, arising out of or in connection with the use or performance of information available from the services. The documents and related graphics contained herein could include technical inaccuracies or typographical errors. Changes are periodically added to the information herein. Microsoft and/or its respective suppliers may make improvements and/or changes in the product(s) and/or the program(s) described herein at any time. Partial screen shots may be viewed in full within the software version specified. Microsoft® Windows®, and Microsoft Office® are registered trademarks of the Microsoft Corporation in the U.S.A. and other countries. This book is not sponsored or endorsed by or affiliated with the Microsoft Corporation. All rights reserved. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, request forms and the appropriate contacts within the Pearson Education Global Rights & Permissions Department, please visit pearson.com/en-us/global-permission-granting.html. ISBN-13: 978-0-13-555857-7 ISBN-10: 0-13-555857-3
Page
6
Contents Preface Acknowledgments 1. An Introduction to Java 1.1. Java as a Programming Platform 1.2. The Java “White Paper” Buzzwords 1.2.1. Simple 1.2.2. Object-Oriented 1.2.3. Distributed 1.2.4. Robust 1.2.5. Secure 1.2.6. Architecture-Neutral 1.2.7. Portable 1.2.8. Interpreted 1.2.9. High-Performance 1.2.10. Multithreaded 1.2.11. Dynamic 1.3. Java Applets and the Internet 1.4. A Short History of Java 1.5. Common Misconceptions about Java 2. The Java Programming Environment 2.1. Installing the Java Development Kit 2.1.1. Downloading the JDK 2.1.2. Setting Up the JDK 2.1.3. Source Files and Documentation 2.2. Using the Command-Line Tools 2.3. Using an Integrated Development Environment
Page
7
2.4. JShell 3. Fundamental Programming Structures in Java 3.1. A Simple Java Program 3.2. Comments 3.3. Data Types 3.3.1. Integer Types 3.3.2. Floating-Point Types 3.3.3. The char Type 3.3.4. Unicode and the char Type 3.3.5. The boolean Type 3.4. Variables and Constants 3.4.1. Declaring Variables 3.4.2. Initializing Variables 3.4.3. Constants 3.4.4. Enumerated Types 3.5. Operators 3.5.1. Arithmetic Operators 3.5.2. Mathematical Functions and Constants 3.5.3. Conversions between Numeric Types 3.5.4. Casts 3.5.5. Assignment 3.5.6. Increment and Decrement Operators 3.5.7. Relational and boolean Operators 3.5.8. The Conditional Operator 3.5.9. Switch Expressions 3.5.10. Bitwise Operators 3.5.11. Parentheses and Operator Hierarchy 3.6. Strings 3.6.1. Concatenation
Page
8
3.6.2. Static and Instance Methods 3.6.3. Indexes and Substrings 3.6.4. Strings Are Immutable 3.6.5. Testing Strings for Equality 3.6.6. Empty and Null Strings 3.6.7. The String API 3.6.8. Reading the Online API Documentation 3.6.9. Building Strings 3.6.10. Text Blocks 3.7. Input and Output 3.7.1. Reading Input 3.7.2. Formatting Output 3.8. Control Flow 3.8.1. Block Scope 3.8.2. Conditional Statements 3.8.3. Loops 3.8.4. Determinate Loops 3.8.5. Multiple Selections with switch 3.8.6. Statements That Break Control Flow 3.9. Big Numbers 3.10. Arrays 3.10.1. Declaring Arrays 3.10.2. Accessing Array Elements 3.10.3. The “for each” Loop 3.10.4. Array Copying 3.10.5. Command-Line Arguments 3.10.6. Array Sorting 3.10.7. Multidimensional Arrays 3.10.8. Ragged Arrays
Page
9
4. Objects and Classes 4.1. Introduction to Object-Oriented Programming 4.1.1. Classes 4.1.2. Objects 4.1.3. Identifying Classes 4.1.4. Relationships between Classes 4.2. Using Predefined Classes 4.2.1. Objects and Object Variables 4.2.2. The LocalDate Class of the Java Library 4.2.3. Mutator and Accessor Methods 4.3. Defining Your Own Classes 4.3.1. An Employee Class 4.3.2. Dissecting the Employee Class 4.3.3. First Steps with Constructors 4.3.4. Declaring Local Variables with var 4.3.5. Working with null References 4.3.6. Implicit and Explicit Parameters 4.3.7. Benefits of Encapsulation 4.3.8. Class-Based Access Privileges 4.3.9. Private Methods 4.3.10. Final Instance Fields 4.4. Static Fields and Methods 4.4.1. Static Fields 4.4.2. Static Constants 4.4.3. Static Methods 4.4.4. Factory Methods 4.4.5. The main Method 4.5. Method Parameters
Page
10
4.6. Object Construction 4.6.1. Overloading 4.6.2. Default Field Initialization 4.6.3. The Constructor with No Arguments 4.6.4. Explicit Field Initialization 4.6.5. Parameter Names 4.6.6. Calling Another Constructor 4.6.7. Initialization Blocks 4.6.8. Static Initialization 4.7. Records 4.7.1. The Record Concept 4.7.2. Constructors: Canonical, Compact, and Custom 4.8. Packages 4.8.1. Encapsulation 4.8.2. Package Names 4.8.3. Class Importation 4.8.4. Module Imports 4.8.5. Static Imports 4.8.6. Addition of a Class into a Package 4.8.7. Compiling with Packages 4.8.8. Package Access 4.8.9. The Class Path 4.8.10. Setting the Class Path 4.9. JAR Files 4.9.1. Creating JAR files 4.9.2. The Manifest 4.9.3. Executable JAR Files 4.9.4. Multi-Release JAR Files
Page
11
4.9.5. A Note about Command-Line Options 4.10. Documentation Comments 4.10.1. Comment Insertion 4.10.2. Class Comments 4.10.3. Method Comments 4.10.4. Field Comments 4.10.5. Package Comments 4.10.6. HTML Markup 4.10.7. Links 4.10.8. General Comments 4.10.9. Code Snippets 4.10.10. Comment Extraction 4.11. Class Design Hints 5. Inheritance 5.1. Classes, Superclasses, and Subclasses 5.1.1. Defining Subclasses 5.1.2. Overriding Methods 5.1.3. Subclass Constructors 5.1.4. Inheritance Hierarchies 5.1.5. Polymorphism 5.1.6. Understanding Method Calls 5.1.7. Preventing Inheritance: Final Classes and Methods 5.1.8. Casting 5.1.9. Pattern Matching for instanceof 5.1.10. Protected Access 5.2. Object: The Cosmic Superclass 5.2.1. Variables of Type Object 5.2.2. The equals Method
Page
12
5.2.3. Equality Testing and Inheritance 5.2.4. The hashCode Method 5.2.5. The toString Method 5.3. Generic Array Lists 5.3.1. Declaring Array Lists 5.3.2. Accessing Array List Elements 5.3.3. Compatibility between Typed and Raw Array Lists 5.4. Object Wrappers and Autoboxing 5.5. Methods with a Variable Number of Arguments 5.6. Abstract Classes 5.7. Enumeration Classes 5.8. Sealed Classes 5.9. Pattern Matching 5.9.1. Null Handling 5.9.2. Guards 5.9.3. Exhaustiveness 5.9.4. Dominance 5.9.5. Patterns and Constants 5.9.6. Variable Scope and Fallthrough 5.10. Reflection 5.10.1. The Class Class 5.10.2. A Primer on Declaring Exceptions 5.10.3. Resources 5.10.4. Using Reflection to Analyze the Capabilities of Classes 5.10.5. Using Reflection to Analyze Objects at Runtime 5.10.6. Using Reflection to Write Generic Array Code
Page
13
5.10.7. Invoking Arbitrary Methods and Constructors 5.11. Design Hints for Inheritance 6. Interfaces, Lambda Expressions, and Inner Classes 6.1. Interfaces 6.1.1. The Interface Concept 6.1.2. Properties of Interfaces 6.1.3. Interfaces and Abstract Classes 6.1.4. Static and Private Methods 6.1.5. Default Methods 6.1.6. Resolving Default Method Conflicts 6.1.7. Interfaces and Callbacks 6.1.8. The Comparator Interface 6.1.9. Object Cloning 6.2. Lambda Expressions 6.2.1. Why Lambdas? 6.2.2. The Syntax of Lambda Expressions 6.2.3. Functional Interfaces 6.2.4. Function Types 6.2.5. Method References 6.2.6. Constructor References 6.2.7. Variable Scope 6.2.8. Lambda Expressions and this 6.2.9. Processing Lambda Expressions 6.2.10. Creating Comparators 6.3. Inner Classes 6.3.1. Use of an Inner Class to Access Object State 6.3.2. Special Syntax Rules for Inner Classes 6.3.3. Are Inner Classes Useful? Actually Necessary? Secure?
Page
14
6.3.4. Local Inner Classes 6.3.5. Accessing Variables from Outer Methods 6.3.6. Anonymous Inner Classes 6.3.7. Static Classes 6.3.8. Nested Records 6.4. Service Loaders 6.5. Proxies 6.5.1. When to Use Proxies 6.5.2. Creating Proxy Objects 6.5.3. Properties of Proxy Classes 7. Exceptions, Assertions, and Logging 7.1. Dealing with Errors 7.1.1. The Classification of Exceptions 7.1.2. Declaring Checked Exceptions 7.1.3. How to Throw an Exception 7.1.4. Creating Exception Classes 7.2. Catching Exceptions 7.2.1. Catching an Exception 7.2.2. Catching Multiple Exceptions 7.2.3. Rethrowing and Chaining Exceptions 7.2.4. The finally Clause 7.2.5. The try-with-Resources Statement 7.2.6. Analyzing Stack Trace Elements 7.3. Tips for Using Exceptions 7.4. Using Assertions 7.4.1. The Assertion Concept 7.4.2. Assertion Enabling and Disabling 7.4.3. Using Assertions for Parameter Checking
Page
15
7.4.4. Using Assertions for Documenting Assumptions 7.5. Logging 7.5.1. Should You Use the Java Logging Framework? 7.5.2. Logging 101 7.5.3. The Platform Logging API 7.5.4. Logging Configuration 7.5.5. Log Handlers 7.5.6. Filters and Formatters 7.5.7. A Logging Recipe 7.6. Debugging Tips 8. Generic Programming 8.1. Type Parameters 8.1.1. The Advantage of Generic Programming 8.1.2. Who Wants to Be a Generic Programmer? 8.1.3. Defining a Simple Generic Class 8.1.4. Generic Methods 8.1.5. Bounds for Type Variables 8.1.6. Generic Exceptions 8.2. Generic Code and the Virtual Machine 8.2.1. Type Erasure 8.2.2. Translating Generic Expressions 8.2.3. Translating Generic Methods 8.2.4. Calling Legacy Code 8.3. Inheritance Rules for Generic Types 8.4. Wildcard Types 8.4.1. The Wildcard Concept 8.4.2. Supertype Bounds for Wildcards 8.4.3. Unbounded Wildcards
Page
16
8.4.4. Wildcard Capture 8.5. Restrictions and Limitations 8.5.1. Type Parameters Cannot Be Instantiated with Primitive Types 8.5.2. Casts Only Work with Raw Types 8.5.3. You Cannot Create Arrays of Parameterized Types 8.5.4. Varargs Warnings 8.5.5. Generic Varargs Do Not Spread Primitive Arrays 8.5.6. You Cannot Instantiate Type Variables 8.5.7. You Cannot Construct a Generic Array 8.5.8. Type Variables Are Not Valid in Static Contexts of Generic Classes 8.5.9. You Can Defeat Checked Exception Checking 8.5.10. Beware of Clashes after Erasure 8.5.11. Type Inference in Generic Record Patterns is Limited 8.6. Reflection and Generics 8.6.1. The Generic Class Class 8.6.2. Using Class<T> Parameters for Type Matching 8.6.3. Generic Type Information in the Virtual Machine 8.6.4. Type Literals 9. Collections 9.1. The Java Collections Framework 9.1.1. Separating Collection Interfaces and Implementation 9.1.2. The Collection Interface
Page
17
9.1.3. Iterators 9.1.4. Generic Utility Methods 9.2. Interfaces in the Collections Framework 9.3. Concrete Collections 9.3.1. Linked Lists 9.3.2. Array Lists 9.3.3. Hash Sets 9.3.4. Tree Sets 9.3.5. Queues and Deques 9.3.6. Priority Queues 9.4. Maps 9.4.1. Basic Map Operations 9.4.2. Updating Map Entries 9.4.3. Map Views 9.4.4. Weak Hash Maps 9.4.5. Linked Hash Sets and Maps 9.4.6. Enumeration Sets and Maps 9.4.7. Identity Hash Maps 9.5. Copies and Views 9.5.1. Small Collections 9.5.2. Unmodifiable Copies and Views 9.5.3. Subranges 9.5.4. Sets From Boolean-Valued Maps 9.5.5. Reversed Views 9.5.6. Checked Views 9.5.7. Synchronized Views 9.5.8. A Note on Optional Operations 9.6. Algorithms 9.6.1. Why Generic Algorithms?
Page
18
9.6.2. Sorting and Shuffling 9.6.3. Binary Search 9.6.4. Simple Algorithms 9.6.5. Bulk Operations 9.6.6. Converting between Collections and Arrays 9.6.7. Writing Your Own Algorithms 9.7. Legacy Collections 9.7.1. The Hashtable Class 9.7.2. Enumerations 9.7.3. Property Maps 9.7.4. System Properties 9.7.5. Stacks 9.7.6. Bit Sets 10. Concurrency 10.1. Running Threads 10.2. Thread States 10.2.1. New Threads 10.2.2. Runnable Threads 10.2.3. Blocked and Waiting Threads 10.2.4. Terminated Threads 10.3. Thread Properties 10.3.1. Virtual Threads 10.3.2. Thread Interruption 10.3.3. Daemon Threads 10.3.4. Thread Names and Ids 10.3.5. Handlers for Uncaught Exceptions 10.3.6. Thread Priorities 10.3.7. Thread Factories and Builders
Page
19
10.4. Coordinating Tasks 10.4.1. Callables and Futures 10.4.2. Executor Services 10.4.3. Invoking a Group of Tasks 10.4.4. Thread-Local Variables 10.4.5. Scoped Values 10.4.6. The Fork-Join Framework 10.5. Synchronization 10.5.1. An Example of a Race Condition 10.5.2. The Race Condition Explained 10.5.3. Lock Objects 10.5.4. Condition Objects 10.5.5. Deadlocks 10.5.6. The synchronized Keyword 10.5.7. Synchronized Blocks 10.5.8. The Monitor Concept 10.5.9. Volatile Fields 10.5.10. Final Fields 10.5.11. Atomics 10.5.12. On-Demand Initialization 10.5.13. Safe Publication 10.5.14. Sharing with Thread-Local Variables 10.6. Thread-Safe Collections 10.6.1. Blocking Queues 10.6.2. Efficient Maps, Sets, and Queues 10.6.3. Atomic Update of Map Entries 10.6.4. Bulk Operations on Concurrent Hash Maps 10.6.5. Concurrent Set Views 10.6.6. Copy on Write Arrays
Page
20
10.6.7. Parallel Array Algorithms 10.6.8. Older Thread-Safe Collections 10.7. Asynchronous Computations 10.7.1. Completable Futures 10.7.2. Composing Completable Futures 10.7.3. Long-Running Tasks in User Interface Callbacks 10.8. Processes 10.8.1. Building a Process 10.8.2. Running a Process 10.8.3. Process Handles 11. Annotations 11.1. Using Annotations 11.1.1. Annotation Elements 11.1.2. Multiple and Repeated Annotations 11.1.3. Annotating Declarations 11.1.4. Annotating Type Uses 11.1.5. Receiver Parameters 11.2. Defining Annotations 11.3. Annotations in the Java API 11.3.1. Annotations for Compilation 11.3.2. Meta-Annotations 11.4. Processing Annotations at Runtime 11.5. Source-Level Annotation Processing 11.5.1. Annotation Processors 11.5.2. The Language Model API 11.5.3. Using Annotations to Generate Source Code 11.6. Bytecode Engineering 11.6.1. Modifying Class Files
The above is a preview of the first 20 pages. Register to read the complete e-book.
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
AI guide
# Core Java Volume I — Reading Guide
## 【One-Line Pitch】
The definitive practical reference for Java developers who want to master the language's fundamentals, from syntax basics to advanced concurrency, written with clear explanations and real-world examples. Ideal for programmers transitioning from other languages or those seeking to deepen their Java expertise.
## 【Book Arc】
- **Opening (~0%–10%)**: Introduces Java's design philosophy as a cleaned-up C++ and walks through the first programs, including the new Java 25 simplified syntax with `void main()` and `IO.println`, plus the evolution of the language through version history.
- **Early (~10%–23%)**: Covers core language mechanics—switch expressions with arrow syntax, text blocks for multi-line strings, console input/output, and the shift from procedural to object-oriented thinking with classes, encapsulation, and the `var` keyword for local type inference.
- **Early (~23%–32%)**: Delves into object-oriented fundamentals—working with null references safely, records for immutable data carriers, packages for code organization, and the modernized command-line options with double-dash conventions.
- **Middle (~32%–48%)**: Explores inheritance hierarchies with the Manager/Employee example, abstract classes and methods, the `toString` method contract, ArrayList operations, and pattern matching with `instanceof` for cleaner type checks.
- **Late (~48%–100%)**: Advances into reflection for runtime class inspection, then the substantial concurrency chapter covering callables, futures, executor services, synchronization with locks and conditions, race conditions, thread-safe collections, and the fork-join framework.
## 【Key Takeaways】
- **Java's simplicity is deliberate** (Opening): The language omits C++ features like pointer arithmetic and operator overloading to reduce programmer error, making it more approachable while retaining familiar syntax for C++ developers.
- **Modern Java has simplified program entry** (Opening): Java 25's `void main()` without class wrappers and the `IO` utility make introductory programs dramatically shorter, though older versions require traditional `public static void main(String[] args)` structure.
- **Switch expressions are now powerful** (Early): The arrow syntax with multiple labels per case and enum support eliminates break statements and accidental fall-through, while requiring a `default` case for numeric or String selectors.
- **Text blocks handle multi-line strings elegantly** (Early): Common indentation is automatically stripped, but mixed tabs and spaces can cause subtle bugs—keep indentation consistent or place non-Java content at the left margin.
- **Encapsulation is the foundation of OO design** (Early): Classes as "cookie cutters" producing object "cookies" with private fields and public methods creates black-box behavior that enables reuse and reliability through information hiding.
- **`var` reduces verbosity without losing safety** (Early): Local variable type inference works only when the type is obvious from initialization, and should be avoided with numeric types to prevent confusion between `0`, `0L`, and `0.0`.
- **Inheritance requires careful design** (Middle): Subclasses only specify differences from superclasses, but private fields aren't directly accessible—the JLS distinction between "having" and "inheriting" fields confuses many developers.
- **Concurrency coordination is a major topic** (Late): The book dedicates substantial coverage to coordinating tasks with callables and futures, executor services, synchronization via locks and conditions, and thread-safe collections like blocking queues and concurrent hash maps.
## 【Reading Tips】
- **Skim the version history table** (Opening): It provides useful context for language evolution but isn't essential for practical coding—return to it only if you need to understand feature availability.
- **Deep-read the switch expression and text block sections** (Early): These are frequently used in modern Java and have subtle rules (default requirements, indentation stripping) that trip up even experienced developers.
- **Study the Manager/Employee inheritance example carefully** (Middle): It's the canonical illustration of how subclassing works, including the confusing point about private field inheritance.
- **The concurrency chapter deserves multiple passes** (Late): Start with callables and futures for task coordination, then tackle synchronization concepts like race conditions and lock objects before moving to thread-safe collections.
- **Use the code listings as your primary study material**: The book's examples are practical and runnable—type them out and experiment rather than just reading explanations.
## 【Coverage Limits】
This guide covers the core language fundamentals and object-oriented programming through the middle sections. The extensive concurrency chapter (Chapter 10) is summarized at a high level; excerpts do not cover advanced topics like the fork-join framework details, scoped values, or the complete synchronization examples.
##
Passage locations
Excerpt 1
ually not the most commonly used programming language. Many developers used Visual Basic and its drag-and-drop programming environment. These developers did...
View in text
Excerpt 2
ou may want to break completely out of all the nested loops. It is inconvenient to program that simply by adding extra conditions to the various loop tests....
View in text
Excerpt 3
ion format of the tar command without dashes: jar cvf . . . JEP 293 (https://openjdk.org/jeps/293) provides guidelines for moving toward a more common option...
View in text
Excerpt 4
to check whether an object has a particular type. The most convenient form of instanceof declares a variable of the matching type: Employee e = . . .; String...
View in text
Recommended for You
{{#thumbnailUrl}}
{{/thumbnailUrl}}
{{^thumbnailUrl}}
{{/thumbnailUrl}}
Loading recommended books...
Failed to load, please try again later
Tip the Site
Scan the WeChat Pay or Alipay code to tip. No login required.
WeChat Pay
Alipay