Cover image for Learning Java

Book description

Part of O'Reilly's definitive set of Java documentation, Learning Java introduces the basics of Java, the object-oriented programming language for networked applications from Sun Microsystems. This book provides a broad survey of the Java 2 Standard Edition and contains everything necessary to get up to speed quickly. It covers the essentials of hot topics like Swing and JFC; describes new tools for signing applets and other Java classes; and shows how to write networked clients and servers, servlets, JavaBeans, and state-of-the-art user interfaces. Java started out as a tool for creating animated web pages, but it's proven to be much more. Java is now used for everything from sophisticated web clients to mission-critical enterprise applications. In the future, Java will become the basis for a new generation of distributed software that runs on devices ranging from cell phones to supercomputers. In the practical, hands-on approach characteristic of O'Reilly, Learning Java demonstrates why Java is now the language of choice for building the next generation of computer software. Includes a CD-ROM containing the example code and JBuilder for Windows and Solaris. Learning Java covers:

  • History and principles of Java

  • How to write simple applets and applications

  • How to integrate applets into the World Wide Web

  • Java Fundamental Class (JFC) and Swing Libraries

  • Using threads

  • Using arrays

  • Network programming with sockets

  • Remote Method Invocation

  • Servlets

  • Signing applets

  • Creating a security policy

