Edited by GREGORY BRILL
CodeNotes
for Java
Intermediate and
Advanced Language
Features
NEW YORK
Contents
Acknowledgments
First, thanks to John Gomez who saw the potential of the CodeNotes idea before anyone else and introduced me to Random House. Without John, there would be no CodeNotes. John, you are a true friend, a real visionary. Id also like to thank Annik LaFarge, who fearlessly championed the series and whose creativity, enthusiasm, and publishing savvy has been instrumental in its creation. Thank you to Mary Bahr, our unflappable editor, who paved the way and crafted the marketing. Thank you to Ann Godoff, whose strength, decisiveness, and wisdom gave CodeNotes just the momentum it needed. And, of course, the production, sales, and business teams at Random House, with particular thanks to Howard Weill, Jean Cody, and Richard Elman.
On the Infusion Development side, thank you to Brent Williams, Peter Chiu, and Dexter Chu for their many contributions to the book. Special thanks to Tom Nicholson for dedicated proofreading on the entire series. Thank you to the CodeNotes reviewers, who gave us invaluable feedback and suggestions on our early drafts. And thank you to the entire cast and crew of Infusion Development Corporation, who have supported and encouraged this venture throughout. I know CodeNotes was extremely trying, tough to do, and involved an awesome amount of research, writing, and editing. But here it is... as we envisioned it.
Gregory Brill
Using CodeNotes
PHILOSOPHY
The CodeNotes philosophy is that the core concepts of any technology can be presented succinctly. The product of many years of consulting and training experience, the CodeNotes series is designed to make you productive in a technology in as short a time as possible.
CODENOTES POINTERS
Throughout the book, you will encounter CodeNotes pointers: AJ010101 . These pointers are links to additional content available online at the CodeNotes website. To use a CodeNotes pointer, simply point a web browser to www.codenotes.com and enter the pointer number. The website will direct you to an article or an example that provides additional information about the topic.
CODENOTES STYLE
The CodeNotes series follows certain style guidelines:
Code objects and code keywords are highlighted using a special font. For example, java.lang.Object .
Code blocks, screen output, and command lines are placed in individual blocks with a special font:
//This is an example code block
Listing Chapter#.X Some code
WHAT YOU NEED TO KNOW BEFORE CONTINUING
This CodeNotes installment is for Java developers of all skill levels. Provided that you understand how to build, compile, and run a basic Java class, you should be able to follow the code examples in this book. However, several of the advanced topics require more background than can be compressed into this format. For these topics, you can usually find more material on the CodeNotes website.
If you are familiar with any other object-oriented programming language, such as C++, then you should be able to follow the ideas presented in the code examples, although specific details and code fragments may be unclear.
Chapter 1
INTRODUCTION
JAVA: INTERMEDIATE AND ADVANCED TOPICS
If you work with Java for any length of time, you will almost certainly encounter many of the concepts presented in this book. However, there is a difference between being able to use the language and actually understanding the language. The main purpose of this book is to help any beginning or intermediate Java programmer develop a firm understanding of both how and why the Java language works. For more advanced Java developers, this book contains many examples and design notes about how to use some of Javas most powerful features. As part of the CodeNotes philosophy, this book focuses on practical examples, including working with collections, creating and using threads, selecting the proper I/O classes, and designing an application that supports multiple languages. With the numerous code samples and extensive additional material on the CodeNotes website, this book will make almost anyone a better Java developer.
ROAD MAP
This book as a whole is fundamentally about why Java works the way it does. However, the individual chapters are devoted specifically to features found in the Java 2 Standard Edition (J2SE). The core of this book is divided into five main development chapters (Chapters 3 to 7), a short deployment chapter (Chapter 8), and The Dark Side (Chapter 9) for everything that was left out.
Chapter 3, Objects and Classes. Java is fundamentally an object-oriented language, which means that objects, encapsulation, and polymorphism play a major role in every single aspect of the language. At first glance, this chapter may seem like a basic topic. However, understanding why the Java language works the way it does requires a firm grasp of inheritance, interfaces, inner classes, and reflection. Although this chapter does explain the basic mechanics, it goes much further into the possibilities that are only available through object-oriented programming.
Chapter 4, Collections. Almost every developer is familiar with the concept of an array as a data-storage mechanism. However, arrays are not always useful. The Java Collections Framework offers a set of alternative classes for storing and managing collections of data. Building extensively on interfaces and inheritance, the Java Collections Framework provides an ideal example of how object-oriented programming works in practice.
Chapter 5, Java I/O. Very few programs operate without accepting some form of input or generating some form of output. Java provides an extensive framework for creating I/O systems of almost any description. Once again, this framework relies heavily on object-oriented programming principles, which is why it is often confusing to new programmers. Javas version of I/O has all of the same functionality as other I/O systems (such as C-style fopen() , fprintf() , and fclose() , or C++-style IOStream objects with >> pipes), although it is often hidden through abstraction and encapsulation.
Chapter 6, Threads. Java provides a surprisingly simple mechanism for creating and controlling multithreaded applications. However, this simplicity is often misleading, as a few lines of code can belie tremendous complexity. This chapter illustrates the basic process for creating and controlling threads and provides several examples for the common tools needed to control multithreaded programs (such as semaphores).
Chapter 7, Internationalization. As the international demand for computer software continues to expand, the need for multilanguage, multicountry interfaces becomes even more acute. Rather than developing a new application for each country and each language, you can take advantage of Javas many built-in features for customizing an application for a particular region. This chapter may not provide all of the answers, but it should open your eyes to the many challenges of creating international applications and provide enough of a background so that you can start the process on your own systems.
Next page