Core Java® Volume I—Fundamentals, Tenth Edition

Book description

Core Java® has long been recognized as the leading, no-nonsense tutorial and reference for experienced programmers who want to write robust Java code for real-world applications. Now, Core Java®, Volume I-Fundamentals, Tenth Edition, has been extensively updated to reflect the most eagerly awaited and innovative version of Java in years: Java SE 8. Rewritten and reorganized to illuminate new Java SE 8 features, idioms, and best practices, it contains hundreds of example programs-all carefully crafted for easy understanding and practical applicability.

 

Writing for serious programmers solving real-world problems, Cay Horstmann helps you achieve a deep understanding of the Java language and library. In this first volume of the two-volume work, Horstmann focuses on fundamental language concepts and the foundations of modern user interface programming. You'll find in-depth coverage of topics ranging from Java object-oriented programming to generics, collections, lambda expressions, Swing UI design, and the latest approaches to concurrency and functional programming. This guide will help you

  • Leverage your existing programming knowledge to quickly master core Java syntax
  • Understand how encapsulation, classes, and inheritance work in Java
  • Master interfaces, inner classes, and lambda expressions for functional programming
  • Improve program robustness with exception handling and effective debugging
  • Write safer, more readable programs with generics and strong typing
  • Use pre-built collections to collect multiple objects for later retrieval
  • Master concurrent programming techniques from the ground up
  • Build modern cross-platform GUIs with standard Swing components
  • Deploy configurable applications and applets, and deliver them across the Internet
  • Simplify concurrency and enhance performance with new functional techniques

If you're an experienced programmer moving to Java SE 8, Core Java®, Tenth Edition, will be your reliable, practical companion-now and for many years to come.

 

Look for the companion volume, Core Java®, Volume II-Advanced Features, Tenth Edition (ISBN-13: 978-0-13-417729-8), for coverage of Java 8 streams, input and output, XML, databases, annotations, and other advanced topics.

 

Register your productor convenient access to downloads, updates, and corrections as they become available. See inside the book for information.

