Table Of Contenti
What’s New in This Edition
Over the last few years Java has been maturing and evolving into the latest
Java release, 1.1—a much more robust programming language. This edition
of the Java Developer’s Guide has been updated to match this new release and
show you the new maturity and capability of the language.
As well as reorganizing the material in the text to make it easier to under-
stand, we have added some informational chapters to help you understand
the Java paradigm by tying together some of the less obvious concepts. We
also have added additional technical chapters, specifically coverage of the
remote invocation package, the SQL package, printing, security, and many
other new concepts.
When studying the trends in Java development, we found that the future of
Java is dependent upon database architecture and the implementation of
distributed applications. For this reason, we added two new sections with
multiple chapters to help you understand these most important features in
Java. At the same time we know that without enhanced security, any appli-
cation is doomed; therefore, we’ve added coverage of the new security fea-
tures of Java 1.1 also.
Although you’ll see other new topics covered in the book, don’t skip over
Java’s latest component technology. We’ve provided an in-depth coverage
of JavaBeans and what it means to the application environment.
00.283-8 FM 1 9/8/97, 9:31 AM
Java 1.1 Developer’s Guide
ii
00.283-8 FM 2 9/8/97, 9:31 AM
iii
JAVA 1.1
Developer’s Guide
Second Edition
Jamie Jaworski
Joe
Author
201 West 103rd Street
S a m s . n e t D e v e l o p e r ’ s G u i d e
Indianapolis, Indiana 46290
00.283-8 FM 3 9/8/97, 9:31 AM
Java 1.1 Developer’s Guide
iv This book is dedicated to my parents, Stanley and Gloria. Acquisitions Editor
Christopher Denny
Development Editor
Copyright © 1997 by Sams.net Publishing Anthony Amico
SECOND EDITION Software Development Specialist
Brad Myers
All rights reserved. No part of this book shall be reproduced, stored in a retrieval
system, or transmitted by any means, electronic, mechanical, photocopying, Production Editor
recording, or otherwise, without written permission from the publisher. No Colleen Williams
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 Indexer
book, the publisher and author assume no responsibility for errors or omissions. Christine Nelsen
Neither is any liability assumed for damages resulting from the use of the
Technical Reviewer
information contained herein. For information, address Sams.net Publishing,
Craig Olague
201 W. 103rd St., Indianapolis, IN 46290.
Jeff Perkins
International Standard Book Number: 1-57521-283-8
Editorial Coordinators
Library of Congress Catalog Card Number: 96-72399 Mandie Rowell
Katie Wise
2000 99 98 97 4 3 2 1
Technical Edit Coordinator
Interpretation of the printing code: the rightmost double-digit number is the
year of the book’s printing; the rightmost single-digit, the number of the book’s Lynette Quinn
printing. For example, a printing code of 97-1 shows that the first printing of the
Editorial Assistants
book occurred in 1997.
Carol Ackerman
Composed in AGaramond and MCPdigital by Macmillan Computer Publishing Andi Richter
Rhonda Tinch-Mize
Printed in the United States of America
Karen Williams
Trademarks Cover Designer
Alyssa Yesh
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 Book Designer
the accuracy of this information. Use of a term in this book should not be Anne Jones
regarded as affecting the validity of any trademark or service mark. Java is a
Copy Writer
registered trademark of Sun Microsystems, Inc.
David Reichwein
Production Team Supervisors
President, Sams.net Publishing Richard K. Swadley
Brad Chinn
Publishing Manager Greg Wiegand Charlotte Clapp
Director of Editorial Services Cindy Morrow Production
Managing Editor Kitty Wilson Jarrett Mona Brown
Jeanne Clark
Director of Marketing Kelli Spencer
Jennifer Dierdorff
Product Marketing Managers Wendy Gilbride
Lana Dominguez
Kim Margolius Brad Lenser
Becky Stutzman
Associate Product Marketing Manager Jennifer Pock
Marketing Coordinator Linda Beckwith
00.283-8 FM 4 9/8/97, 9:31 AM
Overview v
Introduction xxxv
Part I Introduction to Java
1 The Java Phenomenon 3
2 Java Overview 11
3 Using the Java Development Kit 25
Part II Programming in Java
4 First Programs: Hello World! to BlackJack 47
5 Classes and Objects 89
6 Interfaces 123
7 Exceptions 145
8 Multithreading 159
9 Using the Debugger 173
10 Automating Software Documentation 187
11 Language Summary 199
Part III Using the Java API
12 Portable Software and the java.lang Packages 217
13 Stream-Based Input/Output and the java.io Package 243
14 Useful Tools in the java.util and java.math Packages 283
15 Window Programming with the java.awt Packages 309
16 Web Programming with the java.applet Package 329
17 Network Programming with the java.net Package 343
18 Building Distributed Applications with the java.rmi Packages 371
19 Connecting to Databases with the java.sql Package 385
20 Security and the java.security Packages 405
21 Internationalization and the java.text Package 425
22 Building Reusable Components with java.beans 441
00.283-8 FM 5 9/8/97, 9:31 AM
Java 1.1 Developer’s Guide
vi
Part IV Window Programming
23 Opening Windows 461
24 Organizing Window Programs 481
25 Menus, Buttons, and Dialog Boxes 513
26 Checkboxes, Choices, and Lists 539
27 Text and Fonts 553
28 The Canvas 573
29 Scrollbars 593
30 Printing 615
31 Using the Clipboard 627
32 Using Animation 639
33 The Future of AWT 651
Part V Network Programming
34 Client Programs 659
35 Server Programs 677
36 Content Handlers 691
37 Protocol Handlers 699
38 Using Jeeves 705
39 Working with the Java Management API 723
Part VI Web Programming
40 Sample Applets 733
41 Developing Applets 749
42 Using .jar Files and Signed Applets 755
43 Working with Netscape’s LiveConnect 773
44 Using JavaBeans 783
45 Java Commerce 807
46 Server-Side Web Programming with Java 813
47 Java Broadcasting and Castanet 821
00.283-8 FM 6 9/8/97, 9:31 AM
Overview
vii
Part VII Database Programming
48 Database Fundamentals 843
49 Using JDBC 859
50 Integrating Database Support into Web Applications 881
Part VIII Developing Distributed Applications
51 Distributed Applications Architecture 903
52 Remote Method Invocation 915
53 Using Object Serialization 933
54 Java IDL and ORBs 943
55 Network Computers and JavaStation 951
Part IX Extending Java
56 Java Platform and Extensions 963
57 Java Development Tools 969
58 The JavaOS 975
59 Creating Native Methods 983
60 Java Security 991
Part X Appendixes
A Differences Between Java and C++ 1003
B Moving C/C++ Legacy Code to Java 1011
Index 1019
00.283-8 FM 7 9/8/97, 9:31 AM
viii Java 1.1 CDeveolopenr’s Gtueidents
Introduction xxxv
I Introduction to Java
1 The Java Phenomenon 3
What Is Java? ...........................................................................................5
The Origin of Java ...................................................................................6
Why Program in Java?..............................................................................7
Java and HotJava......................................................................................9
Summary................................................................................................10
2 Java Overview 11
Getting the JDK.....................................................................................12
A Quick Tour of the JDK......................................................................14
The Java Language.................................................................................17
Java Is Familiar and Simple................................................................17
Java Is Object-Oriented.....................................................................17
Java Is Safer and More Reliable..........................................................18
Java Is Secure.....................................................................................18
Java Is Multithreaded.........................................................................19
Java Is Interpreted and Portable.........................................................20
Java Is the Programming Language of the Web..................................20
The Java API..........................................................................................21
Summary................................................................................................23
3 Using the Java Development Kit 25
Overview................................................................................................26
The Compiler ........................................................................................28
Using Classes from Other Packages ...................................................28
Setting Your CLASSPATH......................................................................29
Changing the Root Directory............................................................31
Generating Debugging Tables ...........................................................31
Code Optimization............................................................................31
Suppressing Warnings .......................................................................31
Using Verbose Mode.........................................................................32
Other Compiler Options...................................................................32
The Interpreter.......................................................................................32
Help and Version Information ..........................................................33
Changing the CLASSPATH ....................................................................33
00.283-8 FM 8 9/8/97, 9:31 AM
ix
Checking for Source Code Changes...................................................34
Verifying Your Code..........................................................................34
Controlling the Garbage Collector.....................................................35
Changing Properties..........................................................................35
Setting Memory and Stack Limits......................................................36
Debugging Options...........................................................................37
The Debugger........................................................................................37
The Disassembler...................................................................................38
The Applet Viewer.................................................................................39
Running the Demo Programs............................................................40
Automating Software Documentation....................................................41
Header File Generation..........................................................................42
Other Tools ...........................................................................................43
Summary................................................................................................43
II Programming in Java
4 First Programs: Hello World! to BlackJack 47
Hello World!..........................................................................................48
Comments.........................................................................................50
Java Program Structure......................................................................51
Package Statement.............................................................................51
The import Statement........................................................................52
Classes and Methods..........................................................................53
The System Class ...............................................................................53
I Can Read!............................................................................................54
Overview of ICanReadApp...................................................................55
Declaring Variables and Creating Objects..........................................56
Identifiers and Keywords ...................................................................56
Using System.in ................................................................................57
Type This!..............................................................................................58
Overview of TypeThisApp...................................................................59
The Primitive Java Data Types..........................................................59
Literal Values.....................................................................................59
BlackJack ...............................................................................................60
Overview of BlackJackApp .................................................................67
Arrays................................................................................................81
Statements.........................................................................................82
Summary................................................................................................87
00.283-8 FM 9 9/8/97, 9:31 AM
Java 1.1 Developer’s Guide
x
5 Classes and Objects 89
Object-Oriented Programming Concepts...............................................90
It’s an Object-Oriented World ..........................................................90
Composition and Reuse.....................................................................91
Classes...............................................................................................92
Classification and Inheritance............................................................92
Multiple Inheritance..........................................................................94
Messages, Methods, and Object Interaction.......................................95
Encapsulation....................................................................................95
Polymorphism...................................................................................96
Dynamic Binding..............................................................................96
Java Classes ............................................................................................96
Class Syntax.......................................................................................97
The Point Class.................................................................................97
Class Modifiers..................................................................................98
Extending Superclasses ......................................................................99
Adding Body to Classes .....................................................................99
The CGrid Class...............................................................................101
The CGObject Class..........................................................................103
The PrintCGrid Class.......................................................................104
The BorderedPrintCGrid Class.........................................................106
The CGPoint Class............................................................................108
The CGBox Class...............................................................................109
The CGText Class .............................................................................111
The KeyboardInput Class .................................................................112
The CDrawApp Program.....................................................................113
Running CDrawApp ...........................................................................116
CDrawApp’s Implementation of Object-Oriented Concepts ...............121
Summary..............................................................................................122
6 Interfaces 123
The Purpose of Java Interfaces..............................................................124
The Benefits of Interfaces.....................................................................124
Declaring Interfaces..............................................................................125
Implementing Interfaces.......................................................................126
The CDrawApp Interface Example...........................................................126
The CGTextEdit Interface.................................................................127
Updating the CGText Class...............................................................127
The CGTextPoint Class.....................................................................128
The CGTextBox Class........................................................................130
Updating the CDraw Class.................................................................131
Running the Example......................................................................135
Example Summary...........................................................................139
00.283-8 FM 10 9/8/97, 9:31 AM
Description:Decent one for anybody trying to grasp Java - with an abundantly available time of course.