As Java continues to evolve, this cookbook continues to grow in tandem with hundreds of hands-on recipes across a broad range of Java topics. Author Ian Darwin gets developers up to speed right away with useful techniques for everything from string handling and functional programming to network communication and AI.
If you're familiar with any release of Java, this book will bolster your knowledge of the language and its many recent changes, including how to apply them in your day-to-day development. Each recipe includes self-contained code solutions that you can freely use, along with a discussion of how and why they work.
Downloadable from GitHub, all code examples compile successfully. This updated edition covers changes up to Java 23 and most of Java 24. You will:
Learn how to apply many new and old Java APIs
Use the new language features in recent Java versions
Understand the code you're maintaining
Develop code using standard APIs and good practices
Explore the brave new world of current Java development
Ian Darwin has a lifetime of experience in the software industry, having worked with Java across many platforms and types of software, from Java's initial pre-release to the present, from desktop to enterprise to mobile.
1133658629
Java Cookbook
As Java continues to evolve, this cookbook continues to grow in tandem with hundreds of hands-on recipes across a broad range of Java topics. Author Ian Darwin gets developers up to speed right away with useful techniques for everything from string handling and functional programming to network communication and AI.
If you're familiar with any release of Java, this book will bolster your knowledge of the language and its many recent changes, including how to apply them in your day-to-day development. Each recipe includes self-contained code solutions that you can freely use, along with a discussion of how and why they work.
Downloadable from GitHub, all code examples compile successfully. This updated edition covers changes up to Java 23 and most of Java 24. You will:
Learn how to apply many new and old Java APIs
Use the new language features in recent Java versions
Understand the code you're maintaining
Develop code using standard APIs and good practices
Explore the brave new world of current Java development
Ian Darwin has a lifetime of experience in the software industry, having worked with Java across many platforms and types of software, from Java's initial pre-release to the present, from desktop to enterprise to mobile.
As Java continues to evolve, this cookbook continues to grow in tandem with hundreds of hands-on recipes across a broad range of Java topics. Author Ian Darwin gets developers up to speed right away with useful techniques for everything from string handling and functional programming to network communication and AI.
If you're familiar with any release of Java, this book will bolster your knowledge of the language and its many recent changes, including how to apply them in your day-to-day development. Each recipe includes self-contained code solutions that you can freely use, along with a discussion of how and why they work.
Downloadable from GitHub, all code examples compile successfully. This updated edition covers changes up to Java 23 and most of Java 24. You will:
Learn how to apply many new and old Java APIs
Use the new language features in recent Java versions
Understand the code you're maintaining
Develop code using standard APIs and good practices
Explore the brave new world of current Java development
Ian Darwin has a lifetime of experience in the software industry, having worked with Java across many platforms and types of software, from Java's initial pre-release to the present, from desktop to enterprise to mobile.
Ian Darwin has a lifetime of experience in the software industry, having worked with Java across many platforms and types of software, from Java’s initial pre-release to the present, from desktop to enterprise to mobile. A founding member of Sun/Oracle Java Champions advocacy group, Ian is also the author of the Android Cookbook and several smaller books on related tech. He’s written and taught courses for computer science undergraduates and for leading career development company Learning Tree International.
Table of Contents
Preface
xv
1.
Getting Started: Compiling, Running, and Debugging
1
1.1
Compiling and Running Java: JDK
1
1.2
Editing and Compiling with a Color-Highlighting Editor
3
1.3
Compiling, Running, and Testing with an IDE
4
1.4
Using CLASSPATH Effectively
11
1.5
Using the com.darwinsys API Classes from This Book
14
1.6
Compiling the Source Code Examples from This Book
15
1.7
Automating Compilation with Ant
16
1.8
Running Applets
17
1.9
Dealing with Deprecation Warnings
20
1.10
Conditional Debugging Without #ifdef
22
1.11
Debugging Printouts
24
1.12
Maintaining Program Correctness with Assertions
25
1.13
Debugging with JDB
26
1.14
Unit Testing: Avoid the Need for Debuggers
28
1.15
Getting Readable Tracebacks
30
1.16
Finding More Java Source Code
32
1.17
Program: Debug
33
2.
Interacting with the Environment
35
2.1
Getting Environment Variables
35
2.2
System Properties
37
2.3
Writing JDK Release-Dependent Code
39
2.4
Writing Operating System-Dependent Code
40
2.5
Using Extensions or Other Packaged APIs
42
2.6
Parsing Command-Line Arguments
43
3.
Strings and Things
50
3.1
Taking Strings Apart with Substrings
52
3.2
Taking Strings Apart with String Tokenizer
53
3.3
Putting Strings Together with +, StringBuilder (JDK 1.5), and StringBuffer
56
3.4
Processing a String One Character at a Time
59
3.5
Aligning Strings
60
3.6
Converting Between Unicode Characters and Strings
63
3.7
Reversing a String by Word or by Character
64
3.8
Expanding and Compressing Tabs
65
3.9
Controlling Case
70
3.10
Indenting Text Documents
71
3.11
Entering Nonprintable Characters
73
3.12
Trimming Blanks from the End of a String
74
3.13
Parsing Comma-Separated Data
75
3.14
Program: A Simple Text Formatter
80
3.15
Program: Soundex Name Comparisons
82
4.
Pattern Matching with Regular Expressions
85
4.1
Regular Expression Syntax
87
4.2
Using regexes in Java: Test for a Pattern
94
4.3
Finding the Matching Text
97
4.4
Replacing the Matched Text
99
4.5
Printing All Occurrences of a Pattern
100
4.6
Printing Lines Containing a Pattern
103
4.7
Controlling Case in Regular Expressions
104
4.8
Matching "Accented" or Composite Characters
105
4.9
Matching Newlines in Text
106
4.10
Program: Apache Logfile Parsing
108
4.11
Program: Data Mining
110
4.12
Program: Full Grep
112
5.
Numbers
117
5.1
Checking Whether a String Is a Valid Number
119
5.2
Storing a Larger Number in a Smaller Number
120
5.3
Converting Numbers to Objects and Vice Versa
121
5.4
Taking a Fraction of an Integer Without Using Floating Point
122
5.5
Ensuring the Accuracy of Floating-Point Numbers
123
5.6
Comparing Floating-Point Numbers
125
5.7
Rounding Floating-Point Numbers
127
5.8
Formatting Numbers
128
5.9
Converting Between Binary, Octal, Decimal, and Hexadecimal
130
5.10
Operating on a Series of Integers
131
5.11
Working with Roman Numerals
132
5.12
Formatting with Correct Plurals
136
5.13
Generating Random Numbers
138
5.14
Generating Better Random Numbers
139
5.15
Calculating Trigonometric Functions
140
5.16
Taking Logarithms
141
5.17
Multiplying Matrices
141
5.18
Using Complex Numbers
143
5.19
Handling Very Large Numbers
145
5.20
Program: TempConverter
147
5.21
Program: Number Palindromes
151
6.
Dates and Times
154
6.1
Finding Today's Date
155
6.2
Printing Date/Time in a Given Format
156
6.3
Representing Dates in Other Epochs
159
6.4
Converting YMDHMS to a Calendar or Epoch Seconds
160
6.5
Parsing Strings into Dates
161
6.6
Converting Epoch Seconds to DMYHMS
162
6.7
Adding to or Subtracting from a Date or Calendar
163
6.8
Difference Between Two Dates
165
6.9
Comparing Dates
165
6.10
Day of Week/Month/Year or Week Number
167
6.11
Creating a Calendar Page
168
6.12
Measuring Elapsed Time
171
6.13
Sleeping for a While
173
6.14
Program: Reminder Service
173
7.
Structuring Data with Java
176
7.1
Using Arrays for Data Structuring
177
7.2
Resizing an Array
178
7.3
Like an Array, but More Dynamic
180
7.4
Using Iterators for Data-Independent Access
181
7.5
Structuring Data in a Linked List
183
7.6
Mapping with Hashtable and HashMap
185
7.7
Storing Strings in Properties and Preferences
186
7.8
Sorting a Collection
190
7.9
Avoiding the Urge to Sort
193
7.10
Eschewing Duplication
195
7.11
Finding an Object in a Collection
196
7.12
Converting a Collection to an Array
198
7.13
Rolling Your Own Iterator
199
7.14
Stack
201
7.15
Multidimensional Structures
202
7.16
Finally, Collections
204
7.17
Program: Timing Comparisons
206
8.
Data Structuring with Generics, foreach, and Enumerations (JDK 1.5)
208
8.1
Using Generic Collections
209
8.2
Using "foreach" Loops
210
8.3
Avoid Casting by Using Generics
211
8.4
Let Java Convert with AutoBoxing and AutoUnboxing
214
8.5
Using Typesafe Enumerations
215
8.6
Program: MediaInvoicer
219
9.
Object-Oriented Techniques
222
9.1
Printing Objects: Formatting with toString()
224
9.2
Overriding the Equals Method
225
9.3
Overriding the hashCode Method
228
9.4
The Clone Method
229
9.5
The Finalize Method
231
9.6
Using Inner Classes
233
9.7
Providing Callbacks via Interfaces
234
9.8
Polymorphism/Abstract Methods
238
9.9
Passing Values
239
9.10
Enforcing the Singleton Pattern
242
9.11
Roll Your Own Exceptions
243
9.12
Program: Plotter
244
10.
Input and Output
247
10.1
Reading Standard Input
248
10.2
Writing Standard Output
252
10.3
Printing with the 1.5 Formatter
253
10.4
Scanning a File with Stream Tokenizer
257
10.5
Scanning Input with the 1.5 Scanner Class
262
10.6
Opening a File by Name
265
10.7
Copying a File
266
10.8
Reading a File into a String
269
10.9
Reassigning the Standard Streams
270
10.10
Duplicating a Stream as It Is Written
270
10.11
Reading/Writing a Different Character Set
273
10.12
Those Pesky End-of-Line Characters
274
10.13
Beware Platform-Dependent File Code
274
10.14
Reading "Continued" Lines
275
10.15
Binary Data
280
10.16
Seeking
281
10.17
Writing Data Streams from C
282
10.18
Saving and Restoring Java Objects
284
10.19
Preventing ClassCastExceptions with SerialVersionUID
287
10.20
Reading and Writing JAR or Zip Archives
289
10.21
Reading and Writing Compressed Files
292
10.22
Program: Text to PostScript
293
11.
Directory and Filesystem Operations
297
11.1
Getting File Information
297
11.2
Creating a File
300
11.3
Renaming a File
301
11.4
Deleting a File
302
11.5
Creating a Transient File
303
11.6
Changing File Attributes
305
11.7
Listing a Directory
306
11.8
Getting the Directory Roots
308
11.9
Creating New Directories
309
11.10
Program: Find
310
12.
Programming External Devices: Serial and Parallel Ports