Index
[] arrays
Index
[] binary data
Index
[] calculating Calendar objects Class object classes closing collections comparing converting creating
Index
[] databases connections date/time values directories discovering
Index
[] email MIME executing
Index
[] field values files Zip archives finding floating point numbers formatting
Index
[] generating
Index
[] handling HTTP
Index
[]
Index
[] Java email JDBC
Index
[] listing
Index
[] matched patterns matching methods
Index
[]
Index
[] obtaining operating system streams
Index
[] parsing XML performing
Index
[]
Index
[] reading textmatching text removing retrieving returning
Index
[] sending serialized data servers binary dataserialized data setting binary dataserialized data strings
Index
[]
Index
[] text transforming
Index
[]
Index
[]
Index
[] websites writing
Index
[]documents
Index
[] Zip archives
Introduction
Earlier this year, I was asked by an editor at Pearson to write this phrasebook focused on Java. It is one of several in their Phrasebook series. Christian Wenz wrote the first one in the series, the PHP Phrasebook . The concept for the phrasebook series comes from the foreign language phrasebooks. Foreign language phrasebooks contain a list of common phrases that you might want to say in the foreign language. These books are very useful for visitors who do not know the local language. The concept of technical phrasebooks is similar. They show the reader how to accomplish common tasks using the technology which is the subject of the book.
The goal of this phrasebook is to provide you with a guide book of commonly used Java programming phrases. This book should be useful to both the seasoned Java programmer and the programmer who is new to Java. Although the book can be read cover-to-cover to get a good overview of the Java language, the book is best used as an on-hand reference when the programmer wants to know how to accomplish a common task in the Java language. You may also wish to explore the book to discover some Java features and techniques that you may not have been aware of before.
The book is not a Java language tutorial or introduction, nor is it a complete reference to the Java language. There are many more classes and APIs than what you will find covered in this book. There are already many excellent learning Java style books and Java reference books covering pretty much every technology you can think of. If your goal is to get a deeper understanding of a specific technology, this is not the book you are looking for.
Most of the phrases and sample code shown in this book do not contain error handling code. Many of the phrases may throw exceptions that you will have to handle in any real applications that you write. The error and exception handling code is not shown in this book so that the reader can focus on the purpose of the phrase or sample code without being distracted by a lot of error handling code. When you include all of the standard exception handling in a code sample, the phrase can quickly become something much more than a short concise phrase, and yet showing all of the exception handling code will not have helped you to understand the specific phrase any better. Not showing the error handling code allows the phrases to be kept short and concise. The JavaDoc for the Java JDK is an excellent source for looking up which exceptions can be thrown by any method contained in the Java classes that you will come across in this book. You can access the JavaDoc at: http://java.sun.com/j2se/1.5.0/docs/api/.
The phrases contained in this book should be OS independent. The Java platform's motto of write-once-run-anywhere should apply to all of the phrases and sample code contained in this book. The phrases were tested under JDK 1.5, also referred to as Java 5.0. Most of the phrases will work well under earlier versions of the JDK as well, except where noted.
All of the phrases and sample code in this book have been tested and should be error-free. While my hope is that this book is 100% error-free, I do realize that most technical books don't quite meet that mark. Any errors and other errata that are found will be made available on www.samspublishing.com.
While writing this book, I have tried to come up with what I considered to be the most useful phrases while maintaining the concise format of the Phrasebook series. I am certain that at some point you will be looking for a certain phrase that you will not find in this book. If you feel that a phrase should be included in this book, I'd like to hear from you. Also, if you feel that there are phrases contained in this book which are less than useful, please tell me that as well. As a writer, I always enjoy feedback from my readers. Perhaps at some point in the future you will see a second edition of this book that takes your feedback into account. You can reach me through my website at www.timothyfisher.com.
There is more to Java, of course, than can be covered in a book this size. When you register this book at www.samspublishing.com/register, you will find additional material as well as any updates.
1. The Basics
This chapter contains the phrases that you will need to get you started in Java development. These are things that you need to know before you can accomplish much of anything in Java. The basics include compiling and running your Java code, and understanding the Java class path. Unlike languages such as PHP and Basic, Java source code must be compiled into what is called byte code before it can be executed. The compiler places the byte code into Java class files. Therefore, it is important for anyone programming in Java to understand how to compile their source code into class files, and then be able to execute those class files. Knowledge of the Java class path is important for both compiling and executing Java code. Therefore it is with these phrases that we start.
Today it is common to do Java development within an integrated development environment (IDE), such as the freely available Eclipse projectsee http://www.eclipse.org. This chapter assumes that you are performing the tasks at the command line. Although realistically, you might use an IDE for most of your development, every developer should be familiar with setting up and accomplishing these tasks outside the IDE. Performing these tasks within the IDE will vary with the IDE, and references for your particular IDE are the best place to go for that help.
To execute the phrases contained in this chapter, you should obtain a Java distribution from Sun. Sun makes Java technology available in several popular forms. The most common Java distributions are the Java Standard Edition (SE), the Java Enterprise Edition (EE), and the Java Micro Edition (ME). To complete all phrases in this book, you will only need the Java SE package. Java EE contains additional features for developing enterprise applications, and the Java ME is geared towards developing applications for devices such as cell phones and PDAs. All of these packages can be downloaded from the Sun Java website at http://java.sun.com. The J2SE 5.0 is the most recent version of the Java SE at the time of this writing. Unless you have a reason to use a previous version, this is the version you should use with this book. Within the J2SE 5.0, you will find two packages available for download, the JDK and the JRE. The JDK is the Java development kit and is what you will need to develop Java applications. The JRE is the Java runtime edition and will only allow you to run Java applications, but not build your own. So for this book you will need the JDK distribution of the J2SE 5.0 package.