Table of Contents
Introduction 1
About This Book 1
Foolish Assumptions 2
Icons Used in This Book 3
Beyond the Book 4
Where to Go from Here 4
Part 1 Getting Started with Java 5
Chapter 1 All about Java 7
What You Can Do with Java 8
Why You Should Use Java 9
Gaining Perspective: Where Java Fits In 10
Object-Oriented Programming (OOP) 12
Object-oriented languages 13
Objects and their classes 15
What's so great about an object-oriented language? 15
Objects and classes are everywhere 18
What's Next? 19
Chapter 2 All about Software 21
Get Ready for Java 21
The Inside Scoop 23
What is a compiler? 24
What is a Java virtual machine? 27
Developing Software 31
Spoiler Alert! 32
Chapter 3 Using the Basic Building Blocks 33
Speaking the Java Language 33
The grammar and the common names 34
The words in a Java program 35
Checking Out Java Code for the First Time 37
Understanding a Simple Java Program 38
The Java class 38
The Java method 40
The main method in a program 41
How you finally tell the computer to do something 43
Brace yourself 45
And Now, a Few Comments 49
Adding comments to your code 49
What's Barry's excuse? 53
Using comments to experiment with your code 53
Part 2 Writing Your Own Java Programs 55
Chapter 4 Making the Most of Variables and Their Values 57
Varying a Variable 58
Assignment statements 60
The types of values that variables may have 61
How to hold the line 64
Numbers without decimal points 65
Combining declarations and initializing variables 67
Experimenting with JShell 69
What Happened to All the Cool Visual Effects? 72
The Atoms: Java's Primitive Types 73
The char type 73
The boolean type 76
The Molecules and Compounds: Reference Types 78
An Import Declaration 82
Creating New Values by Applying Operators 84
Initialize once, assign often 87
The increment and decrement operators 89
Assignment operators 93
Chapter 5 Controlling Program Flow with Decision-Making Statements 97
Making Decisions (Java if Statements) 98
Guess the number 98
She controlled keystrokes from the keyboard 99
Creating randomness 102
The if statement 103
Equal, equal 104
Brace yourself 105
Your intent to indent 105
Elseless in Helsinki 106
Using Blocks in JShell 108
Forming Conditions with Comparisons and Logical Operators 109
Comparing numbers; comparing characters 109
Comparing objects 110
Look! 113
Java's logical operators 115
Vive les nuls! 118
(Conditions in parentheses) 119
The Nesting Habits of if Statements 121
Choosing among Many Alternatives 124
Java's glorious switch statement 124
A switch in time saves 14 127
Your grandparents' switch statement 130
Free fall 131
Chapter 6 Controlling Program Flow with Loops 135
Repeating Instructions Over and Over Again (Java while Statements) 136
Count On Me 139
The anatomy of a for statement 141
The world premiere of "Al's All Wet' 142
You Can Always Get What You Want 145
The root of the matter 148
Reading a single character 149
File handling in Java 150
Block on the while side 151
Part 3 Working with the Big Picture: Object-Oriented Programming 155
Chapter 7 The Inside scOOP 157
Defining a Class (What It Means to Be an Account) 158
Declaring variables and creating objects 163
Initializing a variable 166
Using an object's fields 167
One program; several classes 167
Declaring a public class 167
Defining a Method within a Class (Displaying an Account) 169
An account that displays itself 170
The display method's header 172
Sending Values to and from Methods (Calculating Interest) 173
Passing a value to a method 176
Returning a value from the getInterest method 178
Giving Your Numbers a Makeover 180
Hide-and-Seek 185
Good programming 185
Public lives and private dreams: Making a field inaccessible 188
Enforcing rules with accessor methods 190
Barry's Own GUI Class 190
Chapter 8 Saving Time and Money: Reusing Existing Code 199
What It Means to Be an Employee 200
The last word on employees 200
Putting your class to good use 202
Cutting a check 206
Working with Disk Files (a Brief Detour) 207
Storing data in a file 207
Repeat after me 208
Reading from a file 209
Who moved my file? 212
You moved your file! 212
Reading a line at a time 213
Clean up after yourself 215
Defining Subclasses (What It Means to Be a Full-Time or Part-Time Employee) 216
Creating a subclass 218
Creating subclasses is habit-forming 220
Using Subclasses 221
Making types match 223
The second half of the story 224
Changing the Payments for Only Some of the Employees 225
A Java annotation 227
Using methods from classes and subclasses 228
Chapter 9 Constructing New Objects 233
Defining Constructors (What It Means to Be a Temperature) 234
What is a temperature? 235
What is a temperature scale? (Java's enum type) 235
Okay, so then what is a temperature? 236
What you can do with a temperature 238
Constructing a temperature; a slow-motion replay 240
Some things never change 243
Doing Something about the Weather 246
Building better temperatures 246
Constructors for subclasses 248
Using all this stuff 249
The default constructor 250
A Constructor That Does More 254
Classes and methods from the Java API 256
Live dangerously 257
Part 4 Smart Java Techniques 259
Chapter 10 Putting Variables and Methods Where They Belong 261
Defining a Class (What It Means to Be a Baseball Player) 262
Another way to beautify your numbers 263
Using the Player class 264
One class; nine objects 266
Don't get all GUI on me 266
Some facts about potatoes 268
Making Static (Finding the Team Average) 270
Why is there so much static? 272
Meet the static initializer 272
Displaying the overall team average 273
The static keyword is yesterday's news 276
Could cause static; handle with care 276
Experiments with Variables 279
Putting a variable in its place 280
Telling a variable where to go 283
Passing Parameters 287
Pass by value 288
Returning a result 290
Pass by reference 290
Returning an object from a method 292
Epilogue 294
Chapter 11 Using Arrays to Juggle Values 295
Getting Your Ducks All in a Row 295
Creating an array in two easy steps 298
How to book hotel guests 299
Tab stops and other special things 301
Make life easy for yourself 302
Stepping through an array with the enhanced for loop 304
Do you have a room? 306
Writing to a file 308
When to close a file 309
Arrays of Objects 311
Using the Room class 313
Yet another way to beautify your numbers 316
The conditional operator 317
How to Argue with Your Code 318
Settling the argument 319
Checking for the right number of program arguments 321
Chapter 12 Using Collections and Streams (When Arrays Aren't Good Enough) 323
Arrays Have Limitations 324
Collection Classes to the Rescue 325
Using an ArrayList 325
Using generics 328
Wrap it up 331
Are we done yet? 333
Once and again 333
So many collection classes! 335
Functional Programming 336
Problem-solving the old-fashioned way 337
Lambda expressions 339
A black sheep among the lambdas 341
A taxonomy of lambda expressions 342
The interpretation of streams 342
Why bother? 348
Method references 351
Chapter 13 Looking Good When Things Take Unexpected Turns 355
Garbage In 356
The parameter in a catch clause 360
Do it yourself 362
Who will catch the exception? 364
Catching two or more exceptions at a time 371
The Buck Stops Here, Except When it Doesn't 372
Catch it soon 374
Catch it later 375
Checked or unchecked? 377
Try, Try Again! 380
Chapter 14 Sharing Names among the Parts of a Java Program 385
Access Modifiers 386
Classes and Their Members 386
Public and Private Access for Members 387
Drawing on a frame 390
Putting a package in its place 392
Making a frame 394
Default Access for Members 396
Switching to Default access 398
Accessing default members within a package 400
Protected Access for Members 402
A class in one package and a subclass in another 402
Two classes in the same package 404
Access Modifiers for Java Classes 408
Public classes 408
Nonpublic classes 409
From Classes Come Modules 410
Chapter 15 Fancy Reference Types 411
Java's Types 411
The Java Interface 412
Declaring two interfaces 413
Implementing interfaces 415
Putting the pieces together 417
Abstract Classes 420
Caring for your pet 423
Using all your classes 425
Relax! You're Not Seeing Double! 427
Chapter 16 Java's Juggling Act 431
Juggling Two or More Calls 432
Events and event handling 435
Follow the thread 436
Don't miss this 437
Inside the actionPerformed method 439
Some Events Aren't Button Clicks 440
The Inner Sanctum 446
Chapter 17 Using Java Database Connectivity 451
Creating a^Database and a Table 452
Seeing what happens when you run the code 453
Using SQL commands 454
Connecting and disconnecting 455
Putting Data in the Table 456
Retrieving Data 457
Destroying Data 459
One Step Beyond 461
Part 5 The Part of Tens 463
Chapter 18 Ten Packs of Java Websites 465
This Book's Website 465
For Business Issues Related to This Book 465
Download the Java Development Kit 466
Your Grandparents' Java Download Site 466
The Horse's Mouth 466
Join Java User Groups 466
Find the Latest News about Java 466
Find News, Reviews, and Sample Code 466
Got a Technical Question about Anything? 467
Become Involved in the Future of Java 467
Chapter 19 Ten Bits of Advice for New Software Developers 469
How Long Does It Take to Learn Java? 470
Which of Your Books Should I Read? 470
Are Books Other than Yours Good for Learning Java and Android Development? 471
Which Computer Programming Language(s) Should I Learn? 471
Which Skills Other than Computer Coding Should i Learn? 471
How Should I Continue My Learning as a Software Developer? 472
How Else Should I Continue My Learning as a Developer? 472
How Can I Land a Job Developing Software? 473
I Still Don't Know What to Do with My Life 473
If I Have Other Questions, How Can i Contact You? 474
Index 475