Peter Norton's Guide to

    Java Programming



by Peter Norton & Wiliam Stanek


C  O  N  T  E  N  T  S




Introducing

Chapter 1  Introducing Java

Chapter 2  Getting Started with the JDK

Chapter 3  The Java Browser and the World Wide Web: A Primer

Chapter 4  The Java Language: A Primer

Chapter 5  Java Tools and the JDK: A Primer

Chapter 6  Fundamentals of the Java Language

Chapter 7  Building Objects

Chapter 8  Tying It All Together: Threads, Exceptions, and More

Chapter 9  Introducing the Java API

Chapter 10  The java.lang and java.applet Class Libraries

Chapter 11  The AWT Class Library

Chapter 12  The Java I/O and Utility Class Libraries

Chapter 13  The Net and Debug Class Libraries

Chapter 14  Creating a Java Applet

Chapter 15  Greating Java-Powered Web Presentations with Applets

Chapter 16  Applet Reuse

Chapter 17  Creating a Stand-alone Application

Chapter 18  Using Java Applications in the Real World

Chapter 19  Application Upgrades

Chapter 20  Designing and Implementing Advanced Applets

Chapter 21  Designing and Implementing Advanced Applications

Chapter 22  Integrating Native Programs and Libraries

Chapter 23  Advanced Debugging and Troubleshooting

Chapter 24  The Java Virtual Machine

Appendix A  Java API Table Reference

Appendix B  Java Terminology

Credits



To the dreamers of the world who dream the perfect dreams and then create wonderful things like Java.

Copyright © 1996 by Peter Norton

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-088-6

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. Peter Norton's stylized signature, and Peter Norton's crossed-arms pose are registered trademarks of Peter Norton. Java is a trademark of Sun Microsystems, Inc.




President, Sams PublishingRichard K. Swadley
Publishing Team Leader Greg Wiegand
Managing Editor Cindy Morrow
Director of Marketing John Pierce
Assistant Marketing Manager Kristina Perry, Rachel Wolfe




Acquisitions EditorChristopher DennyDevelopment EditorAnthony Amico, L. Angelique Brittingham
Software Development SpecialistSteve StraigerProduction EditorKimberly K. Hannel
Copy EditorMarilyn StoneIndexerTom Griffin
Technical ReviewersKaren Clere, Christopher StoneEditorial CoordinatorBill Whitmer
Technical Edit CoordinatorLynette QuinnResource CoordinatorDeborah Frisby
FormatterFrank SinclairEditorial AssistantsCarol Ackerman, Andi Richter, Rhonda Tinch-Mize,
Cover DesignerTim AmrheinBook DesignerAlyssa Yesh
Copy WriterPeter FullerProduction Team SupervisorBrad Chinn
ProductionMary Ann Abramson, Georgiana Briggs, Susan Knose, Ayanna Lacey

Acknowledgments

Creating the perfect book on Java programming required a great deal of effort-long hours, days without sleep, grueling research, and much more. I want to personally thank everyone who poured their heart into this book.

Special thanks to my wife and family, who continue to put up with the tappety-tap of my keyboard at all hours of the day and night. Without your support, this book wouldn't have been possible.


About the Author

