Preface

To the Reader

In late 1995, the Java programming language burst onto the Internet scene and gained instant celebrity status. The promise of Java technology was that it would become the universal glue that connects users with information, whether that information comes from web servers, databases, information providers, or 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 1995, Sun Microsystems has released six major revisions of the Java Development Kit. Over the course of the last nine years, the Application Programming Interface (API) has grown from about 200 to over 3,000 classes. The API now spans such diverse areas as user interface construction, database management, internationalization, security, and XML processing. JDK 5.0, released in 2004, is the most impressive update of the Java language since the original Java release.

The book you have in your hand is the first volume of the seventh edition of the Core Java 2 book. With the publishing of each edition, 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. In this edition, we are enthusiastic users of generic collections, the enhanced for loop, and other exciting features of JDK 5.0.

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 for Windows, UNIX, or the Macintosh.)

What we do is assume you want to:

  • Write real code to solve real problems

and

  • Don’t like books filled with toy examples (such as toasters, fruits, or zoo animals)

In this book you will find lots of sample code 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:

  • Object-oriented programming

  • Reflection and proxies

  • Interfaces and inner classes

  • The event listener model

  • Graphical user interface design with the Swing UI toolkit

  • Exception handling

  • Stream input/output and object serialization

  • Generic programming

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:

  • Multithreading

  • Distributed objects

  • Databases

  • Advanced GUI components

  • Native methods

  • XML processing

  • Network programming

  • Collection classes

  • Advanced graphics

  • Internationalization

  • JavaBeans

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 http://www.horstmann.com/corejava.html. Strategically placed at the end of the errata page (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 download and install the JDK and the program examples for this book. Then we guide you through compiling and running three typical Java programs, a console application, a graphical application, and an applet, using the plain JDK, a Java-enabled text editor, and a Java IDE.

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, 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 to have full access to the power of Java’s completely object-oriented approach to programming. We also cover a useful technical feature of Java 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 window toolkit. 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 allows you 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 look at applets as a method of program deployment. We then describe how to package applications in JAR files, and how to deliver applications over the Internet with the Java Web Start mechanism. Finally, we explain how Java programs can store and retrieve configuration information once they have been deployed.

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 an integrated development environment, a profiler, a code coverage testing tool, and the AWT robot.

The topic of Chapter 12 is 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. We show you what goes on under the hood when you use the object serialization mechanism, which makes saving and loading objects easy and convenient. Finally, we cover several libraries that have been added to JDK 1.4: the “new I/O” classes that contain support for advanced and more efficient file operations, and the regular expression library.

We finish the book with an overview of generic programming, a major advance of JDK 5.0. Generic programming will make your programs easier to read and safer. We show you how you can use strong typing with collections and remove unsightly and unsafe casts.

Appendix A lists the reserved words of the Java language.

Apppendix B tells you how to modify the code examples so that they compile on an older (JDK 1.4) compiler.

Conventions

As is common in many computer books, we use monospace type to represent computer code.

C++ Note

C++ Note

There are many C++ notes that explain the difference between Java and C++. You can skip over them if you don’t have a background in C++ or if you consider your experience with that language a bad dream of which you’d rather not be reminded.

Note

Note

Notes are tagged with “note” icons that look like this.

Tip

Tip

Tips are tagged with the “tip” icon that look like this.

Caution

Caution

When there is danger ahead, we warn you with a “caution” icon.

Caution
Application Programming Interface

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 at the end of the section. These descriptions are a bit more informal, but we hope also a little more informative than those in the official on-line API documentation. We now tag each API note with the version number in which the feature was introduced, to help the readers who don’t use the “bleeding edge” version of Java.

Programs whose source code is on the Web are listed as examples, for instance

Example 2–4: WelcomeApplet.java

Sample Code

The web site for this book at http://www.phptr.com/corejava 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 Development Kit. See Chapter 2 for more information about installing the Java Development Kit and the sample code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset