Java Cookbook, Second Edition

( 2 )


You have a choice: you can wade your way through lengthy Java tutorials and figure things out by trial and error, or you can pick up Java Cookbook, 2nd Edition and get to the heart of what you need to know when you need to know it.With the completely revised and thoroughly updated Java Cookbook, 2nd Edition, Java developers like you will learn by example, try out new features, and use sample code to understand how new additions to the language and platform work—and how to put them to work for you.This ...

See more details below
Paperback (Second Edition)
$36.99 price
(Save 26%)$49.99 List Price
Other sellers (Paperback)
  • All (19) from $1.99   
  • New (4) from $20.26   
  • Used (15) from $1.99   
Sending request ...


You have a choice: you can wade your way through lengthy Java tutorials and figure things out by trial and error, or you can pick up Java Cookbook, 2nd Edition and get to the heart of what you need to know when you need to know it.With the completely revised and thoroughly updated Java Cookbook, 2nd Edition, Java developers like you will learn by example, try out new features, and use sample code to understand how new additions to the language and platform work—and how to put them to work for you.This comprehensive collection of problems, solutions, and practical examples will satisfy Java developers at all levels of expertise. Whether you're new to Java programming and need something to bridge the gap between theory-laden reference manuals and real-world programs or you're a seasoned Java programmer looking for a new perspective or a different problem-solving context, this book will help you make the most of your Java knowledge.Packed with hundreds of tried-and-true Java recipes covering all of the major APIs from the 1.4 version of Java, this book also offers significant first-look recipes for the most important features of the new 1.5 version, which is in beta release. You get practical solutions to everyday problems, and each is followed by a detailed, ultimately useful explanation of how and why the technology works.Java Cookbook, 2nd Edition includes code segments covering many specialized APIs—like those for working with Struts, Ant and other new popular Open Source tools. It also includes expanded Mac OS X Panther coverage and serves as a great launching point for Java developers who want to get started in areas outside of their specialization.In this major revision, you'll find succinct pieces of code that can be easily incorporated into other programs. Focusing on what's useful or tricky—or what's useful and tricky—Java Cookbook, 2nd Edition is the most practical Java programming book on the market.

Read More Show Less

Product Details

  • ISBN-13: 9780596007010
  • Publisher: O'Reilly Media, Incorporated
  • Publication date: 6/28/2004
  • Edition description: Second Edition
  • Edition number: 2
  • Pages: 864
  • Product dimensions: 7.14 (w) x 9.22 (h) x 1.54 (d)

Meet the Author

Ian F. Darwin has worked in the computer industry for three decades: with Unix since 1980, Java since 1995, and OpenBSD since 1998. He wrote the freeware file(1) command used on Linux and BSD and is the author of Checking C Programs with Lint, Java Cookbook, and over seventy articles and several courses (both university and commercial) on C and Unix. In addition to programming and consulting, Ian teaches Unix, C, and Java for Learning Tree International, one of the world's largest technical training companies. He runs OpenBSD on most of his computers, and he runs a mirror of The Unix History Society archive.

Read More Show Less

Table of Contents

