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

Chapter 4  First Programs: Hello World! to BlackJack

Chapter 5  Classes and Objects

Chapter 6  Interfaces

Chapter 7  Exceptions

Chapter 8  Multithreading

Chapter 9  Using the Debugger

Chapter 10  Automating Software Documentation

Chapter 11  Language Summary

Chapter 12  Portable Software and the java.lang Package

Chapter 13  Stream-Based Input/Output and the java.io Package

Chapter 14  Useful Tools in the java.util Package

Chapter 15  Window Programming with the java.awt Package

Chapter 16  Web Programming With the java.applet Package

Chapter 17  Network Programming with the java.net Package

Chapter 18  Opening Windows

Chapter 19  Organizing Window Programs

Chapter 20  Menus, Buttons, and Dialong Boxes

Chapter 21  Checkboxes, Choices, and Lists

Chapter 22  Text and Fonts

Chapter 23  The Canvas

Chapter 24  Scrollbars

Chapter 25  Using Animation

Chapter 26  Client Programs

Chapter 27  Server Programs

Chapter 28  Content Handlers

Chapter 29  Protocol Handlers

Chapter 30  Sample Applets

Chapter 31  Developing Applets

Chapter 32  Integrating Applets into Distributed Applications

Chapter 33  Working with JavaScript

Chapter 34  Sample Scripts

Chapter 35  JavaScript Reference

Chapter 36  The Java Source Code

Chapter 37  The Java Virtual Machine

Chapter 38  Creating Native Methods

Chapter 39  Java Security

Chapter 40  Java Platforms and Extensions

Appendix A  The Jawa API Quick Reference

Appendix B  Differences Between java and C++

Appendix C  Moving C/C++ Legacy Code to Java

Credits



This book is dedicated to Lisa, Emily, and Jason.

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 PublishingRichard K. Swadley
Publishing Manager Mark Taber
Managing Editor Cindy Morrow
Marketing Manager John Pierce
Assistant Marketing Manager Kristina Perry




Acquisitions EditorChristopher DennyDevelopment EditorL. Angelique Brittingham
Software Development SpecialistMerle NewlonProduction EditorKitty Wilson
Copy EditorKimberly K. HannelTechnical ReviewerChristopher M. Stone
Editorial CoordinatorBill WhitmerTechnical Edit CoordinatorLynette Quinn
FormatterFrank SinclairEditorial AssistantsCarol Ackerman, Andi Richter, Rhonda Tinch-Mize,
Cover DesignerTim AmrheinBook DesignerAlyssa Yesh
Copy WriterPeter FullerProduction Team SupervisorBrad Chinn
ProductionMary 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:

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.