Table of contents

  1. About This E-Book
  2. Title Page
  3. Copyright Page
  4. Contents
  5. Preface
    1. To the Reader
    2. A Tour of This Book
    3. Conventions
    4. Sample Code
  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
      1. 1.2.1 Simple
      2. 1.2.2 Object-Oriented
      3. 1.2.3 Distributed
      4. 1.2.4 Robust
      5. 1.2.5 Secure
      6. 1.2.6 Architecture-Neutral
      7. 1.2.7 Portable
      8. 1.2.8 Interpreted
      9. 1.2.9 High-Performance
      10. 1.2.10 Multithreaded
      11. 1.2.11 Dynamic
    3. 1.3 Java Applets and the Internet
    4. 1.4 A Short History of Java
    5. 1.5 Common Misconceptions about Java
  8. Chapter 2. The Java Programming Environment
    1. 2.1 Installing the Java Development Kit
      1. 2.1.1 Downloading the JDK
      2. 2.1.2 Setting up the JDK
      3. 2.1.3 Installing Source Files and Documentation
    2. 2.2 Using the Command-Line Tools
    3. 2.3 Using an Integrated Development Environment
    4. 2.4 Running a Graphical Application
    5. 2.5 Building and Running Applets
  9. Chapter 3. Fundamental Programming Structures in Java
    1. 3.1 A Simple Java Program
    2. 3.2 Comments
    3. 3.3 Data Types
      1. 3.3.1 Integer Types
      2. 3.3.2 Floating-Point Types
      3. 3.3.3 The char Type
      4. 3.3.4 Unicode and the char Type
      5. 3.3.5 The boolean Type
    4. 3.4 Variables
      1. 3.4.1 Initializing Variables
      2. 3.4.2 Constants
    5. 3.5 Operators
      1. 3.5.1 Mathematical Functions and Constants
      2. 3.5.2 Conversions between Numeric Types
      3. 3.5.3 Casts
      4. 3.5.4 Combining Assignment with Operators
      5. 3.5.5 Increment and Decrement Operators
      6. 3.5.6 Relational and boolean Operators
      7. 3.5.7 Bitwise Operators
      8. 3.5.8 Parentheses and Operator Hierarchy
      9. 3.5.9 Enumerated Types
    6. 3.6 Strings
      1. 3.6.1 Substrings
      2. 3.6.2 Concatenation
      3. 3.6.3 Strings Are Immutable
      4. 3.6.4 Testing Strings for Equality
      5. 3.6.5 Empty and Null Strings
      6. 3.6.6 Code Points and Code Units
      7. 3.6.7 The String API
      8. 3.6.8 Reading the Online API Documentation
      9. 3.6.9 Building Strings
    7. 3.7 Input and Output
      1. 3.7.1 Reading Input
      2. 3.7.2 Formatting Output
      3. 3.7.3 File Input and Output
    8. 3.8 Control Flow
      1. 3.8.1 Block Scope
      2. 3.8.2 Conditional Statements
      3. 3.8.3 Loops
      4. 3.8.4 Determinate Loops
      5. 3.8.5 Multiple Selections—The switch Statement
      6. 3.8.6 Statements That Break Control Flow
    9. 3.9 Big Numbers
    10. 3.10 Arrays
      1. 3.10.1 The “for each” Loop
      2. 3.10.2 Array Initializers and Anonymous Arrays
      3. 3.10.3 Array Copying
      4. 3.10.4 Command-Line Parameters
      5. 3.10.5 Array Sorting
      6. 3.10.6 Multidimensional Arrays
      7. 3.10.7 Ragged Arrays
  10. Chapter 4. Objects and Classes
    1. 4.1 Introduction to Object-Oriented Programming
      1. 4.1.1 Classes
      2. 4.1.2 Objects
      3. 4.1.3 Identifying Classes
      4. 4.1.4 Relationships between Classes
    2. 4.2 Using Predefined Classes
      1. 4.2.1 Objects and Object Variables
      2. 4.2.2 The LocalDate Class of the Java Library
      3. 4.2.3 Mutator and Accessor Methods
    3. 4.3 Defining Your Own Classes
      1. 4.3.1 An Employee Class
      2. 4.3.2 Use of Multiple Source Files
      3. 4.3.3 Dissecting the Employee Class
      4. 4.3.4 First Steps with Constructors
      5. 4.3.5 Implicit and Explicit Parameters
      6. 4.3.6 Benefits of Encapsulation
      7. 4.3.7 Class-Based Access Privileges
      8. 4.3.8 Private Methods
      9. 4.3.9 Final Instance Fields
    4. 4.4 Static Fields and Methods
      1. 4.4.1 Static Fields
      2. 4.4.2 Static Constants
      3. 4.4.3 Static Methods
      4. 4.4.4 Factory Methods
      5. 4.4.5 The main Method
    5. 4.5 Method Parameters
    6. 4.6 Object Construction
      1. 4.6.1 Overloading
      2. 4.6.2 Default Field Initialization
      3. 4.6.3 The Constructor with No Arguments
      4. 4.6.4 Explicit Field Initialization
      5. 4.6.5 Parameter Names
      6. 4.6.6 Calling Another Constructor
      7. 4.6.7 Initialization Blocks
      8. 4.6.8 Object Destruction and the finalize Method
    7. 4.7 Packages
      1. 4.7.1 Class Importation
      2. 4.7.2 Static Imports
      3. 4.7.3 Addition of a Class into a Package
      4. 4.7.4 Package Scope
    8. 4.8 The Class Path
      1. 4.8.1 Setting the Class Path
    9. 4.9 Documentation Comments
      1. 4.9.1 Comment Insertion
      2. 4.9.2 Class Comments
      3. 4.9.3 Method Comments
      4. 4.9.4 Field Comments
      5. 4.9.5 General Comments
      6. 4.9.6 Package and Overview Comments
      7. 4.9.7 Comment Extraction
    10. 4.10 Class Design Hints
  11. Chapter 5. Inheritance
    1. 5.1 Classes, Superclasses, and Subclasses
      1. 5.1.1 Defining Subclasses
      2. 5.1.2 Overriding Methods
      3. 5.1.3 Subclass Constructors
      4. 5.1.4 Inheritance Hierarchies
      5. 5.1.5 Polymorphism
      6. 5.1.6 Understanding Method Calls
      7. 5.1.7 Preventing Inheritance: Final Classes and Methods
      8. 5.1.8 Casting
      9. 5.1.9 Abstract Classes
      10. 5.1.10 Protected Access
    2. 5.2 Object: The Cosmic Superclass
      1. 5.2.1 The equals Method
      2. 5.2.2 Equality Testing and Inheritance
      3. 5.2.3 The hashCode Method
      4. 5.2.4 The toString Method
    3. 5.3 Generic Array Lists
      1. 5.3.1 Accessing Array List Elements
      2. 5.3.2 Compatibility between Typed and Raw Array Lists
    4. 5.4 Object Wrappers and Autoboxing
    5. 5.5 Methods with a Variable Number of Parameters
    6. 5.6 Enumeration Classes
    7. 5.7 Reflection
      1. 5.7.1 The Class Class
      2. 5.7.2 A Primer on Catching Exceptions
      3. 5.7.3 Using Reflection to Analyze the Capabilities of Classes
      4. 5.7.4 Using Reflection to Analyze Objects at Runtime
      5. 5.7.5 Using Reflection to Write Generic Array Code
      6. 5.7.6 Invoking Arbitrary Methods
    8. 5.8 Design Hints for Inheritance
  12. Chapter 6. Interfaces, Lambda Expressions, and Inner Classes
    1. 6.1 Interfaces
      1. 6.1.1 The Interface Concept
      2. 6.1.2 Properties of Interfaces
      3. 6.1.3 Interfaces and Abstract Classes
      4. 6.1.4 Static Methods
      5. 6.1.5 Default Methods
      6. 6.1.6 Resolving Default Method Conflicts
    2. 6.2 Examples of Interfaces
      1. 6.2.1 Interfaces and Callbacks
      2. 6.2.2 The Comparator Interface
      3. 6.2.3 Object Cloning
    3. 6.3 Lambda Expressions
      1. 6.3.1 Why Lambdas?
      2. 6.3.2 The Syntax of Lambda Expressions
      3. 6.3.3 Functional Interfaces
      4. 6.3.4 Method References
      5. 6.3.5 Constructor References
      6. 6.3.6 Variable Scope
      7. 6.3.7 Processing Lambda Expressions
      8. 6.3.8 More about Comparators
    4. 6.4 Inner Classes
      1. 6.4.1 Use of an Inner Class to Access Object State
      2. 6.4.2 Special Syntax Rules for Inner Classes
      3. 6.4.3 Are Inner Classes Useful? Actually Necessary? Secure?
      4. 6.4.4 Local Inner Classes
      5. 6.4.5 Accessing Variables from Outer Methods
      6. 6.4.6 Anonymous Inner Classes
      7. 6.4.7 Static Inner Classes
    5. 6.5 Proxies
      1. 6.5.1 When to Use Proxies
      2. 6.5.2 Creating Proxy Objects
      3. 6.5.3 Properties of Proxy Classes
  13. Chapter 7. Exceptions, Assertions, and Logging
    1. 7.1 Dealing with Errors
      1. 7.1.1 The Classification of Exceptions
      2. 7.1.2 Declaring Checked Exceptions
      3. 7.1.3 How to Throw an Exception
      4. 7.1.4 Creating Exception Classes
    2. 7.2 Catching Exceptions
      1. 7.2.1 Catching an Exception
      2. 7.2.2 Catching Multiple Exceptions
      3. 7.2.3 Rethrowing and Chaining Exceptions
      4. 7.2.4 The finally Clause
      5. 7.2.5 The Try-with-Resources Statement
      6. 7.2.6 Analyzing Stack Trace Elements
    3. 7.3 Tips for Using Exceptions
    4. 7.4 Using Assertions
      1. 7.4.1 The Assertion Concept
      2. 7.4.2 Assertion Enabling and Disabling
      3. 7.4.3 Using Assertions for Parameter Checking
      4. 7.4.4 Using Assertions for Documenting Assumptions
    5. 7.5 Logging
      1. 7.5.1 Basic Logging
      2. 7.5.2 Advanced Logging
      3. 7.5.3 Changing the Log Manager Configuration
      4. 7.5.4 Localization
      5. 7.5.5 Handlers
      6. 7.5.6 Filters
      7. 7.5.7 Formatters
      8. 7.5.8 A Logging Recipe
    6. 7.6 Debugging Tips
  14. Chapter 8. Generic Programming
    1. 8.1 Why Generic Programming?
      1. 8.1.1 The Advantage of Type Parameters
      2. 8.1.2 Who Wants to Be a Generic Programmer?
    2. 8.2 Defining a Simple Generic Class
    3. 8.3 Generic Methods
    4. 8.4 Bounds for Type Variables
    5. 8.5 Generic Code and the Virtual Machine
      1. 8.5.1 Type Erasure
      2. 8.5.2 Translating Generic Expressions
      3. 8.5.3 Translating Generic Methods
      4. 8.5.4 Calling Legacy Code
    6. 8.6 Restrictions and Limitations
      1. 8.6.1 Type Parameters Cannot Be Instantiated with Primitive Types
      2. 8.6.2 Runtime Type Inquiry Only Works with Raw Types
      3. 8.6.3 You Cannot Create Arrays of Parameterized Types
      4. 8.6.4 Varargs Warnings
      5. 8.6.5 You Cannot Instantiate Type Variables
      6. 8.6.6 You Cannot Construct a Generic Array
      7. 8.6.7 Type Variables Are Not Valid in Static Contexts of Generic Classes
      8. 8.6.8 You Cannot Throw or Catch Instances of a Generic Class
      9. 8.6.9 You Can Defeat Checked Exception Checking
      10. 8.6.10 Beware of Clashes after Erasure
    7. 8.7 Inheritance Rules for Generic Types
    8. 8.8 Wildcard Types
      1. 8.8.1 The Wildcard Concept
      2. 8.8.2 Supertype Bounds for Wildcards
      3. 8.8.3 Unbounded Wildcards
      4. 8.8.4 Wildcard Capture
    9. 8.9 Reflection and Generics
      1. 8.9.1 The Generic Class Class
      2. 8.9.2 Using Class<T> Parameters for Type Matching
      3. 8.9.3 Generic Type Information in the Virtual Machine
  15. Chapter 9. Collections
    1. 9.1 The Java Collections Framework
      1. 9.1.1 Separating Collection Interfaces and Implementation
      2. 9.1.2 The Collection Interface
      3. 9.1.3 Iterators
      4. 9.1.4 Generic Utility Methods
      5. 9.1.5 Interfaces in the Collections Framework
    2. 9.2 Concrete Collections
      1. 9.2.1 Linked Lists
      2. 9.2.2 Array Lists
      3. 9.2.3 Hash Sets
      4. 9.2.4 Tree Sets
      5. 9.2.5 Queues and Deques
      6. 9.2.6 Priority Queues
    3. 9.3 Maps
      1. 9.3.1 Basic Map Operations
      2. 9.3.2 Updating Map Entries
      3. 9.3.3 Map Views
      4. 9.3.4 Weak Hash Maps
      5. 9.3.5 Linked Hash Sets and Maps
      6. 9.3.6 Enumeration Sets and Maps
      7. 9.3.7 Identity Hash Maps
    4. 9.4 Views and Wrappers
      1. 9.4.1 Lightweight Collection Wrappers
      2. 9.4.2 Subranges
      3. 9.4.3 Unmodifiable Views
      4. 9.4.4 Synchronized Views
      5. 9.4.5 Checked Views
      6. 9.4.6 A Note on Optional Operations
    5. 9.5 Algorithms
      1. 9.5.1 Sorting and Shuffling
      2. 9.5.2 Binary Search
      3. 9.5.3 Simple Algorithms
      4. 9.5.4 Bulk Operations
      5. 9.5.5 Converting between Collections and Arrays
      6. 9.5.6 Writing Your Own Algorithms
    6. 9.6 Legacy Collections
      1. 9.6.1 The Hashtable Class
      2. 9.6.2 Enumerations
      3. 9.6.3 Property Maps
      4. 9.6.4 Stacks
      5. 9.6.5 Bit Sets
  16. Chapter 10. Graphics Programming
    1. 10.1 Introducing Swing
    2. 10.2 Creating a Frame
    3. 10.3 Positioning a Frame
      1. 10.3.1 Frame Properties
      2. 10.3.2 Determining a Good Frame Size
    4. 10.4 Displaying Information in a Component
    5. 10.5 Working with 2D Shapes
    6. 10.6 Using Color
    7. 10.7 Using Special Fonts for Text
    8. 10.8 Displaying Images
  17. Chapter 11. Event Handling
    1. 11.1 Basics of Event Handling
      1. 11.1.1 Example: Handling a Button Click
      2. 11.1.2 Specifying Listeners Concisely
      3. 11.1.3 Example: Changing the Look-and-Feel
      4. 11.1.4 Adapter Classes
    2. 11.2 Actions
    3. 11.3 Mouse Events
    4. 11.4 The AWT Event Hierarchy
      1. 11.4.1 Semantic and Low-Level Events
  18. Chapter 12. User Interface Components with Swing
    1. 12.1 Swing and the Model-View-Controller Design Pattern
      1. 12.1.1 Design Patterns
      2. 12.1.2 The Model-View-Controller Pattern
      3. 12.1.3 A Model-View-Controller Analysis of Swing Buttons
    2. 12.2 Introduction to Layout Management
      1. 12.2.1 Border Layout
      2. 12.2.2 Grid Layout
    3. 12.3 Text Input
      1. 12.3.1 Text Fields
      2. 12.3.2 Labels and Labeling Components
      3. 12.3.3 Password Fields
      4. 12.3.4 Text Areas
      5. 12.3.5 Scroll Panes
    4. 12.4 Choice Components
      1. 12.4.1 Checkboxes
      2. 12.4.2 Radio Buttons
      3. 12.4.3 Borders
      4. 12.4.4 Combo Boxes
      5. 12.4.5 Sliders
    5. 12.5 Menus
      1. 12.5.1 Menu Building
      2. 12.5.2 Icons in Menu Items
      3. 12.5.3 Checkbox and Radio Button Menu Items
      4. 12.5.4 Pop-Up Menus
      5. 12.5.5 Keyboard Mnemonics and Accelerators
      6. 12.5.6 Enabling and Disabling Menu Items
      7. 12.5.7 Toolbars
      8. 12.5.8 Tooltips
    6. 12.6 Sophisticated Layout Management
      1. 12.6.1 The Grid Bag Layout
      2. 12.6.2 Group Layout
      3. 12.6.3 Using No Layout Manager
      4. 12.6.4 Custom Layout Managers
      5. 12.6.5 Traversal Order
    7. 12.7 Dialog Boxes
      1. 12.7.1 Option Dialogs
      2. 12.7.2 Creating Dialogs
      3. 12.7.3 Data Exchange
      4. 12.7.4 File Dialogs
      5. 12.7.5 Color Choosers
    8. 12.8 Troubleshooting GUI Programs
      1. 12.8.1 Debugging Tips
      2. 12.8.2 Letting the AWT Robot Do the Work
  19. Chapter 13. Deploying Java Applications
    1. 13.1 JAR Files
      1. 13.1.1 Creating JAR files
      2. 13.1.2 The Manifest
      3. 13.1.3 Executable JAR Files
      4. 13.1.4 Resources
      5. 13.1.5 Sealing
    2. 13.2 Storage of Application Preferences
      1. 13.2.1 Property Maps
      2. 13.2.2 The Preferences API
    3. 13.3 Service Loaders
    4. 13.4 Applets
      1. 13.4.1 A Simple Applet
      2. 13.4.2 The applet HTML Tag and Its Attributes
      3. 13.4.3 Use of Parameters to Pass Information to Applets
      4. 13.4.4 Accessing Image and Audio Files
      5. 13.4.5 The Applet Context
      6. 13.4.6 Inter-Applet Communication
      7. 13.4.7 Displaying Items in the Browser
      8. 13.4.8 The Sandbox
      9. 13.4.9 Signed Code
    5. 13.5 Java Web Start
      1. 13.5.1 Delivering a Java Web Start Application
      2. 13.5.2 The JNLP API
  20. Chapter 14. Concurrency
    1. 14.1 What Are Threads?
      1. 14.1.1 Using Threads to Give Other Tasks a Chance
    2. 14.2 Interrupting Threads
    3. 14.3 Thread States
      1. 14.3.1 New Threads
      2. 14.3.2 Runnable Threads
      3. 14.3.3 Blocked and Waiting Threads
      4. 14.3.4 Terminated Threads
    4. 14.4 Thread Properties
      1. 14.4.1 Thread Priorities
      2. 14.4.2 Daemon Threads
      3. 14.4.3 Handlers for Uncaught Exceptions
    5. 14.5 Synchronization
      1. 14.5.1 An Example of a Race Condition
      2. 14.5.2 The Race Condition Explained
      3. 14.5.3 Lock Objects
      4. 14.5.4 Condition Objects
      5. 14.5.5 The synchronized Keyword
      6. 14.5.6 Synchronized Blocks
      7. 14.5.7 The Monitor Concept
      8. 14.5.8 Volatile Fields
      9. 14.5.9 Final Variables
      10. 14.5.10 Atomics
      11. 14.5.11 Deadlocks
      12. 14.5.12 Thread-Local Variables
      13. 14.5.13 Lock Testing and Timeouts
      14. 14.5.14 Read/Write Locks
      15. 14.5.15 Why the stop and suspend Methods Are Deprecated
    6. 14.6 Blocking Queues
    7. 14.7 Thread-Safe Collections
      1. 14.7.1 Efficient Maps, Sets, and Queues
      2. 14.7.2 Atomic Update of Map Entries
      3. 14.7.3 Bulk Operations on Concurrent Hash Maps
      4. 14.7.4 Concurrent Set Views
      5. 14.7.5 Copy on Write Arrays
      6. 14.7.6 Parallel Array Algorithms
      7. 14.7.7 Older Thread-Safe Collections
    8. 14.8 Callables and Futures
    9. 14.9 Executors
      1. 14.9.1 Thread Pools
      2. 14.9.2 Scheduled Execution
      3. 14.9.3 Controlling Groups of Tasks
      4. 14.9.4 The Fork-Join Framework
      5. 14.9.5 Completable Futures
    10. 14.10 Synchronizers
      1. 14.10.1 Semaphores
      2. 14.10.2 Countdown Latches
      3. 14.10.3 Barriers
      4. 14.10.4 Exchangers
      5. 14.10.5 Synchronous Queues
    11. 14.11 Threads and Swing
      1. 14.11.1 Running Time-Consuming Tasks
      2. 14.11.2 Using the Swing Worker
      3. 14.11.3 The Single-Thread Rule
  21. Appendix A. Java Keywords
  22. Index
  23. Code Snippets

Product information

  • Title: Core Java® Volume I—Fundamentals, Tenth Edition
  • Author(s): Cay S. Horstmann
  • Release date: December 2015
  • Publisher(s): Pearson
  • ISBN: 9780134177335