To the Reader
In late 1995, the Java programming language burst onto the Internet scene and gained instant celebrity status. The promise of Java is that it will become the universal glue that connects users with information, whether that information comes from Web servers, databases, information providers, and any other imaginable source. Indeed Java is in a unique position to fulfill this promise. It is an extremely solidly engineered language that has gained acceptance by all major vendors, except for Microsoft. Its built-in security and safety features are reassuring both to programmers and to the users of Java programs. Java even has built-in support that makes advanced programming tasks, such as network programming, database connectivity, and multithreading, straightforward.
Since then, Sun Microsystems has released four major revisions of the Java Software Development Kit. Version 1.02, released in 1996, supported database connectivity and distributed objects. Version 1.1, released in 1997, added a robust event model, internationalization, and the Java Beans component model. Version 1.2, released at the end of 1998, has numerous enhancements, but one major improvement stands out: the "Swing" user interface toolkit that finally allows programmers to write truly portable GUI applications. Version 1.3, released in the spring of 2000, delivered many incremental improvements.
The book you have in your hand is the first volume of the fifth edition of the Core Java book. Each time, the book followed the release of the Java development kit as quickly as possible, and each time, we rewrote the book to take advantage of the newest Java features.
As with the previous editions of this book, we still target serious programmers who want to put Java to work on real projects. We still guarantee no nervous text or dancing tooth-shaped characters. We think of you, our reader, as a programmer with a solid background in a programming language. But you do not need to know C++ or object-oriented programming. Based on the responses we have received to the earlier editions of this book, we remain confident that experienced Visual Basic, C, or COBOL programmers will have no trouble with this book. (You don't even need any experience in building graphical user interfaces in Windows, Unix, or the Macintosh.)
What we do is assume you want to:
Write real code to solve real problems
Don't like books filled with toy examples (such as kitchen appliances or fruit trees).
You will find lots of sample code on the accompanying CD that demonstrates almost every language and library feature that we discuss. We kept the sample programs purposefully simple to focus on the major points, but, for the most part, they aren't fake and they don't cut corners. They should make good starting points for your own code.
We assume you are willing, even eager, to learn about all the advanced features that Java puts at your disposal. For example, we give you a detailed treatment of:
Reflection and proxies
Interfaces and inner classes
The event listener model
Graphical user interface design with the Swing UI toolkit
Stream input/output and object serialization
We still don't spend much time on the fun but less serious kind of Java programs whose sole purpose is to liven up your Web page. There are quite a few sources for this kind of material alreadywe recommend John Pew's book Instant Java, also published by Sun Microsystems Press/Prentice Hall.
Finally, with the explosive growth of the Java class library, a one-volume treatment of all the features of Java that serious programmers need to know is no longer possible. Hence, we decided to break the book up into two volumes. The first volume, which you hold in your hands, concentrates on the fundamental concepts of the Java language, along with the basics of user-interface programming. The second volume goes further into the enterprise features and advanced user-interface programming. It includes detailed discussions of:
Advanced GUI components
When writing a book, errors and inaccuracies are inevitable. We'd very much like to know about them. But, of course, we'd prefer to learn about each of them only once. We have put up a list of frequently asked questions, bugs fixes, and workarounds in a Web page at horstmann/corejava.html. Strategically placed at the end of the FAQ (to encourage you to read through it first) is a form you can use to report bugs and suggest improvements. Please don't be disappointed if we don't answer every query or if we don't get back to you immediately. We do read all e-mail and appreciate your input to make future editions of this book clearer and more informative.
We hope that you find this book enjoyable and helpful in your Java programming. About This Book
Chapter 1 gives an overview of the capabilities of Java that set it apart from other programming languages. We explain what the designers of the language set out to do and to what extent they succeeded. Then, we give a short history of how Java came into being and how it has evolved.
In Chapter 2, we tell you how to install Java and the companion software for
this book from the CD-ROM onto your computer. Then we guide you through compiling and running three typical Java programs, a console application, a graphical application, and an applet.
Chapter 3 starts the discussion of the Java language. In this chapter, we cover the basics: variables, loops, and simple functions. If you are a C or C++ programmer, this is smooth sailing because the syntax for these language features is essentially the same as in C. If you come from a non-C background such as Visual Basic or COBOL, you will want to read this chapter carefully.
Object-oriented programming (OOP) is now in the mainstream of programming practice, and Java is completely object oriented. Chapter 4 introduces encapsulation, the first of two fundamental building blocks of object orientation, and the Java language mechanism to implement it, that is, classes and methods. In addition to the rules of the Java language, we also give advice on sound OOP design. Finally, we cover the marvelous javadoc tool that formats your code comments as a set of hyperlinked web pages. If you are familiar with C++, then you can browse through this chapter quickly. Programmers coming from a non-object-oriented background should expect to spend some time mastering OOP concepts before going further with Java.
Classes and encapsulation are only one part of the OOP story, and Chapter 5 introduces the other, namely, inheritance. Inheritance lets you take an existing class and modify it according to your needs. This is a fundamental technique for programming in Java. The inheritance mechanism in Java is quite similar to that in C++. Once again, C++ programmers can focus on the differences between the languages.
Chapter 6 shows you how to use Java's notion of an interface. Interfaces let you go beyond the simple inheritance model of Chapter 5. Mastering interfaces allows you full access to the power of Java's completely object-oriented approach to programming. We also cover a useful technical feature of Java here. These are called inner classes. Inner classes help make your code cleaner and more concise.
In Chapter 7, we begin application programming in earnest. We show how you can make windows, how to paint on them, how to draw with geometric shapes, how to format text in multiple fonts, and how to display images.
Chapter 8 is a detailed discussion of the event model of the AWT, the abstract windows toolkit. (We discuss the event model that was added to Java 1.1, not the obsolete and simplistic 1.0 event model.) You'll see how to write the code that responds to events like mouse clicks or key presses. Along the way you'll see how to handle basic GUI elements like buttons and panels.
Chapter 9 discusses the Swing GUI toolkit in great detail. The Swing toolkit is how you can use Java to build a cross-platform graphical user interface. You'll learn all about the various kinds of buttons, text components, borders, sliders, list boxes, menus, and dialog boxes. However, some of the more advanced components are discussed in Volume 2.
After you finish Chapter 9, you finally have all mechanisms in place to write applets, those mini-programs that can live inside a Web page, and so applets are the topic of Chapter 10. We show you a number of useful and fun applets, but more importantly, we show you what goes on behind the scenes. And we show you how to use the Java Plug-in that enables you to roll out applets that take advantage of all the newest Java features, even if your users use old browsers or browsers made by hostile vendors.
Chapter 11 discusses exception handling, Java's robust mechanism to deal with the fact that bad things can happen to good programs. For example, a network connection can become unavailable in the middle of a file download, a disk can fill up, and so on. Exceptions give you an efficient way of separating the normal processing code from the error handling. Of course, even after hardening your program by handling all exceptional conditions, it still might fail to work as expected. In the second half of this chapter, we give you a large number of useful debugging tips. Finally, we guide you through sample sessions with various tools: the JDB debugger, the debugger of the Forte development environment, a profiler, a code coverage testing tool and the AWT robot.
We finish the book with input and output handling. In Java, all I/O is handled through so-called streams. Streams let you deal in a uniform manner with communicating with any source of data, such as files, network connections, or memory blocks. We include detailed coverage of the reader and writer classes, which make it easy to deal with Unicode; and we show you what goes on under the hood when you use object serialization mechanism, which makes saving and loading objects easy and convenient.
An appendix lists the Java language keywords.CD-ROM
The CD-ROM on the back of the book contains the latest version of the Java Software Development Kit. At the time we are writing this, these materials are available only for Windows 95/NT or Solaris 2.
Of course, the CD-ROM contains all sample code from the book, in compressed form. You can expand the file either with one of the familiar unzipping programs or simply with the jar utility that is part of the Java Software Development Kit.
The CD-ROM also contains a small selection of "best of breed" programs that you may find helpful for your development. Generally, these programs require that you pay the vendors some amount of money if you use them beyond a trial period. We have no connection with the vendors, except as satisfied users of their products. Please contact the vendors directly with any questions you may have about the programs.
NOTE: People have often asked what the licensing requirements for using the sample code in a commercial situation are. You can freely use any code from this book for non-commercial use. However, if you do want to use the code as a basis for a commercial product, we simply require that every Java programmer on the development team for that project own a copy of Core Java.
- The experienced developer's guide to Java programmingnow fully updated for JDK 1.3
- Completely revised coverage of object-oriented development and Swing classes
- More of the robust code examples professional programmers need
- CD-ROM includes all source code, J2SE Version 1.3, Forte for Java Community Edition, and much more!
Raves for the previous edition!
"Devoid of shaky, academic examples and packed with robust demonstrations that illustrate hundreds of powerful concepts ...The authors back up the many examples with sharp, fact-rich commentary on how to get things done with Java."
David Wall, Amazon.com
The best-selling guide for serious Java 2 programmersfully updated for JDK 1.3!
Ask any experienced Java programmer: Core Java delivers the real-world guidance you need to accomplish even the most challenging tasks. That's why it's been an international best seller for five straight years. Core Java 2, Volume 1 covers the fundamentals of Java 2, Standard Edition, Version 1.3 and includes completely revised discussions of object-oriented Java development, enhanced coverage of Swing user interface components, and much more.
This new fifth edition delivers even more of the robust, real-world programs previous editions are famous forupdated to reflect deployment and performance enhancements. Volume 1 includes thorough explanations of inner classes, dynamic proxy classes, exception handling, debugging, the Java event model, Input/Output, file management, and much more. For experienced programmers, Core Java 2, Volume 1: Fundamentals sets the standardagain!
State-of-the-art information for Java developers, including:
- Building GUI applications with Swing classes
- Making the most of dynamic proxy classes and inner classes
- Mastering the Java event model
- Understanding Java streams and file management
About the CD-ROM
CD-ROM contains complete source code examples, the Java 2, Standard Edition, Version 1.3 SDK, and useful tools, including Forte for Java, Community Edition, a complete toolset for Java application development, TextPad 4.32, HexWorkshop 3.1, WinZip 8.0, Together J, and SourceAgain.