You are previewing Sams Teach Yourself Java™ 6 in 21 Days.
O'Reilly logo
Sams Teach Yourself Java™ 6 in 21 Days

Book Description

“If you get only one Java book, it should be Sams Teach Yourself Java in 21 Days”   —PC Magazine

In just 21 days, you can acquire the knowledge and skills necessary to develop three kinds of programs with Java: applications on your computer, servlets on a web server, and browser-launched Java Web Start applications.

No previous programming experience required. By following the 21 carefully organized lessons in this book, anyone can learn the basics of Java programming.

Learn at your own pace. You can work through each chapter sequentially to make sure you thoroughly understand all of the concepts and methodologies, or you can focus on specific lessons to learn the techniques that interest you most.

Test your knowledge. Each chapter ends with a Workshop section filled with questions, answers, and exercises for further study. There are even certification practice questions.

“Sams Teach Yourself Java is one of the best introductions to hands-on Java programming. If you buy one book on Java, this is the one to buy!”  —Independent Web Review

  • Completely revised, updated, and expanded to cover the latest features of Java 6

  • Learn to develop standalone Java applications, web servlets, JavaServer Pages, and Java Web Start applications

  • Easy-to-understand, practical examples clearly illustrate the fundamentals of Java programming

  • Discover how Swing can help you quickly develop programs with a graphical user interface

  • Find out about JDBC database programming, Internet client/server programming, and XML parsing with the open source XOM class library

  • Covers new features of version 6 such as Java DB, the new relational database that is now a built-in

  • part of Java

Rogers Cadenhead is a web application developer who has written 22 books on Internet-related topics, including Sams Teach Yourself Java 2 in 24 Hours. He maintains this book’s official website at http://www.java21days.com.

Laura Lemay is one of the world’s most popular authors on Web development topics. She is the author of the bestselling Sams Teach Yourself Web Publishing with HTML, as well as Sams Teach Yourself Perl in 21 Days.

CD-ROM includes:

  • Source code for the examples developed in the book

  • Java™ SE Development Kit 6

  • NetBeans™ IDE 5.5

  • Bonus materials

Category:  Java Programming

Covers:  Java 6 Platform Standard Edition

User Level:  Beginning–Intermediate 

