Cover image for Java Examples in a Nutshell, 3rd Edition

Book description

The author of the best-selling Java in a Nutshell has created an entire book of real-world Java programming examples that you can learn from. If you learn best "by example," this is the book for you. This third edition covers Java 1.4 and contains 193 complete, practical examples: over 21,900 lines of densely commented, professionally written Java code, covering 20 distinct client-side and server-side APIs. It includes new chapters on the Java Sound API and the New I/O API. The chapters on XML and servlets have been rewritten to cover the latest versions of the specifications and to demonstrate best practices for Java 1.4. New and updated examples throughout the book demonstrate many other new Java features and APIs. Java Examples in a Nutshell is a companion volume to Java in a Nutshell, Java Foundation Classes in a Nutshell, and Java Enterprise in a Nutshell. It picks up where those quick references leave off, providing a wealth of examples for both novices and experts. This book doesn't hold your hand; it simply delivers well-commented working examples with succinct explanations to help you learn and explore Java and its APIs. Java Examples in a Nutshell contains examples that demonstrate:

  • Core APIs, including I/O, New I/O, threads, networking, security, serialization, and reflection

  • Desktop APIs, highlighting Swing GUIs, Java 2D graphics, preferences, printing, drag-and-drop, JavaBeans, applets, and sound

  • Enterprise APIs, including JDBC (database access), JAXP (XML parsing and transformation), Servlets 2.4, JSP 2.0 (JavaServer Pages), and RMI

The book begins with introductory examples demonstrating structured and object-oriented programming techniques for new Java programmers. A special index at the end of the book makes it easy to look up examples that use a particular Java class or accomplish a desired task. In between, each chapter includes exercises that challenge readers and suggest further avenues for exploration.