Preface to the Second Edition;
Preface to the First Edition;
Who This Book Is For;
What's in This Book?;
Platform Notes;
Other Books;
Conventions Used in This Book;
Comments and Questions;
Getting the Source Code;
Chapter 1: Getting Started: Compiling, Running, and Debugging;
1.1 Introduction;
1.1 Compiling and Running Java: JDK;
1.2 Editing and Compiling with a Color-Highlighting Editor;
1.3 Compiling, Running, and Testing with an IDE;
1.4 Using CLASSPATH Effectively;
1.5 Using the com.darwinsys API Classes from This Book;
1.6 Compiling the Source Code Examples from This Book;
1.7 Automating Compilation with Ant;
1.8 Running Applets;
1.9 Dealing with Deprecation Warnings;
1.10 Conditional Debugging Without #ifdef;
1.11 Debugging Printouts;
1.12 Maintaining Program Correctness with Assertions;
1.13 Debugging with JDB;
1.14 Unit Testing: Avoid the Need for Debuggers;
1.15 Getting Readable Tracebacks;
1.16 Finding More Java Source Code;
1.17 Program: Debug;
Chapter 2: Interacting with the Environment;
2.1 Introduction;
2.1 Getting Environment Variables;
2.2 System Properties;
2.3 Writing JDK Release-Dependent Code;
2.4 Writing Operating System-Dependent Code;
2.5 Using Extensions or Other Packaged APIs;
2.6 Parsing Command-Line Arguments;
Chapter 3: Strings and Things;
3.1 Introduction;
3.1 Taking Strings Apart with Substrings;
3.2 Taking Strings Apart with StringTokenizer;
3.3 Putting Strings Together with +, StringBuilder (JDK 1.5), and StringBuffer;
3.4 Processing a String One Character at a Time;
3.5 Aligning Strings;
3.6 Converting Between Unicode Characters and Strings;
3.7 Reversing a String by Word or by Character;
3.8 Expanding and Compressing Tabs;
3.9 Controlling Case;
3.10 Indenting Text Documents;
3.11 Entering Nonprintable Characters;
3.12 Trimming Blanks from the End of a String;
3.13 Parsing Comma-Separated Data;
3.14 Program: A Simple Text Formatter;
3.15 Program: Soundex Name Comparisons;
Chapter 4: Pattern Matching with Regular Expressions;
4.1 Introduction;
4.1 Regular Expression Syntax;
4.2 Using regexes in Java: Test for a Pattern;
4.3 Finding the Matching Text;
4.4 Replacing the Matched Text;
4.5 Printing All Occurrences of a Pattern;
4.6 Printing Lines Containing a Pattern;
4.7 Controlling Case in Regular Expressions;
4.8 Matching "Accented" or Composite Characters;
4.9 Matching Newlines in Text;
4.10 Program: Apache Logfile Parsing;
4.11 Program: Data Mining;
4.12 Program: Full Grep;
Chapter 5: Numbers;
5.1 Introduction;
5.1 Checking Whether a String Is a Valid Number;
5.2 Storing a Larger Number in a Smaller Number;
5.3 Converting Numbers to Objects and Vice Versa;
5.4 Taking a Fraction of an Integer Without Using Floating Point;
5.5 Ensuring the Accuracy of Floating-Point Numbers;
5.6 Comparing Floating-Point Numbers;
5.7 Rounding Floating-Point Numbers;
5.8 Formatting Numbers;
5.9 Converting Between Binary, Octal, Decimal, and Hexadecimal;
5.10 Operating on a Series of Integers;
5.11 Working with Roman Numerals;
5.12 Formatting with Correct Plurals;
5.13 Generating Random Numbers;
5.14 Generating Better Random Numbers;
5.15 Calculating Trigonometric Functions;
5.16 Taking Logarithms;
5.17 Multiplying Matrices;
5.18 Using Complex Numbers;
5.19 Handling Very Large Numbers;
5.20 Program: TempConverter;
5.21 Program: Number Palindromes;
Chapter 6: Dates and Times;
6.1 Introduction;
6.1 Finding Today's Date;
6.2 Printing Date/Time in a Given Format;
6.3 Representing Dates in Other Epochs;
6.4 Converting YMDHMS to a Calendar or Epoch Seconds;
6.5 Parsing Strings into Dates;
6.6 Converting Epoch Seconds to DMYHMS;
6.7 Adding to or Subtracting from a Dateor Calendar;
6.8 Difference Between Two Dates;
6.9 Comparing Dates;
6.10 Day of Week/Month/Year or Week Number;
6.11 Creating a Calendar Page;
6.12 Measuring Elapsed Time;
6.13 Sleeping for a While;
6.14 Program: Reminder Service;
Chapter 7: Structuring Data with Java;
7.1 Introduction;
7.1 Using Arrays for Data Structuring;
7.2 Resizing an Array;
7.3 Like an Array, but More Dynamic;
7.4 Using Iterators for Data-Independent Access;
7.5 Structuring Data in a Linked List;
7.6 Mapping with Hashtable and HashMap;
7.7 Storing Strings in Properties and Preferences;
7.8 Sorting a Collection;
7.9 Avoiding the Urge to Sort;
7.10 Eschewing Duplication;
7.11 Finding an Object in a Collection;
7.12 Converting a Collection to an Array;
7.13 Rolling Your Own Iterator;
7.14 Stack;
7.15 Multidimensional Structures;
7.16 Finally, Collections;
7.17 Program: Timing Comparisons;
Chapter 8: Data Structuring with Generics, foreach, and Enumerations (JDK 1.5);
8.1 Introduction;
8.1 Using Generic Collections;
8.2 Using "foreach" Loops;
8.3 Avoid Casting by Using Generics;
8.4 Let Java Convert with AutoBoxing and AutoUnboxing;
8.5 Using Typesafe Enumerations;
8.6 Program: MediaInvoicer;
Chapter 9: Object-Oriented Techniques;
9.1 Introduction;
9.1 Printing Objects: Formatting with toString( );
9.2 Overriding the Equals Method;
9.3 Overriding the hashCode Method;
9.4 The Clone Method;
9.5 The Finalize Method;
9.6 Using Inner Classes;
9.7 Providing Callbacks via Interfaces;
9.8 Polymorphism/Abstract Methods;
9.9 Passing Values;
9.10 Enforcing the Singleton Pattern;
9.11 Roll Your Own Exceptions;
9.12 Program: Plotter;
Chapter 10: Input and Output;
10.1 Introduction;
10.1 Reading Standard Input;
10.2 Writing Standard Output;
10.3 Printing with the 1.5 Formatter;
10.4 Scanning a File with StreamTokenizer;
10.5 Scanning Input with the 1.5 Scanner Class;
10.6 Opening a File by Name;
10.7 Copying a File;
10.8 Reading a File into a String;
10.9 Reassigning the Standard Streams;
10.10 Duplicating a Stream as It Is Written;
10.11 Reading/Writing a Different Character Set;
10.12 Those Pesky End-of-Line Characters;
10.13 Beware Platform-Dependent File Code;
10.14 Reading "Continued" Lines;
10.15 Binary Data;
10.16 Seeking;
10.17 Writing Data Streams from C;
10.18 Saving and Restoring Java Objects;
10.19 Preventing ClassCastExceptions with SerialVersionUID;
10.20 Reading and Writing JAR or Zip Archives;
10.21 Reading and Writing Compressed Files;
10.22 Program: Text to PostScript;
Chapter 11: Directory and Filesystem Operations;
11.1 Introduction;
11.1 Getting File Information;
11.2 Creating a File;
11.3 Renaming a File;
11.4 Deleting a File;
11.5 Creating a Transient File;
11.6 Changing File Attributes;
11.7 Listing a Directory;
11.8 Getting the Directory Roots;
11.9 Creating New Directories;
11.10 Program: Find;
Chapter 12: Programming External Devices: Serial and Parallel Ports;
12.1 Introduction;
12.1 Choosing a Port;
12.2 Opening a Serial Port;
12.3 Opening a Parallel Port;
12.4 Resolving Port Conflicts;
12.5 Reading and Writing: Lock-Step;
12.6 Reading and Writing: Event-Driven;
12.7 Reading and Writing: Threads;
12.8 Program: Penman Plotter;
Chapter 13: Graphics and Sound;
13.1 Introduction;
13.1 Painting with a Graphics Object;
13.2 Testing Graphical Components;
13.3 Drawing Text;
13.4 Drawing Centered Text in a Component;
13.5 Drawing a Drop Shadow;
13.6 Drawing Text with 2D;
13.7 Drawing Text with an Application Font;
13.8 Drawing an Image;
13.9 Playing a Sound File;
13.10 Playing a Video Clip;
13.11 Printing in Java;
13.12 Program: PlotterAWT;
13.13 Program: Grapher;
Chapter 14: Graphical User Interfaces;
14.1 Introduction;
14.1 Displaying GUI Components;
14.2 Designing a Window Layout;
14.3 A Tabbed View of Life;
14.4 Action Handling: Making Buttons Work;
14.5 Action Handling Using Anonymous Inner Classes;
14.6 Terminating a Program with"Window Close";
14.7 Dialogs: When Later Just Won't Do;
14.8 Catching and Formatting GUI Exceptions;
14.9 Getting Program Output into a Window;
14.10 Choosing a Value with JSpinner;
14.11 Choosing a File with JFileChooser;
14.12 Choosing a Color;
14.13 Formatting JComponents with HTML;
14.14 Centering a Main Window;
14.15 Changing a Swing Program's Look and Feel;
14.16 Enhancing Your GUI for Mac OS X;
14.17 Program: Custom Font Chooser;
14.18 Program: Custom Layout Manager;
Chapter 15: Internationalization and Localization;
15.1 Introduction;
15.1 Creating a Button with I18N Resources;
15.2 Listing Available Locales;
15.3 Creating a Menu with I18N Resources;
15.4 Writing Internationalization Convenience Routines;
15.5 Creating a Dialog with I18N Resources;
15.6 Creating a Resource Bundle;
15.7 Extracting Strings from Your Code;
15.8 Using a Particular Locale;
15.9 Setting the Default Locale;
15.10 Formatting Messages;
15.11 Program: MenuIntl;
15.12 Program: BusCard;
Chapter 16: Network Clients;
16.1 Introduction;
16.1 Contacting a Server;
16.2 Finding and Reporting Network Addresses;
16.3 Handling Network Errors;
16.4 Reading and Writing Textual Data;
16.5 Reading and Writing Binary Data;
16.6 Reading and Writing Serialized Data;
16.7 UDP Datagrams;
16.8 Program: TFTP UDP Client;
16.9 Program: Telnet Client;
16.10 Program: Chat Client;
Chapter 17: Server-Side Java: Sockets;
17.1 Introduction;
17.1 Opening a Server for Business;
17.2 Returning a Response (String or Binary);
17.3 Returning Object Information;
17.4 Handling Multiple Clients;
17.5 Serving the HTTP Protocol;
17.6 Securing a Web Server with SSL and JSSE;
17.7 Network Logging;
17.8 Network Logging with log4j;
17.9 Network Logging with JDK 1.4;
17.10 Finding Network Interfaces;
17.11 Program: A Java Chat Server;
Chapter 18: Network Clients II: Applets and Web Clients;
18.1 Introduction;
18.1 Embedding Java in a Web Page;
18.2 Applet Techniques;
18.3 Contacting a Server on the Applet Host;
18.4 Making an Applet Show a Document;
18.5 Making an Applet Run JavaScript;
18.6 Making an Applet Run a CGI Script;
18.7 Reading the Contents of a URL;
18.8 URI, URL, or URN?;
18.9 Extracting HTML from a URL;
18.10 Extracting URLs from a File;
18.11 Converting a Filename to a URL;
18.12 Program: MkIndex;
18.13 Program: LinkChecker;
Chapter 19: Java and Electronic Mail;
19.1 Introduction;
19.1 Sending Email: Browser Version;
19.2 Sending Email: For Real;
19.3 Mail-Enabling a Server Program;
19.4 Sending MIME Mail;
19.5 Providing Mail Settings;
19.6 Sending Mail Without Using JavaMail;
19.7 Reading Email;
19.8 Program: MailReaderBean;
19.9 Program: MailClient;
Chapter 20: Database Access;
20.1 Introduction;
20.1 Easy Database Access with JDO;
20.2 Text-File Databases;
20.3 DBM Databases;
20.4 JDBC Setup and Connection;
20.5 Connecting to a JDBC Database;
20.6 Sending a JDBC Query and Getting Results;
20.7 Using JDBC Prepared Statements;
20.8 Using Stored Procedures with JDBC;
20.9 Changing Data Using a ResultSet;
20.10 Storing Results in a RowSet;
20.11 Changing Data Using SQL;
20.12 Finding JDBC Metadata;
20.13 Program: SQLRunner;
Chapter 21: XML;
21.1 Introduction;
21.1 Generating XML from Objects;
21.2 Transforming XML with XSLT;
21.3 Parsing XML with SAX;
21.4 Parsing XML with DOM;
21.5 Verifying Structure with a DTD;
21.6 Generating Your Own XML with DOM;
21.7 Program: xml2mif;
Chapter 22: Distributed Java: RMI;
22.1 Introduction;
22.1 Defining the RMI Contract;
22.2 Creating an RMI Client;
22.3 Creating an RMI Server;
22.4 Deploying RMI Across a Network;
22.5 Program: RMI Callbacks;
22.6 Program: NetWatch;
Chapter 23: Packages and Packaging;
23.1 Introduction;
23.1 Creating a Package;
23.2 Documenting Classes with Javadoc;
23.3 Beyond JavaDoc: Annotations/Metadata (JDK 1.5) and XDoclet;
23.4 Archiving with jar;
23.5 Running an Applet from a JAR;
23.6 Running an Applet with a Modern JDK;
23.7 Running a Main Program from a JAR;
23.8 Preparing a Class as a JavaBean;
23.9 Pickling Your Bean into a JAR;
23.10 Packaging a Servlet into a WAR File;
23.11 "Write Once, Install Anywhere";
23.12 "Write Once, Install on Mac OS X";
23.13 Java Web Start;
23.14 Signing Your JAR File;
Chapter 24: Threaded Java;
24.1 Introduction;
24.1 Running Code in a Different Thread;
24.2 Displaying a Moving Image with Animation;
24.3 Stopping a Thread;
24.4 Rendezvous and Timeouts;
24.5 Synchronizing Threads with the synchronized Keyword;
24.6 Simplifying Synchronization with 1.5 Locks;
24.7 Synchronizing Threads with wait( ) and notifyAll( );
24.8 Simplifying Producer-Consumer with the 1.5 Queue Interface;
24.9 Background Saving in an Editor;
24.10 Program: Threaded Network Server;
24.11 Simplifying Servers Using the Concurrency Utilities (JDK 1.5);
Chapter 25: Introspection, or "A Class Named Class";
25.1 Introduction;
25.1 Getting a Class Descriptor;
25.2 Finding and Using Methods and Fields;
25.3 Loading and Instantiating a Class Dynamically;
25.4 Constructing a Class from Scratch;
25.5 Performance Timing;
25.6 Printing Class Information;
25.7 Program: CrossRef;
25.8 Program: AppletViewer;
Chapter 26: Using Java with Other Languages;
26.1 Introduction;
26.1 Running a Program;
26.2 Running a Program and Capturing Its Output;
26.3 Mixing Java and Scripts with BSF;
26.4 Marrying Java and Perl;
26.5 Blending in Native Code (C/C++);
26.6 Calling Java from Native Code;
26.7 Program: DBM;
Chapter : Afterword;