William Robert Stanek (director@tvp.com) is the publisher and founder of The Virtual Press (http://tvp.com/ and mirror site http://www.tvpress.com/), a non-traditional press established in March 1994. As a publisher and writer with over 10 years of experience on networks, he brings a solid voice of experience on the Internet and electronic publishing to his many projects. He was first introduced to Internet e-mail in 1988 when he worked for the government, and has been involved in the commercial Internet community since 1991.

His years of practical experience are backed by a solid education, including a Master of Science in Information Systems and a Bachelor of Science in Computer Science. While his press publishes electronic books under four imprints, the core business is shifting, and the time that isn't spent writing such runaway hits as Sams.net's Web Publishing Unleashed is spent consulting with corporate clients and developing hot new Web sites.

William served in the Persian Gulf War as a combat crew member on an Electronic Warfare aircraft. During the war, he flew on numerous combat missions into Iraq and was awarded nine medals for his wartime service including one of our nation's highest flying honors, the Air Force Distinguished Flying Cross. He has written many books, articles, and essays. His book-length projects include nine fiction titles and six nonfiction titles. Watch for the upcoming release of his FrontPage Unleashed from Sams.net in August 1996. When he's not writing or working, he spends time with his family, his favorite time of the day being when he reads to his youngest child.

Tell Us What You Think!

As a reader, you are the most important critic and commentator of our books. We value your opinion and want to know what we're doing right, what we could do better, in what areas you'd like to see us publish, and any other words of wisdom you're willing to pass our way. You can help us make strong books that meet your needs and give you the computer guidance you require.

Do you have access to CompuServe or the World Wide Web? Then check out our CompuServe forum by typing GO SAMS at any prompt. If you prefer the World Wide Web, check out our site at http://www.mcp.com.

Note
If you have a technical question about this book, call the technical support line at (800) 571-5840, ext. 3668.

As the team leader of the group that created this book, I welcome your comments. You can fax, e-mail, or write me directly to let me know what you did or didn't like about this book-as well as what we can do to make our books stronger. Here's the information:


Introduction

Peter Norton's Guide to Java Programming is the definitive guide to the Java programming language. It is designed to meet the needs of today's Web publishers and programmers by helping you set the pace for the future of Internet and object-oriented programming. Not only does this book provide the broadest and most extensive coverage of essential issues, it is the only book on the market of its kind.

Java represents the future of object-oriented programming, and Peter Norton's Guide to Java Programming is the key to this future. We have taken great care to provide invaluable tips and pour our expertise into every page. As you read this book, you will learn about everything the Java programming language has to offer.

For Internet developers, programmers, and Web publishers, now is the time to get in on the inside track to Java. Throughout much of 1995, Java was in Alpha and Beta testing. During this time, Java development was limited to the Solaris and Windows NT environments. But all this quickly changed as word about Java spread like wildfire. In January 1996, Sun Microsystems officially released Java 1.0, and you can now obtain free developer's kits for every major operating system including Solaris, AIX, Windows 95/NT, OS/2, and Macintosh.

For non-programmers, using a programming language such as Java may seem impossible, but nothing could be further from the truth. The simple fact is that if you can create an HTML, a SGML, or a VRML document, you should be able to create Java-powered documents and applications as well. You will find that Java is easy to learn and use because it eliminates or automates many of the problem areas of earlier programming languages.

The power of Java is that even the most basic Java programs can feature multimedia. This is primarily because the fundamentals of the Java programming language are easy to learn and use. Through a plain-English approach, I guide you from the fundamentals to advanced programming and beyond. Here are the major topics covered in this book:

Who Should Read This Book?

Peter Norton's Guide to Java Programming is for today's Web publishers and programmers. Although this book is intended for those with a casual to accomplished knowledge of programming or Web publishing, the plain-English approach makes this book perfect for just about anyone. I truly hope you find this book to be invaluable as you plot your course to success in Java programming.

How This Book Is Organized

This book is designed to be the most comprehensive resource on the Java programming language available anywhere. Chapter by chapter, you will learn everything you need to know to develop and design powerful programs using Java.

Part I, "Next Stop-Java," covers everything you need to know to get started. After introducing the Java programming language and the basic principles of object-oriented programming, Chapter 1 explores the evolution of programming languages and how Java came to be. Chapter 2 details everything the programmer or Web publisher needs to get started with creating Java programs, including installing the Java Developer's Kit. The chapter also provides insight into how C/C++ programmers can best make the transition to Java.

The primers in Part II, "Power Primers," are designed to give you a jump-start on Java development. In addition to being a primer on the World Wide Web, Chapter 3 demonstrates the power of Java through a discussion of the multimedia uses of Java on the Web. Chapter 4 provides a primer on the Java programming language. In this chapter, you create your first Java applet and a Java application. Chapter 5 discusses the essential tools for creating and developing Java programs and how to use them. Featured tools include the developer's tools in the Java Developer's Kit and Java WorkShop, a hot new graphical development environment.

Part III, "Anatomy of the Java Language," examines the inner workings of the Java programming language. Chapter 6 explores the fundamentals of the language including tokens, types, expressions, declarations, and control flow statements. In Chapter 7, everything discussed so far starts to come together as you learn that Java objects are built from classes that contain the fundamentals, a group of classes forms a package, and abstract classes called interfaces patch the structure together. Chapter 8 explores the key concepts of multithreading, error handling, and streaming. These concepts are essential to complete the final pieces of Java programming theories.

The Java Application Programming Interface contains a wealth of ready-to-use code that can save you development time and money. Part IV, "The Java Application Programming Interface," examines everything the API has to offer. In Chapter 9, you will find a list of all available API packages and their relation to one another. Chapter 10 examines the Applet and Language class libraries and their uses in Java programs. Chapter 11 examines the Abstract Windowing Toolkit library and its uses in Java programs. Chapter 12 examines the I/O and Utility libraries and their uses in Java programs. Chapter 13 examines the Net and Debug libraries and their uses in Java programs.

Part V, "Developing Java Applets for the Web," details everything you need to know to develop Java programs for use on the Web. Through example applets, Chapter 14 demonstrates the structure of basic and intermediate-level applets and provides steps for building your own applets. After creating and testing an applet, the next important step is incorporating the applet into an HTML document for use on the World Wide Web. Chapter 15 begins with a brief overview of HTML document structure and then explores HTML elements associated with Java applets. Finally, the chapter shows how the applet can be used in Web publications. An essential concept in object-oriented programming is reusing existing code, which saves development time and money. Chapter 16 discusses when and how to reuse code and provides real-world examples.

Part VI, "Developing Stand-alone Applications," details everything you need to know to develop stand-alone applications. Chapter 17 begins by examining how Java applications differ from applets and then demonstrates the structure of basic and intermediate-level applications. Because useful stand-alone applications are more difficult to create than applets, Chapter 18 provides insight into and examples of the features needed in applications. The chapter goes on to discuss how to increase the functionality of applications. In the real world, software applications have a development life cycle. Chapter 19 explains this life cycle in more detail than previous chapters and shows how to successfully implement application upgrades.

In Part VII, "Advanced Issues," you explore advanced design and development issues. Chapter 20 provides a hands-on example and complete walk-through of an advanced applet. You can use this chapter as a stepping stone for creating your own advanced applets. Chapter 21 provides a hands-on example and complete walk-through of an advanced application. You can use this chapter as a stepping stone for creating your own advanced applications. In Chapter 22, you learn how to use native programs and libraries with Java. These native programs can be in any other programming language including C, C++, and Smalltalk. Debugging and troubleshooting is another important part of programming. Chapter 23 explores advanced debugging issues and provides hands-on examples of using the Java debugger. The final chapter provides a detailed look at the Java runtime system and the Virtual Machine. Understanding the abstract specification of the Java Virtual Machine provides wonderful insight into how Java truly works.

The final section of the book puts key reference resources into your hands. Appendix A, "Java API Table Reference," is an invaluable at-a-glance resource for the Java API. Appendix B, "Java Terminology," is a glossary of terms you will use when working with Java.

Conventions Used in This Book

I have used the following conventions in this book.

Type Conventions

FAX: 317/581-4669
E-mail: programming_mgr@sams.mcp.com
Mail: Greg Wiegand
Comments Department
Sams Publishing
201 W. 103rd Street
Indianapolis, IN 46290
monospace This computer font is used to represent code, whether onscreen or typed by you, and for filenames, directory names, and other nonstandard words.
italic monospace I use italic computer font when the text is a placeholder or a variable rather than absolute code.
italic Italic type is used to call your attention to new terms.

Icons

Note
Notes tell you about interesting facts that aren't really necessary to your understanding of Java; they're just for your information.

Tip
Tips contain tidbits of information that you might not learn elsewhere, or sometimes just tell you about a different way of doing something.

Warning
Warnings mean watch out-you can get into some trouble! Make sure you understand a warning before you follow any instructions that come after it.

Peter's Principle
I'll use the Peter's Principle icon to tell you about a rule that you might adopt as a standard operating procedure. It's like a tip that you can implement to improve general situations, and you can always use the principle as a rule to live by.

Looking Ahead
I include these boxes to give you a peek at what's coming up later and tell you how what you're currently reading will help you then.

The capability icon is intended to alert you to a situation or condition that you should track. It means that situation is dynamic and that better facilities are becoming available all the time.

Watch for the development issue icon to tell you how to implement or expand some function. In some ways it might be like the troubleshooting icon in that it tells you about a possible problem, but it will always offer some solution to the problem.

Each time you see the performance icon I'll be telling you about a technique you can use to make your product more efficient. I may offer some alternatives so that you can choose which method is best for you.

When you see this icon it means that there is a possibility for something to go wrong. The text shows you how to identify the problem and suggests a method for solving it.