Table of Contents

  1. Learning Java
    1. Preface
      1. New Developments
      2. Audience
      3. Using This Book
      4. Getting Wired
      5. Conventions Used in This Book
      6. How to Contact Us
      7. Acknowledgments
    2. 1. Yet Another Language?
      1. Enter Java
        1. Java’s Origins
        2. Future Buzz?
      2. A Virtual Machine
      3. Java Compared with Other Languages
      4. Safety of Design
        1. Syntactic Sweet ‘n’ Low
        2. Type Safety and Method Binding
        3. Incremental Development
        4. Dynamic Memory Management
        5. Error Handling
        6. Multithreading
        7. Scalability
      5. Safety of Implementation
        1. The Verifier
        2. Class Loaders
        3. Security Managers
      6. Application and User-Level Security
        1. Signing Classes
      7. Java and the World Wide Web
        1. Applets
        2. New Kinds of Media
        3. New Software Development Models
      8. Java as a General Application Language
      9. A Java Road Map
        1. The Past: Java 1.0 and Java 1.1
        2. The Present: Java 2
        3. The Future
        4. Availability
    3. 2. A First Application
      1. HelloJava1
        1. Classes
        2. The main( ) Method
        3. Classes and Objects
        4. Variables and Class Types
        5. Inheritance
        6. The JComponent Class
        7. Relationships and Finger Pointing
        8. Packages
        9. The paintComponent( ) Method
      2. HelloJava2: The Sequel
        1. The import Statement
        2. Instance Variables
        3. Constructors
        4. Events
        5. The repaint( ) Method
        6. Interfaces
      3. HelloJava3: The Button Strikes!
        1. Method Overloading
        2. Garbage Collection
        3. Components
        4. Containers
        5. Layout
        6. Subclassing and Subtypes
        7. More Events and Interfaces
        8. Color Commentary
        9. Static Members
        10. Arrays
        11. Using Color Methods
      4. HelloJava4: Netscape’s Revenge
        1. Threads
        2. The Thread Class
        3. The Runnable Interface
        4. Starting the Thread
        5. Running Code in the Thread
        6. Exceptions
        7. A Word About Synchronization
    4. 3. Tools of the Trade
      1. The Java Interpreter
      2. Policy Files
        1. The Default Security Manager
        2. The policytool Utility
        3. Using a Policy File with the Default Security Manager
      3. The Class Path
      4. The Java Compiler
      5. Java Archive (JAR) Files
        1. File Compression
        2. The jar Utility
          1. JAR manifests
    5. 4. The Java Language
      1. Text Encoding
      2. Comments
        1. Javadoc Comments
      3. Types
        1. Primitive Types
          1. Floating-point precision
          2. Variable declaration and initialization
          3. Integer literals
          4. Floating-point literals
          5. Character literals
        2. Reference Types
        3. A Word About Strings
      4. Statements and Expressions
        1. Statements
          1. Unreachable statements
        2. Expressions
          1. Operators
          2. Assignment
          3. The null value
          4. Variable access
          5. Method invocation
          6. Object creation
          7. The instanceof operator
      5. Exceptions
        1. Exceptions and Error Classes
        2. Exception Handling
        3. Bubbling Up
        4. Checked and Unchecked Exceptions
        5. Throwing Exceptions
          1. Re-throwing exceptions
        6. Try Creep
        7. The finally Clause
        8. Performance Issues
      6. Arrays
        1. Array Types
        2. Array Creation and Initialization
        3. Using Arrays
        4. Anonymous Arrays
        5. Multidimensional Arrays
        6. Inside Arrays
    6. 5. Objects in Java
      1. Classes
        1. Accessing Fields and Methods
        2. Static Members
      2. Methods
        1. Local Variables
        2. Shadowing
          1. The “this” reference
        3. Static Methods
        4. Initializing Local Variables
        5. Argument Passing and References
        6. Method Overloading
      3. Object Creation
        1. Constructors
        2. Working with Overloaded Constructors
        3. Static and Nonstatic Code Blocks
      4. Object Destruction
        1. Garbage Collection
        2. Finalization
    7. 6. Relationships Among Classes
      1. Subclassing and Inheritance
        1. Shadowed Variables
        2. Overriding Methods
          1. Overridden methods and dynamic binding
          2. Static method binding
          3. Dynamic method selection and performance
          4. Compiler optimizations
          5. Method selection revisited
          6. Exceptions and overridden methods
        3. Special References: this and super
        4. Casting
        5. Using Superclass Constructors
        6. Full Disclosure: Constructors and Initialization
        7. Abstract Methods and Classes
      2. Interfaces
        1. Interfaces as Callbacks
        2. Interface Variables
          1. Empty interfaces
        3. Subinterfaces
      3. Packages and Compilation Units
        1. Compilation Units
        2. Package Names
        3. Class Visibility
        4. Importing Classes
        5. The Unnamed Package
      4. Visibility of Variables and Methods
        1. Basic Access Modifiers
        2. Subclasses and Visibility
        3. Interfaces and Visibility
      5. Arrays and the Class Hierarchy
      6. Inner Classes
        1. Inner Classes Within Methods
          1. Limitations on inner classes
          2. Static inner classes
          3. Anonymous inner classes
          4. Scoping of the “this” reference
          5. How do inner classes really work?
          6. Security implications
    8. 7. Working with Objects and Classes
      1. The Object Class
        1. Equality and Equivalence
        2. Hashcodes
        3. Cloning Objects
      2. The Class Class
      3. Reflection
        1. Security
        2. Accessing Fields
        3. Accessing Methods
        4. Accessing Constructors
        5. What About Arrays?
        6. Dynamic Interface Adapters
        7. What Is Reflection Good for?
          1. The BeanShell application
    9. 8. Threads
      1. Introducing Threads
        1. The Thread Class and the Runnable Interface
          1. Creating and starting threads
          2. A natural-born thread
          3. Using an adapter
        2. Controlling Threads
          1. Deprecated methods
          2. The sleep( ) method
          3. The join( ) method
        3. A Thread’s Life
      2. Threads in Applets
      3. Synchronization
        1. Serializing Access to Methods
          1. Accessing instance variables
        2. The wait() and notify( ) Methods
        3. Passing Messages
      4. Scheduling and Priority
        1. Time-Slicing
        2. Priorities
        3. User-Controlled Time-Slicing
        4. Yielding
        5. Native Threads
      5. Thread Groups
        1. Working with the ThreadGroup Class
    10. 9. Basic Utility Classes
      1. Strings
        1. String Constructors
        2. Strings from Things
        3. Things from Strings
        4. Comparisons
          1. The Collator class
        5. Searching
        6. Editing
        7. String Method Summary
        8. The java.lang.StringBuffer Class
        9. The java.util.StringTokenizer Class
      2. Math Utilities
        1. The java.lang.Math Class
        2. The java.math Class
        3. Wrappers for Primitive Types
        4. Random Numbers
      3. Dates
      4. Timers
      5. Collections
        1. The Collection Interface
        2. Iterators
        3. Collection Flavors
        4. The Map Interface
        5. Implementations
          1. Hashcodes and key values
        6. Slam Dunking with Collections
        7. Sorting for Free
        8. A Thrilling Example
        9. Thread Safety and Iterators
        10. WeakHashMap: An Interesting Variation
      6. Properties
        1. Default Values
        2. Loading and Storing
        3. System Properties
        4. Observers and Observables
      7. The Security Manager
      8. Internationalization
        1. The java.util.Locale Class
        2. Resource Bundles
        3. The java.text Class
    11. 10. Input/Output Facilities
      1. Streams
        1. Terminal I/O
        2. Character Streams
        3. Stream Wrappers
          1. Data streams
          2. Buffered streams
          3. Print streams
        4. Pipes
        5. Strings to Streams and Back
        6. The rot13InputStream Class
      2. Files
        1. The java.io.File Class
          1. File constructors
          2. Path localization
          3. File operations
        2. File Streams
        3. The java.io.RandomAccessFile Class
        4. Applets and Files
        5. Loading Application Resources
      3. Serialization
      4. Data Compression
        1. Compressing Data
        2. Decompressing Data
    12. 11. Network Programming with Sockets and RMI
      1. Sockets
        1. Clients and Servers
          1. Clients
          2. Servers
          3. Sockets and security
        2. The DateAtHost Client
        3. The TinyHttpd Server
          1. Do French web servers speak French?
          2. Taming the daemon
          3. Room for improvement
        4. Socket Options
          1. SO_TIMEOUT
          2. TCP_NODELAY
          3. SO_LINGER
          4. TCP_KEEPALIVE
          5. “Half Close”
        5. Proxies and Firewalls
      2. Datagram Sockets
        1. The HeartBeat Applet
          1. The HeartBeat applet code
          2. The Pulse server code
      3. Simple Serialized Object Protocols
        1. A Simple Object-Based Server
          1. Limitations
      4. Remote Method Invocation (RMI)
        1. Remote and Non-Remote Objects
          1. Stubs and skeletons
          2. Remote interfaces
          3. The UnicastRemoteObject class
          4. The RMI registry
        2. An RMI Example
          1. Dynamic class loading
          2. Passing remote object references
        3. RMI Object Activation
        4. RMI and CORBA
    13. 12. Programming for the Web
      1. Uniform Resource Locators (URLs)
      2. The URL Class
        1. Stream Data
        2. Getting the Content as an Object
      3. Web Browsers and Handlers
        1. Other Handler Frameworks
        2. Writing Content and Protocol Handlers
      4. Talking to CGI Programs and Servlets
        1. Using the GET Method
        2. Using the POST Method
        3. SSL and Secure Web Communications
      5. Implementing Servlets
        1. Why Servlets?
        2. The Servlet Life Cycle
        3. HTTP (Web) Servlets
        4. The HelloClient Servlet
        5. Content Types
        6. Servlet Parameters
          1. GET, POST, and the “extra path”
          2. Which one to use?
        7. The ShowParameters Servlet
        8. User Session Management
          1. The ShowSession servlet
          2. The ShoppingCart servlet
        9. Cookies
    14. 13. Swing
      1. Components
        1. Peers
        2. Why the Move from AWT to Swing?
        3. The Model/View/Controller Framework
        4. Painting
        5. Enabling and Disabling Components
        6. Focus, Please
        7. Other Component Methods
      2. Containers
        1. Layout Managers
        2. Insets
        3. Z-Ordering (Stacking Components)
        4. The revalidate( ) and doLayout( ) Methods
        5. Managing Components
        6. Listening for Components
        7. Windows and Frames
        8. Other Methods for Controlling Frames
        9. Using Content Panes
      3. Events
        1. Event Receivers and Listener Interfaces
        2. Event Sources
        3. Event Delivery
        4. Event Types
        5. The java.awt.event.InputEvent Class
        6. Mouse and Key Modifiers on InputEvents
      4. Event Summary
        1. Adapter Classes
        2. Dummy Adapters
        3. Old-Style and New-Style Event Processing
          1. processEvent( )
          2. Enabling new-style events explicitly
      5. Multithreading in Swing
    15. 14. Using Swing Components
      1. Buttons and Labels
      2. Checkboxes and Radio Buttons
      3. Lists and Combo Boxes
      4. Borders
      5. Menus
      6. The PopupMenu Class
      7. The JScrollPane Class
      8. The JSplitPane Class
      9. The JTabbedPane Class
      10. Scrollbars and Sliders
      11. Dialogs
        1. File Selection Dialog
        2. The Color Chooser
    16. 15. More Swing Components
      1. Text Components
        1. The TextEntryBox Application
        2. Say the Magic Word
        3. Sharing a Data Model
        4. HTML and RTF for Free
        5. Managing Text Yourself
      2. Trees
        1. Nodes and Models
        2. Save a Tree
        3. Tree Events
        4. A Complete Example
      3. Tables
        1. A First Stab: Freeloading
        2. Round Two: Creating a Table Model
        3. Round Three: A Simple Spreadsheet
      4. Desktops
      5. Pluggable Look-and-Feel
      6. Creating Custom Components
        1. Generating Events
        2. A Dial Component
        3. Model and View Separation
    17. 16. Layout Managers
      1. FlowLayout
      2. GridLayout
      3. BorderLayout
      4. BoxLayout
      5. CardLayout
      6. GridBagLayout
        1. The GridBagConstraints Class
        2. Grid Coordinates
        3. The fill Constraint
        4. Spanning Rows and Columns
        5. Weighting
          1. Calculating the weights of rows and columns
        6. Anchoring
        7. Padding and Insets
        8. Relative Positioning
        9. Composite Layouts
      7. Nonstandard Layout Managers
      8. Absolute Positioning
    18. 17. Drawing with the 2D API
      1. The Big Picture
      2. The Rendering Pipeline
      3. A Quick Tour of Java 2D
        1. Filling Shapes
        2. Drawing Shape Outlines
        3. Convenience Methods
        4. Drawing Text
        5. Drawing Images
        6. Go Crazy
        7. The Whole Iguana
      4. Filling Shapes
        1. Solid Colors
        2. Color Gradients
        3. Textures
        4. Desktop Colors
      5. Stroking Shape Outlines
      6. Using Fonts
        1. Font Metrics
      7. Displaying Images
        1. The Image Class
        2. Image Observers
        3. Scaling and Size
      8. Using Drawing Techniques
        1. Limited Redrawing
        2. Double Buffering
        3. Offscreen Drawing
      9. Printing
    19. 18. Working with Images and Other Media
      1. Implementing an ImageObserver
      2. Using a MediaTracker
      3. Producing Image Data
        1. Drawing Animations
        2. BufferedImage Anatomy
        3. Color Models
        4. Creating an Image
        5. Updating a BufferedImage
      4. Filtering Image Data
        1. How ImageProcessor Works
        2. Converting an Image to a BufferedImage
        3. Using the RescaleOp Class
        4. Using the AffineTransformOp Class
      5. Working with Audio
      6. Working with Movies
    20. 19. Java Beans
      1. What’s a Bean?
        1. How Big Is a Bean?
        2. The BeanBox Application
          1. Running the BeanBox application
        3. Properties and Customizers
        4. Events Hookups and Adapters
        5. Bound Properties
        6. Constrained Properties
      2. Building Beans
        1. Creating a Component with Bindable Properties
          1. Design patterns for properties
        2. A (Slightly) More Realistic Example
          1. A Bean for displaying text
          2. A Bean for validating numeric data
          3. An invisible multiplier
          4. Putting them together
        3. Customizing with BeanInfo
          1. Getting Properties information
          2. Getting Events information
          3. Supplying icons
          4. Creating customizers and property editors
      3. Hand-Coding with Beans
        1. Bean Instantiation and Type Management
        2. Working with Serialized Beans
      4. Putting Reflection to Work
        1. Safety Implications
        2. Runtime Event Hookups with Reflection
      5. BeanContext and BeanContextServices
      6. The Java Activation Framework
      7. Enterprise JavaBeans
    21. 20. Applets
      1. The JApplet Class
        1. Applet Control
        2. The Applet Security Sandbox
          1. Trusted applets
        3. Getting Applet Resources
          1. Applet parameters
          2. Applet resources
          3. Driving the browser
          4. Inter-applet communication
          5. Applets versus standalone applications
      2. The <APPLET> Tag
        1. Attributes
        2. Parameters
        3. ¿Habla Applet?
        4. The Complete <APPLET> Tag
        5. Loading Class Files
        6. Packages
        7. Viewing Applets
      3. Using the Java Plug-in
        1. What Is the Java Plug-in?
        2. Messy Tags
        3. Viewing Plug-in Applets
      4. Using Digital Signatures
        1. Certificates
          1. Certificate authority (CA) certificates
          2. Site certificates
          3. User (signer) certificates
        2. Signed JAR Files: The keytool and jarsigner Utilities
          1. What about Netscape and Internet Explorer?
          2. The TestWrite example
        3. Keystores, Keys, and Certificates
          1. Public and private keys
          2. Certificates
          3. Signing JARs
        4. Where We’ve Been
    22. 21. Glossary
    23. A. Content and Protocol Handlers
      1. Writing a Content Handler
        1. Locating Content Handlers
        2. The application/x-tar Handler
          1. The ContentHandler class
          2. The URLConnection
          3. Constructing the object
          4. Using our new handler
      2. Writing a Protocol Handler
        1. Locating Protocol Handlers
        2. URLs, Stream Handlers, and Connections
        3. The crypt Handler
          1. The Encryption class
          2. The URLStreamHandler
          3. The URLConnection
          4. Trying it out
    24. B. BeanShell: Simple Java Scripting
      1. Running BeanShell
      2. Java Statements and Expressions
      3. BeanShell Commands
      4. Scripted Methods and Objects
      5. Learning More . . .
    25. Index
    26. Colophon