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™ 2 by Example, Second Edition

Book Description

Java 2 by Example, Second Edition gives novice programmers in-depth coverage of both object-oriented programming and Java fundamentals. It starts with an overview of Java, including a survey of development tools beginners should use. The book explains the basics of the Java language, including operators, expressions, statements and more; and Object-Oriented Programming with classes and objects, inheritance, and dynamic methods. The author includes a chapter applying the concepts of OOP to object-oriented analysis and design methods. Later chapters demonstrate organizing data in collections and utilizing Java's built-in mathematical functions. Along the way, readers learn from hundreds of examples explaining every concept. Plus, each chapter ends with a series of review questions to ensure that readers are caught up - with answers provided in an appendix.

Table of Contents

  1. Copyright
    1. Dedication
  2. About the Author
  3. Acknowledgments
  4. Tell Us What You Think!
  5. Introduction
    1. The by Example Series
    2. Who Should Use This Book
    3. This Book's Organization
    4. Conventions Used in This Book
      1. By Example Icons
      2. Syntax
      3. Types, Operators, and Statements
      4. Java Language Specification
    5. Where to Get the Book's Source Code
    6. What's Next
  6. I. Exploring the Language
    1. 1. Introducing Java
      1. What Is Java?
        1. A Basic Definition
        2. A Program Development and Execution Environment
        3. A Brief History
      2. Development Tools
        1. From JDK to SDK
          1. Downloading SDK 1.4
          2. Touring the Windows Version of SDK 1.4
        2. Commercial Development Tools
        3. Freeware Development Tools
      3. Java Programs
        1. From Applets to Applications
        2. Your First Application
          1. Source Code
          2. Compilation
          3. Execution
        3. Application Structure
      4. Java Versus C++
        1. Two Listings of the Same Program
        2. Language Similarities
          1. Reserved Words
          2. Types
          3. Operators
          4. Statements
        3. Language Differences
          1. Reserved Words
          2. Types
          3. Operators
          4. Statements
      5. What's Next?
      6. Reviewing It
      7. Checking It
        1. Multiple Choice
        2. True or False
      8. Applying It
    2. 2. From Unicode to Types
      1. Unicode
      2. Comments
        1. Single-line Comments
        2. Multiline Comments
          1. Documentation Comments
      3. Identifiers
        1. Reserved Words and Keywords
      4. Types
        1. Primitive Types
          1. Primitive Type Conversion
        2. Reference Types
      5. What's Next?
      6. Reviewing It
      7. Checking It
        1. Multiple Choice
        2. True or False
      8. Applying It
    3. 3. From Literals to Expressions
      1. Literals
        1. Boolean Literals
        2. Character Literals
        3. Floating-Point Literals
        4. Integer Literals
        5. Null Literals
        6. String Literals
      2. Variables
        1. Declaration
          1. Simple Variable Declaration
          2. Array Variable Declaration
        2. Initialization
          1. Simple Variable Initialization
          2. Array Variable Initialization
      3. Separators and Operators
        1. Separators
        2. Operators
          1. Classifying Operators: Unary, Binary, and Ternary
          2. Classifying Operators: Prefix, Postfix, and Infix
          3. Additive Operators
          4. Assignment Operators
          5. Bitwise and Logical Operators
          6. Conditional Operators
          7. Equality Operators
          8. Multiplicative Operators
          9. Relational Operators
          10. Shift Operators
          11. Unary Operators
      4. Expressions
      5. What's Next?
      6. Reviewing It
      7. Checking It
        1. Multiple Choice
        2. True or False
      8. Applying It
    4. 4. Statements
      1. Types of Statements
      2. Decision Statements
        1. If
        2. If-Else
          1. The Dangling-Else Problem
          2. Chained If-Else Statements
        3. Switch
      3. Loop and Loop Control Statements
        1. For
        2. While
        3. Do
        4. Break
        5. Continue
      4. Other Statements
        1. Empty
        2. Local Variable Declaration
        3. Expression Statements
        4. Return
        5. Throw
        6. Try
        7. Synchronized
      5. What's Next?
      6. Reviewing It
      7. Checking It
        1. Multiple Choice
        2. True or False
      8. Applying It
    5. 5. Classes and Objects
      1. Classes
      2. Fields
        1. Access Specifiers
        2. Modifiers
        3. Instance Fields
        4. Class Fields
        5. Constants
      3. Methods
        1. Access Specifiers
        2. Modifiers
        3. Instance Methods
        4. Class Methods
        5. Overloading Methods
      4. Objects
        1. Accessing Fields
          1. Shadowing
        2. Calling Methods
          1. Chaining Method Calls
          2. Call-by-Value
          3. Method Call Stack
          4. Recursion
        3. Constructors
        4. Singletons and Enumerated Types
          1. Singletons
          2. Enumerated Types
        5. Objects and Information Hiding
      5. What's Next?
      6. Reviewing It
      7. Checking It
        1. Multiple Choice
        2. True or False
      8. Applying It
    6. 6. Inheritance
      1. What Is Inheritance?
        1. Implementation Inheritance
          1. Calling Superclass Constructors
          2. Overriding Methods
          3. Subclass-Superclass Casting
      2. The Root of All Classes
        1. Class Information
        2. Object Cloning
        3. Object Equality
        4. Finalization
        5. Hash Codes
        6. Notification and Waiting
        7. String Representation
      3. Interfaces
        1. Declaring an Interface
          1. Tagging Interfaces
        2. Implementing an Interface
          1. Implementing Multiple Interfaces
        3. Extending Interfaces
      4. Inheritance Versus Composition
      5. What's Next?
      6. Reviewing It
      7. Checking It
        1. Multiple Choice
        2. True or False
      8. Applying It
    7. 7. Polymorphism
      1. What Is Polymorphism?
      2. Method Binding
        1. A Tale of Rectangles and Squares
        2. Dynamic Method Binding Versus Switch Logic
      3. Abstract Classes
        1. Abstract Classes Versus Interfaces
      4. Runtime Type Information
      5. What's Next?
      6. Reviewing It
      7. Checking It
        1. Multiple Choice
        2. True or False
      8. Applying It
    8. 8. Initializers and Nested Classes
      1. Initializers
        1. Class Initializers
        2. Instance Initializers
        3. Mixing Class and Instance Initializers
        4. Initializers and Inheritance
      2. Garbage Collection
        1. Reachable and Unreachable Objects
        2. Running the Garbage Collector
      3. Finalization
        1. Running Finalizers
        2. Resurrection
      4. Nested Classes
        1. Top-Level Classes
        2. Inner Classes
          1. Instance Inner Classes
          2. Local Inner Classes
          3. Anonymous Inner Classes
      5. What's Next?
      6. Reviewing It
      7. Checking It
        1. Multiple Choice
        2. True or False
      8. Applying It
    9. 9. Exceptions and Exception Handlers
      1. What Are Exceptions?
        1. From Error Codes to Objects
        2. An Exceptional API
        3. Checked and Unchecked Exceptions
          1. Creating Your Own Exception Classes
      2. Throwing Exceptions
        1. The Throw Statement
        2. The Throws Clause
          1. Throws Clauses and Constructors
          2. Throws Clauses and Inheritance
        3. The Try Statement
      3. Catching Exceptions
        1. The Catch Clause
          1. Multiple Catch Clauses
        2. Throwing Exceptions from Catch Clauses
      4. Cleaning Up
        1. The Finally Clause
        2. Throwing Exceptions from Finally Clauses
      5. What's Next?
      6. Reviewing It
      7. Checking It
        1. Multiple Choice
        2. True or False
      8. Applying It
    10. 10. Threads
      1. What Are Threads?
        1. Multithreading with the Thread Class
        2. Multithreading with the Runnable Interface
        3. Basic Thread Operations
          1. Thread Naming
          2. Thread Sleeping
          3. Determining Thread Alive Status
          4. Thread Joining
          5. Thread Enumeration
          6. Thread Debugging
          7. User Threads and Daemon Threads
        4. Timers
      2. Synchronization
        1. Locks and Blocks
          1. Object Locks and Class Locks
        2. Deadlock
        3. Waiting and Notification
          1. Thread Interruption
        4. Volatility
      3. Scheduling
        1. Thread States and Priority
        2. Equal Priority Thread Scheduling
      4. Thread Groups
      5. What's Next?
      6. Reviewing It
      7. Checking It
        1. Multiple Choice
        2. True or False
      8. Applying It
    11. 11. Packages
      1. What Are Packages?
        1. Package Information
      2. The Package Directive
        1. Unique Package Names
      3. The Import Directive
        1. The CLASSPATH Environment Variable
          1. Setting CLASSPATH
          2. Searching for Class Files
      4. Playing with Packages
        1. Archives and Package Information
      5. What's Next?
      6. Reviewing It
      7. Checking It
        1. Multiple Choice
        2. True or False
      8. Applying It
  7. II. Exploring APIs
    1. 12. From Characters to String Tokenizers
      1. Characters
        1. Constructing Characters
        2. Classifying Characters
        3. Converting Characters
      2. Strings
        1. Constructing Strings
        2. Character Arrays and Strings
        3. Comparing Strings
        4. Concatenating Strings
        5. Converting Strings
        6. Extracting Characters
        7. Interning Strings
        8. Searching Strings
        9. String Length
        10. Values to Strings
      3. String Buffers
        1. Constructing String Buffers
        2. Appending Characters
        3. Buffer Capacity
        4. Buffer Length
        5. Deleting Characters
        6. Extracting Characters
        7. Inserting Characters
        8. Replacing Characters
        9. Reversing Characters
      4. String Tokenizers
        1. Constructing String Tokenizers
        2. Acquiring Tokens
      5. What's Next?
      6. Reviewing It
      7. Checking It
        1. Multiple Choice
        2. True or False
      8. Applying It
    2. 13. From Fundamental Data Structures to Collections
      1. Fundamental Data Structures
        1. Arrays
          1. Array Creation
          2. Array Access
          3. Copying an Array
          4. Sorting and Searching Arrays
          5. Two-Dimensional Arrays
          6. Arrays are Objects
        2. Bitsets
        3. Enumerations
        4. Hashtables
        5. Properties
          1. System Properties
        6. Stacks
        7. Vectors
      2. Wrappers
      3. Self-Referential Classes
      4. Collections
        1. Interfaces
          1. The Collection Hierarchy
          2. The Map Hierarchy
        2. Implementations
          1. Implementing Sets and Sorted Sets
          2. Implementing Lists
          3. Implementing Maps and Sorted Maps
        3. Utilities
          1. Empty Collections
          2. Copying and Filling
          3. Enumerations
          4. Maximum and Minimum
          5. Reversal
          6. Singleton and Multiple Copy Collections
          7. Sorting and Searching
          8. Synchronized Collections
          9. Unmodifiable Collections
      5. What's Next?
      6. Reviewing It
      7. Checking It
        1. Multiple Choice
        2. True or False
      8. Applying It
    3. 14. Mathematics
      1. Java and Mathematics
        1. Integer Types
          1. Internal Representation
          2. Mathematics-Based Operators
        2. Floating-Point Types
          1. Internal Representation
          2. Mathematics-Based Operators
        3. Floating-Point Issues
          1. Value Sets and FP-strict
          2. Floating-Point Imperfections
      2. Essential Math Classes
      3. Random Numbers
      4. Big Decimals and Big Integers
        1. Big Decimals
        2. Big Integers
      5. What's Next?
      6. Reviewing It
      7. Checking It
        1. Multiple Choice
        2. True or False
      8. Applying It
    4. 15. Files and Streams
      1. Working with Files
        1. Name and Attribute Operations
          1. Abstract Pathname Methods
          2. File-Manipulation Methods
        2. Sequential-Access Data File Content Operations
        3. Random-Access Data File Content Operations
      2. Working with Streams
        1. An Inventory of Stream Classes
          1. File Stream Classes
          2. Buffered Stream Classes
          3. Data Stream Classes
          4. Piped Stream Classes
          5. Zip Stream Classes
        2. Standard I/O
          1. Standard Input
          2. Standard Output
          3. Standard Error
        3. Processes
      3. Object Serialization
        1. Default Serialization and Deserialization
        2. Custom Serialization and Deserialization
        3. Externalization
      4. Stream Tokenizers
      5. What's Next?
      6. Reviewing It
      7. Checking It
        1. Multiple Choice
        2. True or False
      8. Applying It
  8. III. Appendixes
    1. A. Answers
      1. Answers to Chapter 1
        1. Reviewing It
        2. Checking It
          1. Multiple Choice
          2. True or False
        3. Applying It
          1. Independent Exercise 1
          2. Independent Exercise 2
          3. Independent Exercise 3
      2. Answers to Chapter 2
        1. Reviewing It
        2. Checking It
          1. Multiple Choice
          2. True or False
        3. Applying It
          1. Independent Exercise 1
          2. Independent Exercise 2
          3. Independent Exercise 3
      3. Answers to Chapter 3
        1. Reviewing It
        2. Checking It
          1. Multiple Choice
          2. True or False
        3. Applying It
          1. Independent Exercise 1
          2. Independent Exercise 2
          3. Independent Exercise 3
      4. Answers to Chapter 4
        1. Reviewing It
        2. Checking It
          1. Multiple Choice
          2. True or False
        3. Applying It
          1. Independent Exercise 1
          2. Independent Exercise 2
          3. Independent Exercise 3
          4. Independent Exercise 4
          5. Independent Exercise 5
      5. Answers to Chapter 5
        1. Reviewing It
        2. Checking It
          1. Multiple Choice
          2. True or False
        3. Applying It
          1. Independent Exercise 1
          2. Independent Exercise 2
          3. Independent Exercise 3
          4. Independent Exercise 4
          5. Independent Exercise 5
      6. Answers to Chapter 6
        1. Reviewing It
        2. Checking It
          1. Multiple Choice
          2. True or False
        3. Applying It
          1. Independent Exercise 1
          2. Independent Exercise 2
          3. Independent Exercise 3
          4. Independent Exercise 4
      7. Answers to Chapter 7
        1. Reviewing It
        2. Checking It
          1. Multiple Choice
          2. True or False
        3. Applying It
          1. Independent Exercise 1
          2. Independent Exercise 2
          3. Independent Exercise 3
      8. Answers to Chapter 8
        1. Reviewing It
        2. Checking It
          1. Multiple Choice
          2. True or False
        3. Applying It
          1. Independent Exercise 1
          2. Independent Exercise 2
          3. Independent Exercise 3
      9. Answers to Chapter 9
        1. Reviewing It
        2. Checking It
          1. Multiple Choice
          2. True or False
        3. Applying It
          1. Independent Exercise 1
          2. Independent Exercise 2
          3. Independent Exercise 3
          4. Independent Exercise 4
      10. Answers to Chapter 10
        1. Reviewing It
        2. Checking It
          1. Multiple Choice
          2. True or False
        3. Applying It
          1. Independent Exercise 1
          2. Independent Exercise 2
          3. Independent Exercise 3
          4. Independent Exercise 4
          5. Independent Exercise 5
      11. Answers to Chapter 11
        1. Reviewing It
        2. Checking It
          1. Multiple Choice
          2. True or False
        3. Applying It
          1. Independent Exercise 1
          2. Independent Exercise 2
      12. Answers to Chapter 12
        1. Reviewing It
        2. Checking It
          1. Multiple Choice
          2. True or False
        3. Applying It
          1. Independent Exercise 1
          2. Independent Exercise 2
          3. Independent Exercise 3
          4. Independent Exercise 4
          5. Independent Exercise 5
      13. Answers to Chapter 13
        1. Reviewing It
        2. Checking It
          1. Multiple Choice
          2. True or False
        3. Applying It
          1. Independent Exercise 1
          2. Independent Exercise 2
          3. Independent Exercise 3
          4. Independent Exercise 4
          5. Independent Exercise 5
          6. Independent Exercise 6
      14. Answers to Chapter 14
        1. Reviewing It
        2. Checking It
          1. Multiple Choice
          2. True or False
        3. Applying It
          1. Independent Exercise 1
          2. Independent Exercise 2
          3. Independent Exercise 3
          4. Independent Exercise 4
          5. Independent Exercise 5
          6. Independent Exercise 6
          7. Independent Exercise 7
      15. Answers to Chapter 15
        1. Reviewing It
        2. Checking It
          1. Multiple Choice
          2. True or False
        3. Applying It
          1. Independent Exercise 1
          2. Independent Exercise 2
          3. Independent Exercise 3
          4. Independent Exercise 4
          5. Independent Exercise 5
          6. Independent Exercise 6
          7. Independent Exercise 7
          8. Independent Exercise 8
          9. Independent Exercise 9
          10. Independent Exercise 10
    2. B. Reserved Words
      1. Assertions
    3. C. Operator Precedence
    4. D. Additional Resources
      1. Certification
      2. FAQs, Forums, and Newsgroups
      3. Magazines, Tutorials, and the Java Developer Connection
    5. Glossary