Read More Show Less

Customer Reviews

Average Rating 4.5
( 2 )
Rating Distribution

5 Star


4 Star


3 Star


2 Star


1 Star


Your Rating:

Your Name: Create a Pen Name or

Barnes & Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation


  • - By submitting a review, you grant to Barnes & and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Terms of Use.
  • - Barnes & reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously
Sort by: Showing all of 2 Customer Reviews
  • Anonymous

    Posted October 3, 2004

    Excellent resource

    This is an excellent book¿it manages to make itself appropriate for both introductory and experienced Java developers. If you¿ve ever looked at any of the books in O¿Reilly¿s ¿Cookbook¿ series, you¿ll know that the book is organized into ¿recipes¿, which illustrate how to accomplish various development tasks in Java. Many of the beginning recipes are pretty basic, but will be used by developers new to Java to get started. Recipes include setting up the IDE to work correctly, working with a debugger, and even an introduction to JUnit. The next few sections of recipes are similar to what you would expect from any ¿Learn to Program in Language¿ book. There is a discussion on strings and string use, numbers, and date time values. As the book progresses, the recipes become increasingly technical. I was quite impressed to see a discussion of Generics in chapter 8, which were added to 1.5. Additional technical recipes include I/O, graphics and sound, and using sockets in Java. The author is able to successfully discuss a lot of different topics in a very clear and concise way. Additionally, I was both pleased and surprised to see the author include brief discussions on software patterns and agile methodologies, both of which are very much coming into vogue. Naturally, this book isn¿t easy to read from cover to cover, but it was never designed that way. Much like an actual cookbook, it is designed to best be used as a reference manual. Experienced non-Java programmers will benefit from this book, which can be used as a good tutorial to learn the Java language quickly. Java programmers will also benefit greatly from this book, and will likely want to have it next to their computer as a reference manual. I would highly recommend this book to anyone either wanting to learn Java or anyone looking for a good Java reference book.

    Was this review helpful? Yes  No   Report this review
  • Anonymous

    Posted July 2, 2004

    Keeps up the style of the first edition

    [A review of the SECOND EDITION, 2004] The salient feature that distinguishes this second edition from its predecessor is the coverage of Java 1.5. The overall format of the book is unchanged. There are over 100 'hacks' that address common problems a Java programmer might face. The grouping of hacks into chapters is quite logical. But you are expected to already know the basics of Java programming. This book is not meant to teach that, but to help fill in gaps in your overall knowledge framework. The solutions are typically easy to understand. That is the tenor of the book. You can quickly see if a solution fits your needs and then easily apply it. The 1.5 features are sprinkled throughout the chapters. Because the numerous changes from 1.4 are distributed over many aspects of the language. So Darwin correctly chose not to aggregate these into one location. Which also means that this book is not the place to learn specifically about 1.5 as a whole. (Try 'Java 1.5 Tiger' by McLaughlin and Flanagan.)

    Was this review helpful? Yes  No   Report this review
Sort by: Showing all of 2 Customer Reviews

If you find inappropriate content, please report it to Barnes & Noble
Why is this product inappropriate?
Comments (optional)