JAVA Developer's Guide
JAVA Developer's Guide
by Jamie Jaworski
C O N T E N T S
Introduction
Chapter 1 The Java Phenomenon
Chapter 2 Java Overview
Chapter 3 Using the Java Developer's Kit
- Overview
- The Compiler
- The Interpreter
- The Debugger
- The Disassembler
- The Applet Viewer
- Automating Software Documentation
- Header File Generation
- Running the Demo Programs
- Summary
Chapter 4 First Programs: Hello World! to BlackJack
Chapter 5 Classes and Objects
- Object-Oriented Programming Concepts
- Java Classes
- Class Syntax
- The Point Class
- Class Modifiers
- Extending Superclasses
- Adding Body to Classes
- The CGrid Class
- The CGObject Class
- The PrintCGrid Class
- The BorderedPrintCGrid Class
- The CGPoint Class
- The CGBox Class
- The CGText Class
- The KeyboardInput Class
- The CDrawApp Program
- Running CDrawApp
- CDrawApp's Implementation of Object-Oriented Concepts
- Summary
Chapter 6 Interfaces
- The Purpose of Java Interfaces
- The Benefits of Interfaces
- Declaring Interfaces
- Implementing Interfaces
- The CDrawApp Interface Example
- Using Interfaces as Abstract Types
- Interface Constants
- Extending Interfaces
- Combining Interfaces
- Summary
Chapter 7 Exceptions
- Eliminating Software Errors
- Error Processing and Exceptions
- Throwing Exceptions
- Declaring Exceptions
- Declare or Catch?
- Using the try Statement
- Catching Exceptions
- Nested Exception Handling
- Rethrowing Exceptions
- Summary
Chapter 8 Multithreading
- Understanding Multithreading
- How Java Supports Multithreading
- Thread States
- Thread Priority and Scheduling
- Synchronization
- Daemon Threads
- Thread Groups
- Summary
Chapter 9 Using the Debugger
Chapter 10 Automating Software Documentation
- How javadoc Works
- Using javadoc
- Placing Doc Comments
- Using javadoc Tags
- Embedding Standard HTML
- Summary
Chapter 11 Language Summary
- The package Statement
- The import Statement
- Comments
- Identifiers
- Reserved Words
- Primitive Data Types and Literal Values
- Class Declarations
- Variable Declarations
- Constructor Declarations
- Access Method Declarations
- Static Initializers
- Interfaces
- Blocks and Block Bodies
- Local Variable Declarations
- Statements
- Empty Statement
- Block Statement
- Method Invocation
- Allocation Statements
- Assignment Statements
- The if Statement
- Statement Labels
- The switch Statement
- The break Statement
- The for Statement
- The while Statement
- The do Statement
- The continue Statement
- The synchronized Statement
- The try Statement
- The return Statement
- Operators
- Summary
Chapter 12 Portable Software and the java.lang Package
- The Object and Class Classes
- The ClassLoader, SecurityManager, and Runtime Classes
- The System Class
- Wrapped Classes
- The Math Class
- The String and StringBuffer Classes
- Threads and Processes
- The Compiler Class
- Exceptions and Errors
- Summary
Chapter 13 Stream-Based Input/Output and the java.io Package
- Streams
- The java.io Class Hierarchy
- The InputStream Class
- The OutputStream Class
- Byte Array I/O
- File I/O
- The SequenceInputStream Class
- Filtered I/O
- The RandomAccessFile Class
- The StreamTokenizer Class
- Summary
Chapter 14 Useful Tools in the java.util Package
- The Date Class
- The Random Class
- The Enumeration Interface
- The Vector Class
- The Stack Class
- The BitSet Class
- The Dictionary, Hashtable, and Properties Classes
- The StringTokenizer Class
- Observer and Observable
- Summary
Chapter 15 Window Programming with the java.awt Package
- Window Programming Classes
- Components and Containers
- Constructing Menus
- Organizing Windows
- Handling Events
- Working with Images
- Geometrical Objects
- Using Fonts
- Using the Toolkit
- Summary
Chapter 16 Web Programming With the java.applet Package
- Applets and the World Wide Web
- The Life Cycle of an Applet
- Responding to Events
- Using Window Components
- Adding Audio and Animation
- Summary
Chapter 17 Network Programming with the java.net Package
- The Internet Protocol Suite
- Client/Server Computing and the Internet
- Overview of java.net
- The InetAddress Class
- The Socket Class
- The ServerSocket Class
- The DatagramSocket Class
- The DatagramPacket Class
- The SocketImpl Class and the SocketImplFactory Interface
- Web-Related Classes
- The ContentHandler and ContentHandlerFactory Classes
- The URLStreamHandler Class and the URLStreamHandlerFactory Interface
- Summary
Chapter 18 Opening Windows
Chapter 19 Organizing Window Programs
- Designing Window Programs
- Opening and Closing Windows
- Using Layouts
- Connecting Code to Events
- The Window Sampler Program
- Summary
Chapter 20 Menus, Buttons, and Dialong Boxes
Chapter 21 Checkboxes, Choices, and Lists
- Using Checkboxes
- Working with Radio Buttons
- The CheckboxApp Program
- Making Choices
- Selecting from Lists
- The ChoiceListApp Program
- Summary
Chapter 22 Text and Fonts
Chapter 23 The Canvas
- The Canvas and Graphics Classes
- Displaying Bitmapped Images
- Drawing and Painting
- Combining Graphics and Text
- The Image-Processing Classes of java.awt.image
- Summary
Chapter 24 Scrollbars
Chapter 25 Using Animation
- Animation Basics
- A Simple Animation
- A Graphics Animation
- Improving Animation Display Qualities
- An Updated Graphics Animation
- Summary
Chapter 26 Client Programs
- Types of Clients
- Client Responsibilities
- A Simple Telnet Client
- A Mail Client
- The Web Fetcher Program
- Summary
Chapter 27 Server Programs
Chapter 28 Content Handlers
- Using Content Handlers
- Multipurpose Internet Mail Extensions (MIME)
- Developing a Content Handler
- A Simple Content Handler
- Summary
Chapter 29 Protocol Handlers
Chapter 30 Sample Applets
Chapter 31 Developing Applets
- How Applets Work
- Using Applets as Inline Viewers
- Using Applets as Network Clients
- Optimizing Applets
- Learning from Online Examples
- Summary
Chapter 32 Integrating Applets into Distributed Applications
- Architecture for Distributed Applications
- OMG and CORBA
- Interfacing with CGI Programs
- Connecting Applets with CGI Programs
- Summary
Chapter 33 Working with JavaScript
- JavaScript and Java
- How JavaScript Works
- The Relationship Between HTML and Scripts
- Embedding JavaScript
- Combining Scripts with Applets
- Summary
Chapter 34 Sample Scripts
Chapter 35 JavaScript Reference
- JavaScript Objects
- Properties and Methods
- Handling Events
- Arrays
- Operators and Expressions
- Statements
- Summary
Chapter 36 The Java Source Code
- Obtaining the Java Source Code
- Platform Differences
- Platform Benchmarks
- Windows 95/NT Overview
- Summary
Chapter 37 The Java Virtual Machine
- Overview
- Structure of .class Files
- Virtual Machine Architecture
- Instruction Set
- Pushing Constants onto the Stack
- Pushing Local Variables onto the Stack
- Storing Stack Values into Local Variables
- Managing Arrays
- Stack Instructions
- Arithmetic Instructions
- Logical Instructions
- Conversion Operations
- Control Transfer Instructions
- Function Return Instructions
- Table Jumping Instructions
- Manipulating Object Fields
- Method Invocation
- Exception Handling
- Object Utility Operations
- Monitors
- The breakpoint Instruction
- Summary
Chapter 38 Creating Native Methods
Chapter 39 Java Security
Chapter 40 Java Platforms and Extensions
- Java-Enabled Browsers
- Borland's Latte
- Symantec's Café
- Database Extensions
- VRML Extensions with Iced Java/Liquid Reality
- Summary
Appendix A The Jawa API Quick Reference
Appendix B Differences Between java and C++
- Program Structure
- Program Development
- Compilation and Execution
- The Preprocessor and Compiler Directives
- The Java API Versus C++ Libraries
- Using CLASSPATH
- Language Syntax
Appendix C Moving C/C++ Legacy Code to Java
- Why Move to Java?
- Reasons Against Moving to Java
- Transition Approaches and Issues
- Translation Approaches and Issues
Credits
Copyright © 1996 by Jamie Jaworski
FIRST EDITION
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained herein. For information, address Sams.net Publishing, 201 W. 103rd St., Indianapolis, IN 46290. International Standard Book Number: 1-57521-069-X
HTML conversion by :
M/s. LeafWriters (India) Pvt. Ltd.
Website : http://leaf.stpn.soft.net
e-mail :
leafwriters@leaf.stpn.soft.net
Trademarks
All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams.net Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Java is a trademark of Sun Microsystems, Inc.
President, Sams Publishing | Richard K. Swadley |
Publishing Manager | Mark Taber |
Managing Editor | Cindy Morrow |
Marketing Manager | John Pierce |
Assistant Marketing Manager | Kristina Perry |
Acquisitions Editor | Christopher Denny | Development Editor | L. Angelique Brittingham |
Software Development Specialist | Merle Newlon | Production Editor | Kitty Wilson |
Copy Editor | Kimberly K. Hannel | Technical Reviewer | Christopher M. Stone |
Editorial Coordinator | Bill Whitmer | Technical Edit Coordinator | Lynette Quinn |
Formatter | Frank Sinclair | Editorial Assistants | Carol Ackerman, Andi Richter, Rhonda Tinch-Mize, |
Cover Designer | Tim Amrhein | Book Designer | Alyssa Yesh |
Copy Writer | Peter Fuller | Production Team Supervisor | Brad Chinn |
Production | Mary Ann Abramson, Steve Adams, Georgiana Briggs, Mona Brown, Michael Brumitt, Charlotte Clapp, Jeanne Clark, Bruce Clingaman, Michael Dietsch, Sonja Hart, Jason Hand, Michael Henry, Ayanna Lacey, Clint Lahnen, Paula Lowell, Donna Martin, Steph Mineart, Dana Rhodes, Erich Richter, Craig Small, SA Springer, Mark Walchle |
Acknowledgments
I'd like to thank everyone who helped to see this book to completion. In particular, I'd like to thank George Stones for introducing me to Java, Margo Maley for making the book possible, and everyone at Sams.net for their great support. I'd also like to thank my co-author, Cary Jardin, for helping me finish the book closer to the scheduled deadline.
About the Author
James Jaworski develops advanced systems for the United States Department of Defense. He has used Java in several research and development projects, including a terrain analysis program and a genetic algorithm demonstration.
Introduction
Never before has a new programming language received so much attention and become so popular so quickly. In less than a year, Java evolved from experimental Alpha and Beta versions to its initial 1.0 release. Along the way, it took the Web by storm and became its adopted programming language. The Java phenomenon has captivated the imaginations of Web programmers and content developers and is leading the way toward the next era of Internet application development.
Java's appeal lies in its simplicity, its familiarity, and the careful selection of programming features that it includes and excludes. Java was not designed by a government committee or by a clique of academics. It shares the spirit of its birth with C more than any syntactical similarities. It is a programming language that was designed by programmers for programmers.
This book shows you how to program in Java. It covers all the details of the language, provides you with plenty of programming examples, and most importantly, arms you with the mindset needed to write Java code in a manner that is simple, efficient, and true to the nature of the language.
Who Should Read This Book
If you want someone to teach you how to program in Java, this book is for you. You will learn how to develop standalone Java programs, Java applets, and JavaScript applets. You will learn how to program window GUI controls, work with network sockets, and use stream-based input and output. You will learn to develop object-oriented programs and explore the breadth and depth of the Java application program interface. If you want to become a Java programmer, this book will show you how.
This book is for programmers and those who aspire to become Java programmers. It consists of 40 chapters that are filled with programming examples. If you have written programs in other languages, you will have the necessary background to understand the material presented in this book. If you have programmed in C or C++, then you will be able to quickly get up to speed with Java, since its syntax is based on these languages. If you have never programmed before, then you will have a difficult time using this book, because it assumes familiarity with basic programming concepts such as variables, types, statements, and expressions. I suggest that you pick up an introductory programming book to help you learn this material.
Conventions Used in This Book
This book uses certain conventions that make it easier for you to use.
A monospaced font is used to identify program code. An italic monospaced font is used to identify placeholders used in Java syntax descriptions.
Note |
Notes like this are used to call your attention to information that is important to understanding and using Java. |
Tip |
Tips like this are used to identify ways that you can use Java more efficiently or take advantage of undocumented features in the Java Developer's Kit or Java-enabled browsers. |
Warning |
Warnings like this are used to help you to avoid common problems encountered when using Java and to keep you clear of potential programming difficulties. |
In order to help you understand where you are going and where you have been, each chapter begins with a short description of the information that will be presented and ends with a summary of the material that has been covered.
Getting Started
To use this book with the Java Developer's Kit, you'll need a computer and operating system that are capable of running Java 1.0. There are a wide variety of computers and operating systems that support Java, and Java continues to be ported to new hardware and software platforms.
To effectively use Java with Windows 95, you will need the following:
- A personal computer that is capable of running Windows 95
- At least 8MB of RAM
- At least 10MB of available hard disk space
- A VGA monitor
- A mouse
To effectively use this book, you will need access to a compact disc drive so that you can copy files from the enclosed CD. In addition, an Internet connection will be required to complete some of the networking examples, and a Java-enabled browser, such as Netscape 2.0 or later, will be needed to work with Java applets and JavaScript. A sound card and speakers will be needed to use audio-playing applets.
You can get started if you are able to run Windows 95 and have access to the Web. Chapter 2 shows you how to obtain the Java Developer's Kit from Sun's JavaSoft website. You can add additional hardware, as necessary, to complete the programming examples of each chapter.
The best way to use this book is to start with Chapter 1 and proceed through each chapter, in succession, working through each programming example that is presented. You will learn to program in Java by compiling, running, analyzing, and understanding the sample programs. Additional hands-on learning can be acquired by tinkering with the sample programs, modifying them, and augmenting their capabilities.