Table of Contents

  1. Copyright
    1. Dedication
  2. About the Authors
  3. Acknowledgments
  4. We Want to Hear from You!
  5. Reader Services
  6. Introduction
    1. How This Book Is Organized
    2. Who Should Read This Book
    3. Conventions Used in This Book
  7. 1. The Java Language
    1. 1. Getting Started with Java
      1. The Java Language
        1. History of the Language
        2. Introduction to Java
        3. Selecting a Development Tool
      2. Object-Oriented Programming
      3. Objects and Classes
      4. Attributes and Behavior
        1. Attributes of a Class of Objects
        2. Behavior of a Class of Objects
        3. Creating a Class
        4. Running the Program
      5. Organizing Classes and Class Behavior
        1. Inheritance
        2. Creating a Class Hierarchy
        3. Inheritance in Action
        4. Single and Multiple Inheritance
        5. Interfaces
        6. Packages
      6. Summary
      7. Q&A
      8. Quiz
        1. Questions
        2. Answers
        3. Certification Practice
      9. Exercises
    2. 2. The ABCs of Programming
      1. Statements and Expressions
      2. Variables and Data Types
        1. Creating Variables
        2. Naming Variables
        3. Variable Types
          1. Data Types
          2. Class Types
        4. Assigning Values to Variables
        5. Constants
      3. Comments
      4. Literals
        1. Number Literals
        2. Boolean Literals
        3. Character Literals
        4. String Literals
      5. Expressions and Operators
        1. Arithmetic
        2. More About Assignment
        3. Incrementing and Decrementing
        4. Comparisons
        5. Logical Operators
        6. Operator Precedence
      6. String Arithmetic
      7. Summary
      8. Q&A
      9. Quiz
        1. Questions
        2. Answers
        3. Certification Practice
      10. Exercises
    3. 3. Working with Objects
      1. Creating New Objects
        1. Using new
        2. What new Does
        3. A Note on Memory Management
      2. Accessing and Setting Class and Instance Variables
        1. Getting Values
        2. Changing Values
        3. Class Variables
      3. Calling Methods
        1. Nesting Method Calls
        2. Class Methods
      4. References to Objects
      5. Casting and Converting Objects and Primitive Types
        1. Casting Primitive Types
        2. Casting Objects
        3. Converting Primitive Types to Objects and Vice Versa
      6. Comparing Object Values and Classes
        1. Comparing Objects
        2. Determining the Class of an Object
      7. Summary
      8. Q&A
      9. Quiz
        1. Questions
        2. Answers
        3. Certification Practice
      10. Exercises
    4. 4. Lists, Logic, and Loops
      1. Arrays
        1. Declaring Array Variables
        2. Creating Array Objects
        3. Accessing Array Elements
        4. Changing Array Elements
        5. Multidimensional Arrays
      2. Block Statements
      3. if Conditionals
      4. switch Conditionals
      5. for Loops
      6. while and do Loops
        1. while Loops
        2. do-while Loops
      7. Breaking Out of Loops
        1. Labeled Loops
        2. The Conditional Operator
      8. Summary
      9. Q&A
      10. Quiz
        1. Questions
        2. Answers
        3. Certification Practice
      11. Exercises
    5. 5. Creating Classes and Methods
      1. Defining Classes
      2. Creating Instance and Class Variables
        1. Defining Instance Variables
        2. Class Variables
      3. Creating Methods
        1. Defining Methods
        2. The this Keyword
        3. Variable Scope and Method Definitions
        4. Passing Arguments to Methods
        5. Class Methods
      4. Creating Java Applications
        1. Helper Classes
      5. Java Applications and Command-line Arguments
        1. Passing Arguments to Java Applications
        2. Handling Arguments in Your Java Application
      6. Creating Methods with the Same Name, Different Arguments
      7. Constructor Methods
        1. Basic Constructor Methods
        2. Calling Another Constructor Method
        3. Overloading Constructor Methods
      8. Overriding Methods
        1. Creating Methods That Override Existing Methods
        2. Calling the Original Method
        3. Overriding Constructors
      9. Finalizer Methods
      10. Summary
      11. Q&A
      12. Quiz
        1. Questions
        2. Answers
        3. Certification Practice
      13. Exercises
    6. 6. Packages, Interfaces, and Other Class Features
      1. Modifiers
        1. Access Control for Methods and Variables
          1. Default Access
          2. Private Access
          3. Public Access
          4. Protected Access
          5. Comparing Levels of Access Control
          6. Access Control and Inheritance
          7. Accessor Methods
      2. Static Variables and Methods
      3. Final Classes, Methods, and Variables
        1. Variables
        2. Methods
        3. Classes
      4. Abstract Classes and Methods
      5. Packages
      6. Using Packages
        1. Full Package and Class Names
        2. The import Declaration
        3. Class Name Conflicts
        4. A Note About Classpath and Where Classes Are Located
      7. Creating Your Own Packages
        1. Picking a Package Name
        2. Creating the Folder Structure
        3. Adding a Class to a Package
        4. Packages and Class Access Control
      8. Interfaces
        1. The Problem of Single Inheritance
        2. Interfaces and Classes
        3. Implementing and Using Interfaces
        4. Implementing Multiple Interfaces
        5. Other Uses of Interfaces
      9. Creating and Extending Interfaces
        1. New Interfaces
        2. Methods Inside Interfaces
        3. Extending Interfaces
        4. Creating an Online Storefront
      10. Inner Classes
      11. Summary
      12. Q&A
      13. Quiz
        1. Questions
        2. Answers
        3. Certification Practice
      14. Exercises
    7. 7. Exceptions, Assertions, and Threads
      1. Exceptions
        1. Exception Classes
      2. Managing Exceptions
        1. Exception Consistency Checking
        2. Protecting Code and Catching Exceptions
        3. The finally Clause
      3. Declaring Methods That Might Throw Exceptions
        1. The throws Clause
        2. Which Exceptions Should You Throw?
        3. Passing On Exceptions
        4. throws and Inheritance
      4. Creating and Throwing Your Own Exceptions
        1. Throwing Exceptions
        2. Creating Your Own Exceptions
        3. Combining throws, try, and throw
      5. When and When Not to Use Exceptions
        1. When to Use Exceptions
        2. When Not to Use Exceptions
        3. Bad Style Using Exceptions
      6. Assertions
      7. Threads
        1. Writing a Threaded Program
        2. A Threaded Application
        3. Stopping a Thread
      8. Summary
      9. Q&A
      10. Quiz
        1. Questions
        2. Answers
        3. Certification Practice
      11. Exercises
  8. 2. The Java Class Library
    1. 8. Data Structures
      1. Moving Beyond Arrays
      2. Java Structures
        1. Iterator
        2. Bit Sets
        3. Vectors
        4. Looping Through Data Structures
        5. Stacks
        6. Map
        7. Hash Tables
      3. Generics
      4. Summary
      5. Q&A
      6. Quiz
        1. Questions
        2. Answers
        3. Certification Practice
      7. Exercises
    2. 9. Working with Swing
      1. Creating an Application
        1. Creating an Interface
        2. Developing a Framework
        3. Displaying a Splash Page
        4. Creating a Component
        5. Adding Components to a Container
      2. Working with Components
        1. Image Icons
        2. Labels
        3. Text Fields
        4. Text Areas
        5. Scrolling Panes
        6. Check Boxes and Radio Buttons
        7. Combo Boxes
      3. Lists
      4. Summary
      5. Q&A
      6. Quiz
        1. Questions
        2. Answers
        3. Certification Practice
      7. Exercises
    3. 10. Building a Swing Interface
      1. Swing Features
        1. Setting the Look and Feel
        2. Standard Dialog Boxes
          1. Confirm Dialog Boxes
          2. Input Dialog Boxes
          3. Message Dialog Boxes
          4. Option Dialog Boxes
        3. Using Dialog Boxes
        4. Sliders
        5. Scroll Panes
        6. Toolbars
        7. Progress Bars
        8. Menus
        9. Tabbed Panes
      2. Summary
      3. Q&A
      4. Quiz
        1. Questions
        2. Answers
        3. Certification Practice
      5. Exercises
    4. 11. Arranging Components on a User Interface
      1. Basic Interface Layout
        1. Laying Out an Interface
        2. Flow Layout
        3. Box Layout
        4. Grid Layout
        5. Border Layout
      2. Mixing Layout Managers
      3. Card Layout
        1. Using Card Layout in an Application
      4. Grid Bag Layout
        1. Designing the Grid
        2. Creating the Grid
        3. Cell Padding and Insets
      5. Summary
      6. Q&A
      7. Quiz
        1. Questions
        2. Answers
        3. Certification Practice
      8. Exercises
    5. 12. Responding to User Input
      1. Event Listeners
        1. Setting Up Components
        2. Event-Handling Methods
      2. Working with Methods
        1. Action Events
        2. Focus Events
        3. Item Events
        4. Key Events
        5. Mouse Events
        6. Mouse Motion Events
        7. Window Events
        8. Using Adapter Classes
      3. Summary
      4. Q&A
      5. Quiz
        1. Questions
        2. Answers
        3. Certification Practice
      6. Exercises
    6. 13. Using Color, Fonts, and Graphics
      1. The Graphics2D Class
        1. The Graphics Coordinate System
      2. Drawing Text
        1. Improving Fonts and Graphics with Antialiasing
        2. Finding Information About a Font
      3. Color
        1. Using Color Objects
        2. Testing and Setting the Current Colors
      4. Drawing Lines and Polygons
        1. User and Device Coordinate Spaces
        2. Specifying the Rendering Attributes
          1. Fill Patterns
          2. Setting a Drawing Stroke
        3. Creating Objects to Draw
          1. Lines
          2. Rectangles
          3. Ellipses
          4. Arcs
          5. Polygons
        4. Drawing Objects
          1. Drawing a Map
      5. Summary
      6. Q&A
      7. Quiz
        1. Questions
        2. Answers
        3. Certification Practice
      8. Exercises
    7. 14. Developing Swing Applications
      1. Java Web Start
      2. Using Java Web Start
        1. Creating a JNLP File
        2. Supporting Web Start on a Server
        3. Additional JNLP Elements
          1. Security
          2. Descriptions
          3. Icons
      3. Improving Performance with SwingWorker
      4. Summary
      5. Q&A
      6. Quiz
        1. Questions
        2. Answers
        3. Certification Practice
      7. Exercises
  9. 3. Java Programming
    1. 15. Working with Input and Output
      1. Introduction to Streams
        1. Using a Stream
        2. Filtering a Stream
        3. Handling Exceptions
      2. Byte Streams
        1. File Streams
          1. File Input Streams
          2. File Output Streams
      3. Filtering a Stream
        1. Byte Filters
          1. Buffered Streams
            1. Console Input Streams
          2. Data Streams
      4. Character Streams
        1. Reading Text Files
        2. Writing Text Files
      5. Files and Filename Filters
      6. Summary
      7. Q&A
      8. Quiz
        1. Questions
        2. Answers
        3. Certification Practice
      9. Exercises
    2. 16. Serializing and Examining Objects
      1. Object Serialization
        1. Object Output Streams
        2. Object Input Streams
        3. Transient Variables
        4. Checking an Object’s Serialized Fields
      2. Inspecting Classes and Methods with Reflection
        1. Inspecting and Creating Classes
        2. Working with Each Part of a Class
        3. Inspecting a Class
      3. Summary
      4. Q&A
      5. Quiz
        1. Questions
        2. Answers
        3. Certification Practice
      6. Exercises
    3. 17. Communicating Across the Internet
      1. Networking in Java
        1. Opening a Stream over the Net
        2. Sockets
        3. Socket Servers
          1. Designing a Server Application
        4. Testing the Server
      2. The java.nio Package
        1. Buffers
          1. Byte Buffers
          2. Character Sets
        2. Channels
          1. Network Channels
            1. Nonblocking Socket Clients and Servers
      3. Summary
      4. Q&A
      5. Quiz
        1. Questions
        2. Answers
        3. Certification Practice
      6. Exercises
    4. 18. Accessing Databases with JDBC
      1. Java Database Connectivity
        1. Database Drivers
      2. The JDBC-ODBC Bridge
        1. Connecting to an ODBC Data Source
          1. Retrieving Data from a Database Using SQL
          2. Writing Data to a Database Using SQL
          3. Moving Through Resultsets
        2. JDBC Drivers
      3. Summary
      4. Q&A
      5. Quiz
        1. Questions
        2. Answers
        3. Certification Practice
      6. Exercises
    5. 19. Reading and Writing RSS Feeds
      1. Using XML
      2. Designing an XML Dialect
      3. Processing XML with Java
      4. Processing XML with XOM
        1. Creating an XML Document
        2. Modifying an XML Document
        3. Formatting an XML Document
        4. Evaluating XOM
      5. Summary
      6. Q&A
      7. Quiz
        1. Questions
        2. Answers
        3. Certification Practice
      8. Exercises
    6. 20. XML Web Services
      1. Introduction to XML-RPC
      2. Communicating with XML-RPC
        1. Sending a Request
        2. Responding to a Request
      3. Choosing an XML-RPC Implementation
      4. Using an XML-RPC Web Service
      5. Creating an XML-RPC Web Service
      6. Summary
      7. Q&A
      8. Quiz
        1. Questions
        2. Answers
        3. Certification Practice
      9. Exercises
    7. 21. Writing Java Servlets and Java Server Pages
      1. Using Servlets
      2. Developing Servlets
        1. Using Cookies
        2. Using Sessions
      3. JSP
        1. Writing a JSP Page
          1. Using Expressions
          2. Using Scriptlets
          3. Using Declarations
        2. Creating a Web Application
      4. JSP Standard Tag Library
      5. Summary
      6. Q&A
      7. Quiz
        1. Questions
        2. Answers
        3. Certification Practice
      8. Exercises
  10. Appendixes
    1. A. Using the Java Development Kit
      1. Choosing a Java Development Tool
        1. Installing the Java Development Kit
          1. Windows Installation
      2. Configuring the Java Development Kit
        1. Using a Command-line Interface
        2. Opening Folders in MS-DOS
        3. Creating Folders in MS-DOS
        4. Running Programs in MS-DOS
        5. Correcting Configuration Errors
          1. Setting the PATH on Windows 98 or Me
          2. Setting the Path on Windows NT, 2000, or XP
      3. Using a Text Editor
      4. Creating a Sample Program
        1. Compiling and Running the Program in Windows
      5. Setting Up the CLASSPATH Variable
        1. Setting the CLASSPATH on Windows 98 or Me
        2. Setting the Classpath on Windows NT, XP, 2000 or 2003
        3. Troubleshooting Your Kit Installation
    2. B. Programming with the Java Development Kit
      1. An Overview of the JDK
      2. The java Interpreter
      3. The javac Compiler
      4. The appletviewer Browser
      5. The javadoc Documentation Tool
      6. The jar Java File Archival Tool
      7. The jdb Debugger
        1. Debugging Applications
        2. Debugging Applets
        3. Advanced Debugging Commands
      8. Using System Properties
    3. C. This Book’s Website
  11. What’s on the CD-ROM
    1. Windows Installation Instructions
    2. UNIX and UNIX-like Installation Instructions
  12. Bonus Material on the Companion CD-ROM
    1. Bonus Article: Choosing Java
      1. Java’s Past, Present, and Future
        1. Interactive Web Programming
        2. Java Grew from a Little Oak
        3. Versions of the Language
        4. Java’s Outlook
      2. Why Choose Java
        1. Java Is Object-Oriented
        2. Java Is Easy to Learn
        3. Java Is Platform Neutral
    2. Bonus Chapter: Writing Java Applets
      1. How Applets and Applications Are Different
      2. Applet Security Restrictions
        1. Choosing a Java Version
      3. Creating Applets
        1. Applet Methods
          1. Initialization Method
          2. Start Method
          3. Stop Method
          4. Destroy Method
          5. Paint Method
        2. Writing an Applet
      4. Including an Applet on a Web Page
        1. The Applet Tag
          1. Other Attributes
        2. Loading an Applet
        3. Putting Applets on the Web
      5. Java Archives
      6. Passing Parameters to Applets
      7. Running Applets with Java Web Start
    3. Bonus Chapter: Regular Expressions
      1. Introduction to Pattern Matching
      2. The CharSequence Interface
      3. Using Regular Expressions
        1. Looking for a Match
        2. Splitting Strings with Patterns
      4. Patterns
        1. Matches
    4. Bonus Article: Where to Go from Here: Java Resources
      1. Other Books to Consider
      2. Sun’s Official Java Site
        1. Java Class Documentation
      3. Other Java Websites
        1. This Book’s Official Site
        2. Café au Lait
        3. Workbench
        4. Java Weblogs
        5. InformIT
        6. Java Review Service
        7. JavaWorld Magazine
        8. Gamelan: Earthweb’s Java Directory
      4. Java Newsgroups
      5. Internet Relay Chat
      6. Job Opportunities
  13. Inside Front Cover