Table of Contents
Preface xiii
Chapter 1 Introduction to Java 1
A Very Brief Introduction to Java 2
Java Release Dates 2
Downloading a Java Release (Short Version) 2
Selecting a Version of Java (detailed version) 3
Java 8 and Java 11 3
Java Version Numbers 3
JRE Versus a JDK 4
Java Distributions 4
Java IDEs 4
Java Data Types, Operators, and their Precedence 5
Java Comments 5
Java Operators 6
Precedence of Java Operators 6
Creating and Compiling Java Classes 8
A "Hello World" Example 10
Working with Numbers 10
Working with Other Bases 11
Working with Random Numbers 12
Working with Built-in Math Functions 13
Working with Built-in Trigonometric Functions 14
Working with Bitwise Operators 14
The Java String Class 16
Characters, Arrays, and Strings in Java 16
Java Strings with Metacharacters 17
The Java new Operator 19
Equality of Strings 20
Comparing Strings 22
Searching for a Substring in Java 22
Useful String Methods in Java 23
Parsing Strings in Java 24
Overriding the Java to String() Method (Optional) 25
Autoboxing and Unboxing (Optional) 26
Summary 28
Chapter 2 Boolean Logic, Unicode, User Input 29
Conditional Logic in Java 29
Working with Boolean Expressions 31
Working with Dates 33
The Current Date and Time 33
Date Comparison 33
Date Formatting with SimpleDateFonnat 34
Parsing Strings as Dates 34
Working with Unicode, i18ns and i10n 35
Working with Base64 38
Working with Exceptions in Java 39
Checked Exceptions 40
Unchecked Exceptions 40
The throws Keyword 41
The Throwable Class 41
Getting User Input and Handling Exceptions 41
Parsing User Input and Handling Exceptions 44
Nested Exceptions 46
A try/catch/finally Example 47
Summary 48
Chapter 3 Methods and Constructors 51
What is a Java Class? 51
Public, Private, or Protected? 52
The Scope of Java Variables 52
Static Methods 54
Other Static Types 55
What are Accessors and Mutators? 56
A Java Class Without Accessors 57
Refactoring Code with Accessor Methods 58
Defining a Constructor and an Accessor 60
Accessors, Mutators, and the this Keyword 62
Arrays of Classes 64
Static Methods 66
Static Code Blocks 68
A Second "Hello World" Example 69
A Third "Hello World" Example 70
A Fourth "Hello World" Example 71
A Fifth "Hello World" Example 72
Java Anonymous Inner Classes 74
Java Named Inner Classes 75
Anonymous Classes Versus Named Inner Classes 76
Limitations of Anonymous Classes 76
Private Constructors (Why?) 76
The Java import Statement 78
The Java package Statement 79
An Example of the package Statement (1) 79
An Example of the package Statement (2) 82
Creating Java JAR Files for Java Class Files 86
Summary 87
Chapter 4 Loops, Arrays, and Recursion 89
Working with for Loops 89
Determining Leap Years 90
Java for Loops with Integers 92
Checking for Palindromes 93
Nested Loops 94
Goldbach's Conjecture 95
Variations of a Basic for Loop 97
Working with while Loops 98
Finding the Divisors of a Number 99
Working with do-while Loops 100
Working with a Java switch Statement 101
Java Arrays 102
Working with Arrays of Numbers 103
The Bubble Sort Algorithm 105
Java Multi-Dimensional Arrays 106
Java Multi-Dimensional Arrays (2) 108
Working with Characters and Strings 110
Working with Arrays of Strings 111
Displaying Command Line Arguments 112
Randomly Accessing Arrays of Strings 113
The StringBuilder and StringBuffer Classes 114
Java Example with StringBuilder and StringBuffer Classes 115
What is Recursion? 117
Adding Integers 118
Powers of Two 118
Factorial Values 118
Fibonacci Numbers 119
Calculating Factorial Values 119
Calculating the GCD of Two Numbers 121
Counting Digits in Integers 122
Adding Digits in Integers 123
Reverse a String via Recursion 125
Summary 125
Chapter 5 Introduction to OOP 127
Java Constructors 128
Overloading Java Methods 129
Public, Protected, and Private Keywords 129
Identity and Equality are Different Concepts 131
A Quick Introduction to Inheritance 134
The final Modifier with Java Methods and Classes 136
Working with Value Objects in Java 136
An Example of a Value Object in Java 137
What is a Java Interface? 139
Extending Interfaces 139
Java Interfaces and Abstract Classes 140
A Java Class That Implements an Interface 140
Java Abstract Methods and Classes 142
An Abstract Class in Java 143
Are Subclasses Always Concrete Classes in Java? 144
Method Arguments and Interfaces 144
What is OOP? 144
Inheritance 145
The Java Object Class 146
Inheritance and Overriding Methods 146
Encapsulation 147
Polymorphism 147
Summary 153
Chapter 6 Data Structures 155
Java Legacy Data Structures 155
Iterators 156
What is an Iterator? 156
Active versus Passive 156
Enumeration: Oldest Style Iteration 156
Iterator: Second Style Iteration 157
Generics: Third Style Iteration 157
ForEach: Fourth Style Iteration 157
Streams: Fifth Style Iteration 158
The Collection Interfaces 158
The Collection Classes 159
Overview of Java Collections 160
Iterating over Wrapper-Synchronized Collections 160
Convenience Implementations 161
List View of an Array 161
Immutable Multiple-Copy List 162
The ArrayList Class 162
The HashMap Class 164
The HashSet Class 166
The LinkedList Class 167
The Queue Class 168
The TreeSet Class 169
The Collections Class 171
Linked Lists of Objects 172
Generic Methods 175
Generic Classes 177
Counting Distinct Characters 177
Java 8 Enhancements for Collections 179
Summary 180
Chapter 7 Streams and Files 181
Working with Streams in Java 181
The Abstract Class InputStream 183
The FileInputStream Class 183
The FilterInputStream Class 184
The BufferedInputStream Class 184
The DataInputStream Class 185
Reading User Input from the Console 185
Reading Line-Oriented User Input 185
Reading Character-Oriented User Input 186
Reading a Text File 187
Reading a CSV File 189
Writing Data to a Text File 191
The FileWriter Class 192
The PriritWriter Class 193
The PrintStream Class 193
Counting Words and Characters in a File 194
Search for a String in a File 195
The BufferedWriter Class 196
The BufferedWriter Class with try-with-resources (Java 7+) 197
Working with Directories 198
Serialization 199
Deserialization 201
Reflection 202
Annotations 203
Summary 204
Chapter 8 Java and SQL 205
What is SQL? 205
What is DCL? 206
What is DDL? 206
Delete vs. Drop vs. Truncate 206
What is DQL? 207
What is DML? 207
What is TCL? 208
Working with MySQL 208
Logging into MySQL 209
Creating a MySQL Database 209
Creating and Dropping Tables 209
Manually Creating MySQL Tables 209
Working with Simple SELECT Statements 211
The EXISTS Keyword 212
The LIMIT Keyword 213
DELETE, TRUNCATE, and DROP in SQL 213
More Options for the DELETE Statement in SQL 213
What is a SQL Index? 214
Types of Indexes 214
Creating an Index 215
Overhead of Indexes 215
Considerations for Defining Indexes 216
Connecting to MySQL in Java 217
Drop and Recreate a MySQL Table 218
Insert Data into a MySQL Table 220
Select Data from a MySQL Table 222
Next Steps 223
Java Threads and Sockets 223
Java Networking 224
Reflection, GC, and Classloaders 224
Summary 224
Index 225