Table of Contents

  1. Java Examples in a Nutshell, 3rd Edition
  2. A Note Regarding Supplemental Files
  3. Preface
    1. New in This Edition
    2. Java Examples Online
    3. Related Books from O'Reilly
    4. Conventions Used in This Book
    5. Request for Comments
    6. Acknowledgments
  4. I. Learning Java
    1. 1. Java Basics
      1. 1.1. Hello World
        1. 1.1.1. Running Hello World
      2. 1.2. FizzBuzz
      3. 1.3. The Fibonacci Series
      4. 1.4. Using Command-Line Arguments
      5. 1.5. Echo in Reverse
      6. 1.6. FizzBuzz Switched
      7. 1.7. Computing Factorials
      8. 1.8. Recursive Factorials
      9. 1.9. Caching Factorials
      10. 1.10. Computing Big Factorials
      11. 1.11. Handling Exceptions
      12. 1.12. Interactive Input
      13. 1.13. Using a StringBuffer
      14. 1.14. Sorting Numbers
      15. 1.15. Computing Primes
      16. 1.16. Exercises
    2. 2. Objects, Classes, and Interfaces
      1. 2.1. A Rectangle Class
      2. 2.2. Testing the Rect Class
      3. 2.3. A Rect Subclass
      4. 2.4. Another Subclass
      5. 2.5. Complex Numbers
      6. 2.6. Computing Statistics
      7. 2.7. An Integer List
      8. 2.8. Tokenizing Text
        1. 2.8.1. The Tokenizer Interface
        2. 2.8.2. The AbstractTokenizer Implementation
        3. 2.8.3. A Concrete CharSequenceTokenizer
      9. 2.9. Exercises
  5. II. Core Java APIs
    1. 3. Input/Output
      1. 3.1. Files and Streams
      2. 3.2. Working with Files
      3. 3.3. Copying File Contents
      4. 3.4. Reading and Displaying Text Files
      5. 3.5. Listing Directory and File Information
      6. 3.6. Compressing Files and Directories
      7. 3.7. Filtering Character Streams
      8. 3.8. Tokenizing a Character Stream
      9. 3.9. Random Access to Files
      10. 3.10. Exercises
    2. 4. Threads
      1. 4.1. Thread Basics
      2. 4.2. Thread-Safe Classes
      3. 4.3. Threads and Thread Groups
      4. 4.4. Deadlock
      5. 4.5. Timers
      6. 4.6. Exercises
    3. 5. Networking
      1. 5.1. Downloading the Contents of a URL
      2. 5.2. Using a URLConnection
      3. 5.3. Sending Email Through a URLConnection
      4. 5.4. A Simple Network Client
      5. 5.5. A Generic Client
      6. 5.6. An HTTP Client
      7. 5.7. A POP Client
      8. 5.8. A Simple Web Server
      9. 5.9. A Proxy Server
      10. 5.10. A Generic Multithreaded Server
      11. 5.11. Sending Datagrams
      12. 5.12. Receiving Datagrams
      13. 5.13. Exercises
    4. 6. New I/O
      1. 6.1. Locking Files
      2. 6.2. Copying Files
      3. 6.3. Regular Expressions and Character Decoding
      4. 6.4. File Copying with Buffers
        1. 6.4.1. Loop Alternatives
      5. 6.5. Advanced Byte-to-Character Conversion
      6. 6.6. Tokenizing Byte Buffers
        1. 6.6.1. Tokenizing Memory Mapped Files
        2. 6.6.2. Tokenizing Channels
      7. 6.7. A Simple HTTP Client
      8. 6.8. The Daytime Service
        1. 6.8.1. Listening for TCP and UDP Connections
        2. 6.8.2. A Daytime Client
      9. 6.9. A Multiplexed Server
      10. 6.10. A Multiplexed Network Client
        1. 6.10.1. Logging in HttpDownloadManager
      11. 6.11. Exercises
    5. 7. Security and Cryptography
      1. 7.1. Running Untrusted Code
      2. 7.2. Loading Untrusted Code
        1. 7.2.1. A Policy for SafeServer
        2. 7.2.2. Testing SafeServer
      3. 7.3. Message Digests and Digital Signatures
      4. 7.4. Cryptography
      5. 7.5. Exercises
    6. 8. Internationalization
      1. 8.1. A Word About Locales
      2. 8.2. Unicode
      3. 8.3. Character Encodings
      4. 8.4. Handling Local Customs
        1. 8.4.1. Setting the Locale
      5. 8.5. Localizing User-Visible Messages
        1. 8.5.1. Working with Resource Bundles
        2. 8.5.2. ResourceBundle Example
      6. 8.6. Formatted Messages
      7. 8.7. Exercises
    7. 9. Reflection
      1. 9.1. Obtaining Class and Member Information
      2. 9.2. Invoking a Named Method
      3. 9.3. Proxy Objects
      4. 9.4. Exercises
    8. 10. Object Serialization
      1. 10.1. Simple Serialization
      2. 10.2. Custom Serialization
      3. 10.3. Externalizable Classes
      4. 10.4. Serialization and Class Versioning
        1. 10.4.1. Advanced Versioning
      5. 10.5. Exercises
  6. III. Desktop Java APIs
    1. 11. Graphical User Interfaces
      1. 11.1. Components
      2. 11.2. Containers
      3. 11.3. Layout Management
        1. 11.3.1. FlowLayout
        2. 11.3.2. GridLayout
        3. 11.3.3. BorderLayout
        4. 11.3.4. Box and BoxLayout
        5. 11.3.5. GridBagLayout
        6. 11.3.6. Hardcoded Layout
        7. 11.3.7. Creating Custom Layout Managers
      4. 11.4. Event Handling
        1. 11.4.1. Handling Mouse Events
        2. 11.4.2. More Mouse Events
        3. 11.4.3. Handling Component Events
        4. 11.4.4. Low-Level Event Handling
        5. 11.4.5. Custom Events and Event Listeners
      5. 11.5. A Complete GUI
      6. 11.6. Actions and Reflection
      7. 11.7. Custom Dialogs
      8. 11.8. An Error Handler Dialog
      9. 11.9. Displaying Tables
      10. 11.10. Displaying Trees
      11. 11.11. A Simple Web Browser
      12. 11.12. Describing GUIs with Properties
        1. 11.12.1. Handling Basic GUI Resources
        2. 11.12.2. An Extension Mechanism for Complex Resources
        3. 11.12.3. Parsing Commands and Actions
        4. 11.12.4. Parsing Menus
      13. 11.13. Themes and the Metal Look-and-Feel
      14. 11.14. Look-and-Feel Preferences
      15. 11.15. The ShowBean Program
      16. 11.16. Exercises
    2. 12. Graphics
      1. 12.1. Graphics Before Java 1.2
        1. 12.1.1. Running the Applet
        2. 12.1.2. Fonts
        3. 12.1.3. Colors
        4. 12.1.4. Simple Animation
      2. 12.2. The Java 2D API
      3. 12.3. Drawing and Filling Shapes
      4. 12.4. Transforms
      5. 12.5. Line Styles with BasicStroke
      6. 12.6. Stroking Lines
      7. 12.7. Filling Shapes with Paint
      8. 12.8. Antialiasing
      9. 12.9. Combining Colors with AlphaComposite
      10. 12.10. Image Processing
      11. 12.11. Image I/O
      12. 12.12. Custom Shapes
      13. 12.13. Custom Strokes
      14. 12.14. Custom Paint
      15. 12.15. Advanced Animation
      16. 12.16. Displaying Graphics Examples
      17. 12.17. Exercises
    3. 13. Printing
      1. 13.1. Printing with the Java 1.1 API
      2. 13.2. Printing with the Java 1.2 API
      3. 13.3. Printing with the Java 1.4 API
      4. 13.4. Printing Multipage Text Documents
      5. 13.5. Advanced Printing with Java 1.4
      6. 13.6. Exercises
    4. 14. Data Transfer
      1. 14.1. Simple Swing Data Transfer
      2. 14.2. A Clock with Drag and Copy Support
      3. 14.3. Data Transfer Architecture
      4. 14.4. Dropping Multiple Datatypes
      5. 14.5. A Transferable Shape
      6. 14.6. Custom Data Transfer
      7. 14.7. Exercises
    5. 15. JavaBeans
      1. 15.1. Bean Basics
      2. 15.2. A Simple Bean
        1. 15.2.1. The Alignment Class
        2. 15.2.2. Packaging a Bean
        3. 15.2.3. Installing a Bean
      3. 15.3. A More Complex Bean
      4. 15.4. Custom Events
      5. 15.5. Specifying Bean Information
      6. 15.6. Defining a Simple Property Editor
      7. 15.7. Defining a Complex Property Editor
      8. 15.8. Defining a Bean Customizer
      9. 15.9. Manipulating Beans
      10. 15.10. Exercises
    6. 16. Applets
      1. 16.1. Introduction to Applets
      2. 16.2. A First Applet
      3. 16.3. A Clock Applet
      4. 16.4. A Timer Applet
      5. 16.5. Applets and the Java 1.0 Event Model
      6. 16.6. Exercises
    7. 17. Sound
      1. 17.1. Ringing the Bell
      2. 17.2. Swing Aural Cues
      3. 17.3. Playing Sounds with AudioClip
        1. 17.3.1. Finding Music Files
      4. 17.4. Playing Sounds with javax.sound
      5. 17.5. Streaming Sounds with javax.sound
      6. 17.6. Synthesizing a MIDI Sequence
      7. 17.7. Real-Time MIDI Sounds
      8. 17.8. Exercises
  7. IV. Enterprise Java APIs
    1. 18. Database Access with SQL
      1. 18.1. Accessing a Database
      2. 18.2. Using Database Metadata
      3. 18.3. Building a Database
      4. 18.4. Using the API Database
      5. 18.5. Atomic Transactions
      6. 18.6. Exercises
    2. 19. XML
      1. 19.1. Parsing with JAXP and SAX
      2. 19.2. Parsing and Manipulating with JAXP and DOM
      3. 19.3. Transforming XML with XSLT
      4. 19.4. An XML Pull Parser
      5. 19.5. Exercises
    3. 20. Servlets and JavaServer Pages
      1. 20.1. Servlet Setup
        1. 20.1.1. The Servlet Container
        2. 20.1.2. Compiling Servlets
        3. 20.1.3. Using Tag Libraries
        4. 20.1.4. Installing and Running Servlets
      2. 20.2. A Hello World Servlet
        1. 20.2.1. Running the HelloNet Servlet
      3. 20.3. Another Simple Servlet
        1. 20.3.1. Testing the Error Handler
      4. 20.4. Servlet Initialization and Persistence: A Counter Servlet
        1. 20.4.1. Running the Counter Servlet
      5. 20.5. Hello JSP
        1. 20.5.1. Displaying a JSP Page
      6. 20.6. Hello JSP2
        1. 20.6.1. Expression Language Syntax
        2. 20.6.2. Displaying JSP Pages that Use Tag Libraries
      7. 20.7. Hello XML
      8. 20.8. The MVC Paradigm for Web Applications
      9. 20.9. ListManager Model Classes
      10. 20.10. ListManager Controller
      11. 20.11. ListManager Views
      12. 20.12. Custom Tags in JSP 2.0
      13. 20.13. Packaging a Web Application
        1. 20.13.1. Web Application Directory Structure
        2. 20.13.2. The web.xml Configuration File
      14. 20.14. Exercises
    4. 21. Remote Method Invocation
      1. 21.1. Remote Banking
      2. 21.2. A Bank Server
      3. 21.3. A Persistent Bank Server
      4. 21.4. A Multiuser Domain
      5. 21.5. Remote MUD Interfaces
      6. 21.6. The MUD Server
      7. 21.7. The MudPlace Class
      8. 21.8. The MudPerson Class
      9. 21.9. A MUD Client
      10. 21.10. Advanced RMI
        1. 21.10.1. Remote Class Loading
        2. 21.10.2. Activation
        3. 21.10.3. CORBA Interoperability with RMI/IIOP
      11. 21.11. Exercises
    5. 22. Example Index
      1. 22.1. Symbols
      2. 22.2. A
      3. 22.3. B
      4. 22.4. C
      5. 22.5. D
      6. 22.6. E
      7. 22.7. F
      8. 22.8. G
      9. 22.9. H
      10. 22.10. I
      11. 22.11. J
      12. 22.12. K
      13. 22.13. L
      14. 22.14. M
      15. 22.15. N
      16. 22.16. O
      17. 22.17. P
      18. 22.18. Q
      19. 22.19. R
      20. 22.20. S
      21. 22.21. T
      22. 22.22. U
      23. 22.23. V
      24. 22.24. W
      25. 22.25. X
      26. 22.26. Y
      27. 22.27. Z
  8. Index
  9. About the Author
  10. Colophon
  11. Copyright