EARTH WEEK
Go paperless and save 40% on eligible eBooks, 70% on video now through April 26. Shop the sale.
Register your product to gain access to bonus material or receive a coupon.
76696-4
“Core Java is the best choice for experienced programmers. It is unmatched for technical depth, yet remains highly readable.” —K.N. King, Computer Reviews
Comprehensive and up-to-date coverage of advanced topics including:
Put Java to work on real problems!
Written for serious programmers who have mastered the fundamentals of the Java language, Core Java 1.1, Vol. II thoroughly explains the more advanced features and libraries you need to build real programs.
New chapters on Java Beans, Advanced AWT, Security, and Internationalization focus on the features and functions added to Java with the release of JDK 1.1. Updated chapters on multithreading and networking include extensive examples and thoroughly tested sample code that illustrate some of the most powerful features of Java.
Volume II also provides in depth coverage of the Java Enterprise API-JDBC, RMI, Native Methods-that demonstrates what objects everywhere can be realistically used for.
The CD-ROM includes all Java source code examples from the book, the latest version of JDK 1.1.x and useful shareware as well as all of the example code from Core Java 1.1, Volume I.
1. Streams.
2. Multithreading.
3. Networking.
4. Database Connectivity: JDBC.
5. Remote Objects.
6. Advanced AWT.
7. Java Beans.
8. Security.
9. Internationalization.
10. Native Methods.
Appendix I.
Index.
Preface
To the Reader
The book you have in your hands is the second part of the third edition of Core Java. The first edition appeared in early 1996, the second in late 1996. The first two editions appeared in a single volume, but the second edition was already 150 pages longer than the first, which was itself not a thin book. When we sat down to work on the third edition, it became clear to us that a one-volume treatment of all the features of Java that a serious programmer needs to know was no longer possible. Hence, we decided to break up the third edition into two volumes; the second volume is what you are looking at.
The first volume covered the essential features of the language; this volume covers the advanced topics that a Java programmer will need to know so that he or she will (we hope) be in a position to do commercial Java development. Thus, as with the first volume and the previous editions of this book, we still are targeting programmers who want to put Java to work on real projects.
Please note: If you are an experienced Java developer who is comfortable with the new event model and the other language features, such as inner classes, that were added to Java 1.1, you need not have read the first volume in order to benefit from this volume. (While we do refer to sections of the previous volume when appropriate and, of course, hope you will buy or have bought Volume 1, you can find the needed background material in any comprehensive introductory Java 1.1 book.)
Finally, when any book is being written, errors and inaccuracies are inevitable. We would very much like to hear about them. Of course, we would prefer to hear about them only once. For this reason, we have put up a Web page at http://www.horstmann.com with an FAQ, bug fixes, and workarounds. Strategically placed at the end of the Web page (to encourage you to read the previous reports) is a Java applet that you can use to report bugs or problems and to send suggestions for improvements to future editions.
About This Book
First, except for Chapter 1, which is a thorough discussion of input and output in Java 1.1, the remaining chapters in this book can almost certainly be read independently of each other-only occasional backtracking will be needed at most. We suggest reading Chapter 1 first and then turning to whatever topic interests you the most in the remaining chapters. Here are short descriptions of the various chapters.
Input/output in Java is handled through what Java calls streams, which are conceptually the same as the similarly named feature of C++. Streams let you deal in a uniform manner with the large number of possible ways of getting input from any source of data that can send out a sequence of bytes and then sending it to any destination that can also work with a sequence of bytes. In particular, we include detailed coverage of the new Reader and Writer classes in Java 1.1, which make it easy to deal with Unicode; and we include the new object serialization mechanism in Java 1.1, which makes saving the state of an object almost trivial.
Chapter 2 covers multithreading, which enables you to program tasks to be done in parallel. (A thread is a flow of control within a program.) We show you how to set up threads and how to make sure none of them get stuck. We put this knowledge to practical use by example, showing you the techniques needed to build timers and animations.
Chapter 3 covers one of the most exciting APIs in Java: the one for networking. Java makes it phenomenally easy to do sophisticated network programming. Not only do we cover this API in depth, we also discuss the important consequences of the applet security model for network programming.
Chapter 4 covers the JDBC, the Java database connectivity API. We show you how to write useful programs to handle realistic databases, using a core subset of the JDBC API. Please note that this is not a complete treatment of everything you can do with the rich JDBC API. (A complete treatment of the JDBC API would certainly require a book almost as long as this one. )
Chapter 5 covers remote objects and Remote Method Invocation (RMI). This API lets you work with Java objects that are distributed over a net or running in separate processes on a single machine. We also show you where the rallying cry of "objects everywhere" can realistically be used.
Chapter 6 covers some advanced features of the Abstract Windowing Toolkit (AWT) that seemed too specialized for coverage in Volume 1 but are, nonetheless, techniques that should be part of every Java programmer's toolkit. These features include the image manipulation API and the (partially implemented) cut-and-paste API. We actually take the cut-and-paste API one step further than JavaSoft itself did: We show you how to cut and paste Java objects between different Java programs via the system clipboard.
Chapter 7 shows you what you need to know about the new component API for Java-JavaBeans_. We show you what you need to know in order to write your own Java beans. (We do not cover the various builder environments that are designed to manipulate beans, however.) The JavaBeans_ component technology is an extraordinarily important technology for the eventual success of Java because it can potentially bring the same ease of use to Java programming environments that ActiveX_ controls give to the millions of Visual Basic_ programmers out there. Of course, since these components are written in Java, they have the advantage over ActiveX controls in that they are immediately cross-platform and immediately capable of fitting into the sophisticated security model of Java.
In fact, Chapter 8 takes up that security model. Java was designed from the ground up to be secure, and this chapter takes you under the hood to see how this design is implemented. We show you how to write your own class loaders and security managers for special-purpose applications. Then, we take up the new security API that allows for such important features as signed classes.
Chapter 9 discusses a specialized feature that we feel can only grow in importance: internationalization. Java is one of the few languages designed from the start to handle Unicode. As a result, you can internationalize Java applications so that they not only cross platforms but cross country boundaries as well. For example, we show you how to write a retirement calculator applet that uses either English, German, or Chinese-depending on the locale of the browser.
Chapter 10 takes up native methods, which let you call methods written for a specific machine such as the Windows API. Obviously, this feature is controversial: Use native methods, and the cross-platform nature of Java vanishes. Nonetheless, every serious programmer writing Java applications for specific platforms needs to know these techniques. There will be times when you need to turn to the operating system's API for your target platform when you are writing a serious application. We illustrate this by showing you how to access the registry functions in Windows.
Appendix I contains instructions for installing the software and example code from the CD-ROM.
Conventions
As is common in many computer books, we use courier type to represent computer code. There are many C++ notes that explain the difference between Java and this language. You can skip over them if you aren't interested in C++.
Notes are tagged with a "notepad" icon that looks like this. Java comes with a large programming library or Application Programming Interface (API). When using an API call for the first time, we add a short summary description, tagged with an API icon. These descriptions are a bit more informal, but also a little more informative than those in the official on-line API documentation.
Programs whose source code is on the CD-ROM are listed as examples, for instance, Example 15-8: WarehouseServer.java refers to the corresponding code on the CD-ROM.
Acknowledgements
Cay's love, gratitude, and apologies go to his wife Hui-Chen and his children Tommy and Nina for their continuing support for this never-ending project. He also appreciates the support and understanding from Vincent Pluvinage and his colleagues at Preview Software.
Gary also wants to thank Cay's family. They were extraordinarily gracious as revisions came too rapidly and took far too long. He also wants to thank his friends and his co-workers for their patience as he too slowly extricated himself from the quagmire of his own overcommitments.
Next, we both want to thank Mary Lou Nohr for her great copyediting job. Her ability to mesh two conflicting writing styles and keep things consistent amazed us. David Geary, Blake Ragsdell, and Christopher Taylor provided us with very useful reviews. Next, we want to thank the people at Prentice Hall, especially our long-suffering editor Greg Doench, for coordinating this very complicated project. Rachel Borden and John Bortner, our publisher and publicist, respectively, at Sun Microsystems Press were always there to listen with a patient ear when we complained about yet another problem-and do their best to fix it. Nikki Wise and her team at Navta Associates, Inc., came through yet one more time for a project that was again on an impossible schedule. Finally, we want to thank Niko Mak (WinZip), Keith MacDonald (TextPad), and Bob Andreasen (HexWorkshop) for allowing us to include their shareware products on the CD.