You are previewing Core Java™ 2 Volume I - Fundamentals, Seventh Edition.
O'Reilly logo
Core Java™ 2 Volume I - Fundamentals, Seventh Edition

Book Description

Completely revised and up-to-date coverage of

  • Generic programming, restrictions and limitations, type bounds, wilcard types, and generic reflection

  • Swing GUI development, including input validation and other enhancements

  • Exception handling and debugging, including chained exceptions, stack frames, assertions, and logging

  • Streams and files, the new I/O API, memory-mapped files, file locking, and character set encoders/decoders

  • Regular expressions using the powerful java.util.regex package

  • Inner classes, reflection, and dynamic proxies

  • Application packaging and the Preferences API

  • The seventh edition of Core Java™ 2, Volume I, covers the fundamentals of the Java 2 Platform, Standard Edition (J2SE™). A no-nonsense tutorial and reliable reference, this book features thoroughly tested real-world examples. The most important language and library features are demonstrated with deliberately simple sample programs, but they aren’t fake and they don’t cut corners. More importantly, all of the programs have been updated for J2SE 5.0 and should make good starting points for your own code. You won’t find any toy examples here. This is a book for programmers who want to write real code to solve real problems.

    Volume I concentrates on the fundamental concepts of the Java language, along with the basics of user-interface programming and provides detailed coverage of

  • Object-oriented programming

  • Reflection and proxies

  • Interfaces and inner classes

  • The event listener model

  • Graphical user-interface design with the Swing UI toolkit

  • Exception handling

  • Stream input/output and object serialization

  • Generic programming

  • For the same real-world treatment of enterprise features and advanced user-interface programming, look for the forthcoming new edition of Core Java™ 2, Volume II—Advanced Features.

    It includes new sections on metadata and other J2SE 5.0 enhancements along with complete coverage of: Multithreading • Distributed objects • Databases • Advanced GUI components • Native methods • XML Processing • Network programming • Collection classes • Advanced graphics • Internationalization • JavaBeans



    Table of Contents

    1. Copyright
    2. List of Code Examples
    3. Preface
      1. To the Reader
      2. About This Book
      3. Conventions
      4. Sample Code
    4. Acknowledgments
    5. 1. An Introduction to Java
      1. Java as a Programming Platform
      2. The Java “White Paper” Buzzwords
        1. Simple
        2. Object Oriented
        3. Distributed
        4. Robust
        5. Secure
        6. Architecture Neutral
        7. Portable
        8. Interpreted
        9. High Performance
        10. Multithreaded
        11. Dynamic
      3. Java and the Internet
      4. A Short History of Java
      5. Common Misconceptions About Java
    6. 2. The Java Programming Environment
      1. Installing the Java Development Kit
        1. Downloading the JDK
        2. Setting the Execution Path
        3. Installing the Library Source and Documentation
        4. Installing the Core Java Program Examples
        5. Navigating the Java Directories
      2. Choosing a Development Environment
      3. Using the Command-Line Tools
        1. Troubleshooting Hints
      4. Using an Integrated Development Environment
        1. Locating Compilation Errors
      5. Compiling and Running Programs from a Text Editor
      6. Running a Graphical Application
      7. Building and Running Applets
    7. 3. Fundamental Programming Structures in Java
      1. A Simple Java Program
      2. Comments
      3. Data Types
        1. Integers
        2. Floating-Point Types
        3. The char Type
        4. The boolean Type
      4. Variables
        1. Initializing Variables
        2. Constants
      5. Operators
        1. Increment and Decrement Operators
        2. Relational and boolean Operators
        3. Bitwise Operators
        4. Mathematical Functions and Constants
        5. Conversions Between Numeric Types
        6. Casts
        7. Parentheses and Operator Hierarchy
        8. Enumerated Types
      6. Strings
        1. Code Points and Code Units
        2. Substrings
        3. String Editing
        4. Concatenation
        5. Testing Strings for Equality
        6. Reading the On-Line API Documentation
      7. Input and Output
        1. Reading Input
        2. Formatting Output
      8. Control Flow
        1. Block Scope
        2. Conditional Statements
        3. Loops
        4. Determinate Loops
        5. Multiple Selections—The switch Statement
        6. Statements That Break Control Flow
      9. Big Numbers
      10. Arrays
        1. The “for each” Loop
        2. Array Initializers and Anonymous Arrays
        3. Array Copying
        4. Command-Line Parameters
        5. Array Sorting
        6. Multidimensional Arrays
        7. Ragged Arrays
    8. 4. Objects and Classes
      1. Introduction to Object-Oriented Programming
        1. The Vocabulary of OOP
        2. Objects
        3. Relationships Between Classes
        4. OOP Contrasted with Traditional Procedural Programming Techniques
      2. Using Predefined Classes
        1. Objects and Object Variables
        2. The GregorianCalendar Class of the Java Library
        3. Mutator and Accessor Methods
      3. Defining Your Own Classes
        1. An Employee Class
        2. Use of Multiple Source Files
        3. Dissecting the Employee Class
        4. First Steps with Constructors
        5. Implicit and Explicit Parameters
        6. Benefits of Encapsulation
        7. Class-Based Access Privileges
        8. Private Methods
        9. Final Instance Fields
      4. Static Fields and Methods
        1. Static Fields
        2. Constants
        3. Static Methods
        4. Factory Methods
        5. The main Method
      5. Method Parameters
      6. Object Construction
        1. Overloading
        2. Default Field Initialization
        3. Default Constructors
        4. Explicit Field Initialization
        5. Parameter Names
        6. Calling Another Constructor
        7. Initialization Blocks
        8. Object Destruction and the finalize Method
      7. Packages
        1. Class Importation
        2. Static Imports
        3. Addition of a Class into a Package
        4. How the Virtual Machine Locates Classes
        5. Package Scope
      8. Documentation Comments
        1. Comment Insertion
        2. Class Comments
        3. Method Comments
        4. Field Comments
        5. General Comments
        6. Package and Overview Comments
        7. Comment Extraction
      9. Class Design Hints
    9. 5. Inheritance
      1. Classes, Superclasses, and Subclasses
        1. Inheritance Hierarchies
        2. Polymorphism
        3. Dynamic Binding
        4. Preventing Inheritance: Final Classes and Methods
        5. Casting
        6. Abstract Classes
        7. Protected Access
      2. Object: The Cosmic Superclass
        1. The equals Method
        2. Equality Testing and Inheritance
        3. The hashCode Method
        4. The toString Method
      3. Generic Array Lists
        1. Accessing Array List Elements
        2. Compatibility Between Typed and Raw Array Lists
      4. Object Wrappers and Autoboxing
        1. Methods with a Variable Number of Parameters
      5. Reflection
        1. The Class Class
        2. Using Reflection to Analyze the Capabilities of Classes
        3. Using Reflection to Analyze Objects at Run Time
        4. Using Reflection to Write Generic Array Code
        5. Method Pointers!
      6. Enumeration Classes
      7. Design Hints for Inheritance
    10. 6. Interfaces and Inner Classes
      1. Interfaces
        1. Properties of Interfaces
        2. Interfaces and Abstract Classes
      2. Object Cloning
      3. Interfaces and Callbacks
      4. Inner Classes
        1. Use of an Inner Class to Access Object State
        2. Special Syntax Rules for Inner Classes
        3. Are Inner Classes Useful? Actually Necessary? Secure?
        4. Local Inner Classes
        5. Anonymous Inner Classes
        6. Static Inner Classes
      5. Proxies
        1. Properties of Proxy Classes
    11. 7. Graphics Programming
      1. Introducing Swing
      2. Creating a Frame
      3. Positioning a Frame
      4. Displaying Information in a Panel
      5. Working with 2D Shapes
      6. Using Color
        1. Filling Shapes
      7. Using Special Fonts for Text
      8. Doing More with Images
    12. 8. Event Handling
      1. Basics of Event Handling
        1. Example: Handling a Button Click
        2. Becoming Comfortable with Inner Classes
        3. Turning Components into Event Listeners
        4. Example: Changing the Look and Feel
        5. Example: Capturing Window Events
          1. Adapter Classes
      2. The AWT Event Hierarchy
      3. Semantic and Low-Level Events in the AWT
        1. Event Handling Summary
      4. Low-Level Event Types
        1. Keyboard Events
        2. Mouse Events
        3. Focus Events
      5. Actions
      6. Multicasting
      7. Implementing Event Sources
    13. 9. User Interface Components with Swing
      1. The Model-View-Controller Design Pattern
        1. A Model-View-Controller Analysis of Swing Buttons
      2. Introduction to Layout Management
        1. Border Layout
        2. Panels
        3. Grid Layout
      3. Text Input
        1. Text Fields
        2. Labels and Labeling Components
        3. Change Tracking in Text Fields
        4. Password Fields
        5. Formatted Input Fields
          1. Integer Input
          2. Behavior on Loss of Focus
          3. Filters
          4. Verifiers
          5. Other Standard Formatters
          6. Custom Formatters
        6. Text Areas
      4. Choice Components
        1. Checkboxes
        2. Radio Buttons
        3. Borders
        4. Combo Boxes
        5. Sliders
        6. The JSpinner Component
      5. Menus
        1. Menu Building
        2. Icons in Menu Items
        3. Checkbox and Radio Button Menu Items
        4. Pop-Up Menus
        5. Keyboard Mnemonics and Accelerators
        6. Enabling and Disabling Menu Items
        7. Toolbars
        8. Tooltips
      6. Sophisticated Layout Management
        1. Box Layout
          1. Fillers
        2. The Grid Bag Layout
          1. The gridx, gridy, gridwidth, and gridheight Parameters
          2. Weight Fields
          3. The fill and anchor Parameters
          4. Padding
          5. Alternative Method to Specify the gridx, gridy, gridwidth, and gridheight Parameters
          6. A Helper Class to Tame the Grid Bag Constraints
        3. The Spring Layout
        4. Using No Layout Manager
        5. Custom Layout Managers
        6. Traversal Order
      7. Dialog Boxes
        1. Option Dialogs
        2. Creating Dialogs
        3. Data Exchange
        4. File Dialogs
        5. Color Choosers
    14. 10. Deploying Applets and Applications
      1. Applet Basics
        1. A Simple Applet
        2. Applet Viewing
        3. Application Conversion to Applets
        4. Life Cycle of an Applet
        5. Security Basics
        6. Pop-Up Windows in Applets
      2. The Applet HTML Tags and Attributes
        1. Applet Attributes for Positioning
        2. Applet Attributes for Code
        3. Applet Attributes for Java-Challenged Viewers
        4. The object Tag
        5. Use of Parameters to Pass Information to Applets
      3. Multimedia
        1. Encapsulating URLs
        2. Obtaining Multimedia Files
      4. The Applet Context
        1. Inter-Applet Communication
        2. Display of Items in the Browser
        3. A Bookmark Applet
        4. It’s an Applet. It’s an Application. It’s Both!
      5. JAR Files
      6. Application Packaging
        1. The Manifest
        2. Self-Running JAR Files
        3. Resources
        4. Sealing
      7. Java Web Start
        1. The JNLP API
      8. Storage of Application Preferences
        1. Property Maps
        2. System Information
        3. The Preferences API
    15. 11. Exceptions and Debugging
      1. Dealing with Errors
        1. The Classification of Exceptions
        2. Declaring Checked Exceptions
        3. How to Throw an Exception
        4. Creating Exception Classes
      2. Catching Exceptions
        1. Catching Multiple Exceptions
        2. Rethrowing and Chaining Exceptions
        3. The finally Clause
        4. Analyzing Stack Trace Elements
        5. Taking a Final Look at Java Error and Exception Handling
      3. Tips for Using Exceptions
      4. Logging
        1. Basic Logging
        2. Advanced Logging
        3. Changing the Log Manager Configuration
        4. Localization
        5. Handlers
        6. Filters
        7. Formatters
      5. Using Assertions
        1. Assertion Enabling and Disabling
        2. Usage Hints for Assertions
      6. Debugging Techniques
        1. Useful Tricks for Debugging
        2. Using a Console Window
        3. Tracing AWT Events
        4. Letting the AWT Robot Do the Work
      7. Using a Debugger
        1. The JDB Debugger
        2. The Eclipse Debugger
    16. 12. Streams and Files
      1. Streams
        1. Reading and Writing Bytes
      2. The Complete Stream Zoo
        1. Layering Stream Filters
        2. Data Streams
        3. Random-Access File Streams
        4. Text Streams
        5. Character Sets
        6. How to Write Text Output
        7. How to Read Text Input
      3. ZIP File Streams
      4. Use of Streams
        1. Writing Delimited Output
        2. String Tokenizers and Delimited Text
        3. Reading Delimited Input
        4. The StringBuilder Class
        5. Working with Random-Access Streams
      5. Object Streams
        1. Storing Objects of Variable Type
        2. Understanding the Object Serialization File Format
        3. Solving the Problem of Saving Object References
        4. Understanding the Output Format for Object References
        5. Modifying the Default Serialization Mechanism
        6. Serializing Singletons and Typesafe Enumerations
        7. Versioning
        8. Using Serialization for Cloning
      6. File Management
      7. New I/O
        1. Memory-Mapped Files
        2. The Buffer Data Structure
        3. File Locking
      8. Regular Expressions
    17. 13. Generic Programming
      1. Why Generic Programming?
        1. Who Wants to Be a Generic Programmer?
      2. Definition of a Simple Generic Class
      3. Generic Methods
      4. Bounds for Type Variables
      5. Generic Code and the Virtual Machine
        1. Translating Generic Expressions
        2. Translating Generic Methods
        3. Calling Legacy Code
      6. Restrictions and Limitations
        1. Primitive Types
        2. Runtime Type Inquiry
        3. Exceptions
        4. Arrays
        5. Instantiation of Generic Types
        6. Static Contexts
        7. Clashes after Erasure
      7. Inheritance Rules for Generic Types
      8. Wildcard Types
        1. Supertype Bounds for Wildcards
        2. Unbounded Wildcards
        3. Wildcard Capture
      9. Reflection and Generics
        1. Using Class<T> Parameters for Type Matching
        2. Generic Type Information in the Virtual Machine
    18. A. Java Keywords
    19. B. Retrofitting JDK 5.0 Code
      1. Enhanced for Loop
      2. Generic Array Lists
      3. Autoboxing
      4. Variable Parameter Lists
      5. Covariant Return Types
      6. Static Import
      7. Console Input
      8. Formatted Output
      9. Content Pane Delegation
      10. Unicode Code Points
      11. Building Strings