Introduction 1
About This Book 2
Foolish Assumptions 2
Icons Used in This Book 3
Beyond the Book 4
Where to Go from Here 4
Book 1: Java Basics 5
Chapter 1: Welcome to Java 7
What Is Java, and Why Is It So Great? 8
Platform independence 8
Object orientation 9
The Java API 10
The Internet 10
Java versus Other Languages 11
Important Features of the Java Language 12
Type checking 13
Exception handling 14
On the Downside: Java's Weaknesses 15
Java Version Insanity 16
What's in a Name? 18
Chapter 2: Installing and Using Java Tools 19
Downloading and Installing the Java Development Kit 20
Downloading the JDK 20
Installing the JDK 21
Perusing the JDK folders 21
Setting the path 22
Using Java's Command-Line Tools 24
Compiling a program 24
Compiling more than one file 25
Using Java compiler options 26
Running a Java program 28
Using the javap command 28
Using Java Documentation 30
Chapter 3: Working with Text Pad 33
Downloading and Installing TextPad 33
Editing Source Files 35
Compiling a Program 37
Running a Java Program 38
Book 2: Programming Basics 41
Chapter 1: Java Programming Basics 43
Looking at the Infamous Hello, World! Program 44
Dealing with Keywords 47
Working with Statements 49
Types of statements 49
White space 50
Working with Blocks 52
Creating Identifiers 53
Crafting Comments 54
End-of-line comments 54
Traditional comments 54
JavaDoc comments 55
Introducing Object-Oriented Programming 56
Understanding classes and objects 56
Understanding static methods 56
Creating an object from a class 57
Viewing a program that uses an object 58
So what's the difference? 60
Importing Java API Classes 61
Chapter 2: Working with Variables and Data Types 63
Declaring Variables 64
Declaring two or more variables in one statement 65
Declaring class variables 65
Declaring instance variables 66
Declaring local variables 67
Initializing Variables 68
Initializing variables with assignment statements 69
Initializing variables with initializers 70
Using Final Variables (Constants) 70
Working with Primitive Data Types 71
Integer types 72
Floating-point types 74
The char type 76
The Boolean type 77
Using wrapper classes 78
Using reference types 78
Using inferred variable types 80
Working with Strings 81
Declaring and initializing strings 82
Combining strings 82
Converting primitives to strings 83
Converting strings to primitives 84
Converting and Casting Numeric Data 85
Automatic conversions 85
Type casting 86
Thinking Inside the Box 87
Understanding Scope 87
Shadowing Variables 89
Printing Data with System out 90
Using standard input and output streams 91
Using System out and System err 92
Getting Input with the Scanner Class 93
Importing the Scanner class 94
Declaring and creating a Scanner object 94
Getting input 94
Getting Input with the JOptionPane Class 96
Using enum to Create Your Own Data Types 98
Chapter 3: Working with Numbers and Expressions 99
Working with Arithmetic Operators 99
Dividing Integers 102
Combining Operators 104
Using the Unary Plus and Minus Operators 105
Using Increment and Decrement Operators 106
Using the Assignment Operator 108
Using Compound Assignment Operators 110
Using the Math Class 111
Using constants of the Math class 112
Working with mathematical functions 113
Creating random numbers 116
Rounding functions 119
Formatting Numbers 121
Recognizing Weird Things about Java Math 123
Integer overflow 123
Floating-point weirdness 124
Division by zero 125
Chapter 4: Making Choices 129
Using Simple Boolean Expressions 130
Using if Statements 132
Simple if statements 132
if-else statements 135
Nested if statements 136
else-if statements 140
Using Mr Spock's Favorite Operators (Logical Ones, of Course) 142
Using the ! operator 142
Using the & and && operators 144
Using the | and || operators 145
Using the ^ operator 146
Combining logical operators 147
Using the Conditional Operator 148
Comparing Strings 149
Chapter 5: Going Around in Circles (Or, Using Loops) 151
Using Your Basic while Loop 152
The while statement 152
A counting loop 152
Breaking Out of a Loop 154
Looping Forever 154
Letting the user decide when to quit 156
Letting the user decide in another way 157
Using the continue Statement 158
Running do-while Loops 159
Validating Input from the User 161
Using the Famous for Loop 164
Understanding the formal format of the for loop 164
Scoping out the counter variable 166
Counting even numbers 167
Counting backward 168
Using for loops without bodies 169
Ganging up your expressions 170
Omitting expressions 172
Breaking and continuing your for loops 172
Nesting Your Loops 173
A simple nested for loop 174
A guessing game 174
Chapter 6: Pulling a Switcheroo 179
Battling else-if Monstrosities 179
Viewing an example else-if program 180
Creating a better version of the example program 181
Using the switch Statement 183
Viewing a boring switch example, complete with flowchart 184
Putting if statements inside switch statements 186
Creating Character Cases 187
Intentionally Leaving Out a Break Statement 188
Switching with Strings 192
Enhanced Switch Features with Java 13 193
Chapter 7: Adding Some Methods to Your Madness 197
The Joy of Methods 198
The Basics of Making Methods 198
An example 200
Another example 201
Methods That Return Values 203
Declaring the method's return type 203
Using the return statement to return the value 204
Using a method that returns a type 205
You gotta have a proper return statement 205
Trying another version of the guessing-game program 207
Methods That Take Parameters 210
Declaring parameters 211
Scoping out parameters 212
Understanding pass-by-value 213
Trying yet another version of the guessing-game program 214
Chapter 8: Handling Exceptions 217
Understanding Exceptions 218
Witnessing an exception 219
Finding the culprit 220
Catching Exceptions 221
A simple example 222
Another example 222
Handling Exceptions with a Preemptive Strike 224
Catching All Exceptions at Once 226
Displaying the Exception Message 227
Using a finally Block 228
Handling Checked Exceptions 231
Viewing the catch-or-throw compiler error 232
Catching FileNotFoundException 232
Throwing the FileNotFoundException 233
Throwing an exception from main 234
Swallowing exceptions 234
Throwing Your Own Exceptions 236
Book 3: Object-oriented Programming 239
Chapter 1: Understanding Object-Oriented Programming 241
What Is Object-Oriented Programming? 242
Understanding Objects 243
Objects have identity 243
Objects have type 244
Objects have state 245
Objects have behavior 246
Understanding the Life Cycle of an Object 247
Working with Related Classes 248
Inheritance 248
Interfaces 249
Designing a Program with Objects 250
Diagramming Classes with UML 251
Drawing classes 252
Drawing arrows 253
Chapter 2: Making Your Own Classes 255
Declaring a Class 256
Picking class names 256
Knowing what goes in the class body 257
Seeing where classes go 258
Working with Members 259
Understanding fields 259
Understanding instance methods 260
Understanding visibility 261
Using Getters and Setters 261
Overloading Methods 264
Creating Constructors 266
Creating basic constructors 266
Creating default constructors 267
Calling other constructors 268
Finding More Uses for the this Keyword 270
Using Initializers 271
Using Records 273
Chapter 3: Working with Statics 275
Understanding Static Fields and Methods 275
Working with Static Fields 276
Using Static Methods 277
Counting Instances 278
Preventing Instances 281
Using Static Initializers 282
Chapter 4: Using Subclasses and Inheritance 285
Introducing Inheritance 285
Motorcycles, trains, and automobiles 287
Game play 287
A businesslike example 288
Inheritance hierarchies 288
Creating Subclasses 289
Overriding Methods 291
Protecting Your Members 292
Using this and super in Your Subclasses 293
Understanding Inheritance and Constructors 294
Using final 295
Final methods 296
Final classes 296
Casting Up and Down 297
Determining an Object's Type 299
Poly What? 300
Creating Custom Exceptions 302
Tracing the Throwable hierarchy 302
Creating an exception class 304
Throwing a custom exception 305
Chapter 5: Using Abstract Classes and Interfaces 307
Using Abstract Classes 307
Using Interfaces 310
Creating a basic interface 311
Implementing an interface 312
Using an interface as a type 313
More Things You Can Do with Interfaces 314
Adding fields to an interface 314
Extending interfaces 315
Using interfaces for callbacks 316
Using Additional Interface Method Types 320
Chapter 6: Using the Object and Class Classes 323
The Mother of All Classes: Object 323
Every object is an Object 324
Object as a type 324
Methods of the Object class 325
Primitives aren't objects 326
The toString Method 327
Using toString 327
Overriding toString 328
The equals Method 330
Using equals 331
Overriding the equals method 332
The clone Method 336
Implementing the clone method 336
Using clone to create a shallow copy 339
Creating deep copies 341
The Class Class 346
Chapter 7: Using Inner Classes, Anonymous Classes, and Lambda Expressions 349
Declaring Inner Classes 350
Understanding inner classes 350
Viewing an example 351
Using Static Inner Classes 354
Using Anonymous Inner Classes 355
Creating an anonymous class 356
Creating a program with an anonymous class 357
Using Lambda Expressions 359
Chapter 8: Working with Packages and the Java Module System 361
Working with Packages 362
Importing classes and packages 362
Creating your own packages 363
An example 365
Putting Your Classes in a JAR File 366
jar command-line options 366
Archiving a package 367
Adding a jar to your classpath 368
Running a program directly from an archive 369
Using Javadoc to Document Your Classes 370
Adding Javadoc comments 371
Using the javadoc command 373
Viewing Javadoc pages 374
Using the Java Module System 375
Understanding modules 376
The module-info java file 377
Setting up folders for a module 378
Compiling a module 379
Creating a modular JAR file 379
Running a modular JAR file 380
Book 4: Strings, Arrays, and Collections 381
Chapter 1: Working with Strings 383
Reviewing Strings 384
Using the String Class 386
Finding the length of a string 388
Making simple string modifications 389
Extracting characters from a string 389
Extracting substrings from a string 390
Splitting a string 392
Replacing parts of a string 394
Using the StringBuilder and StringBuffer Classes 395
Creating a StringBuilder object 396
Using StringBuilder methods 396
Viewing a StringBuilder example 398
Using the CharSequence Interface 399
Chapter 2: Using Arrays 401
Understanding Arrays 401
Creating Arrays 402
Initializing an Array 404
Using for Loops with Arrays 404
Solving Homework Problems with Arrays 405
Using the Enhanced for Loop 408
Using Arrays with Methods 409
Using Varargs 410
Using Two-Dimensional Arrays 411
Creating a two-dimensional array 412
Accessing two-dimensional array elements 413
Initializing a two-dimensional array 414
Using jagged arrays 415
Going beyond two dimensions 416
Working with a Fun but Complicated Example: A Chessboard 417
Using the Arrays Class 425
Filling an array 426
Copying an array 427
Sorting an array 428
Searching an array 429
Comparing arrays 429
Converting arrays to strings 430
Chapter 3: Using the ArrayList Class 431
Understanding the ArrayList Class 432
Creating an ArrayList Object 435
Adding Elements 436
Accessing Elements 437
Printing an ArrayList 438
Using an Iterator 438
Updating Elements 440
Deleting Elements 442
Chapter 4: Using the LinkedList Class 445
Understanding the LinkedList Class 446
Creating a LinkedList 450
Adding Items to a LinkedList 450
Retrieving Items from a LinkedList 452
Updating LinkedList Items 454
Removing LinkedList Items 455
Chapter 5: Creating Generic Collection Classes 457
Why Generics? 458
Creating a Generic Class 459
A Generic Stack Class 461
Using Wildcard-Type Parameters 464
A Generic Queue Class 466
Chapter 6: Using Bulk Data Operations with Collections 471
Looking At a Basic Bulk Data Operation 473
Looking Closer at the Stream Interface 475
Using Parallel Streams 478
Book 5: Programming Techniques 481
Chapter 1: Programming Threads 483
Understanding Threads 484
Creating a Thread 485
Understanding the Thread class 485
Extending the Thread class 486
Creating and starting a thread 488
Implementing the Runnable Interface 488
Using the Runnable interface 489
Creating a class that implements Runnable 489
Using the CountDownApp class 491
Creating Threads That Work Together 493
Using an Executor 497
Synchronizing Methods 499
Creating a Lock 503
Coping with Threadus Interruptus 505
Finding out whether you've been interrupted 505
Aborting the countdown 506
Chapter 2: Using Regular Expressions 511
Creating a Program for Experimenting with Regular Expressions 512
Performing Basic Character Matching 515
Matching single characters 515
Using predefined character classes 515
Using custom character classes 518
Using ranges 519
Using negation 520
Matching multiple characters 520
Using escapes 521
Using parentheses to group characters 522
Using the pipe symbol 523
Using Regular Expressions in Java Programs 524
Understanding the String problem 524
Using regular expressions with the String class 525
Using the Pattern and Matcher classes 526
Chapter 3: Using Recursion 529
Calculating the Classic Factorial Example 529
The nonrecursive solution 530
The recursive solution 530
Displaying Directories 532
Writing Your Own Sorting Routine 536
Understanding how Quicksort works 536
Using the sort method 537
Using the partition method 538
Putting it all together 540
Chapter 4: Working with Dates and Times 545
Pondering How Time is Represented 546
Picking the Right Date and Time Class for Your Application 547
Using the now Method to Create a Date-Time Object 548
Using the parse Method to Create a Date-Time Object 550
Using the of Method to Create a Date-Time Object 551
Using the Month enumeration 552
Using the ZoneId class 553
Using the ZoneOffset class 554
Looking Closer at the LocalDate Class 554
Extracting Information About a Date 556
Comparing Dates 557
Calculating with Dates 558
Formatting Dates 560
Looking at a Fun Birthday Calculator 562
Chapter 5: IoT Programming with Raspberry Pi 567
Introducing the Raspberry Pi 568
Setting Up a Raspberry Pi 570
Installing Java on a Raspberry Pi 571
Installing the Pi4J Library 572
Configuring the Geany Integrated Development Environment for Java Development 572
Examining GPIO Ports 574
Connecting an LED to a GPIO Port 576
Building a Raspberry Pi LED Circuit 581
Parts 581
Steps 582
Examining the Pi4J Library 582
Importing GPIO Types 583
Instantiating a GpioController 584
Provisioning GPIO Pins 584
Controlling the Pin State 587
The Morse Code Program 589
The Cylon Eyes Program 593
Assembling the Cylon Eyes circuit 593
Running the Cylon Eyes program 596
Working with Input Pins 598
Understanding active-high and active-low inputs 599
Provisioning a digital input 600
Reading the state of a digital input pin 601
Building a circuit with a digital input pin 602
Running the Button Switcher Program 604
Finding a Better Way to Handle Input Events 606
Crafting a state change event listener 607
Adding an event handler to a pin 608
Using automatic debounce 609
Working with the EventSwitcher program 610
Book 6: Javafx 613
Chapter 1: Hello, JavaFX! 615
Perusing the Possibilities of JavaFX 616
Getting Ready to Run JavaFX 618
Looking at a Simple JavaFX Program 620
Importing JavaFX Packages 622
Extending the Application Class 623
Launching the Application 624
Overriding the start Method 625
Creating a Button 626
Handling an Action Event 627
Creating a Layout Pane 629
Making a Scene 630
Setting the Stage 631
Examining the Click Counter Program 632
Chapter 2: Handling Events 637
Examining Events 638
Handling Events 639
Implementing the EventHandler Interface 641
Handling Events with Inner Classes 644
Handling Events with Anonymous Inner Classes 647
Using Lambda Expressions to Handle Events 649
Chapter 3: Setting the Stage and Scene Layout 655
Examining the Stage Class 656
Examining the Scene Class 659
Switching Scenes 661
Creating an Alert Box 666
Exit, Stage Right 670
Creating a Close button 671
Handling the CloseRequest event 672
Putting it all together 674
Chapter 4: Using Layout Panes to Arrange Your Scenes 677
Working with Layout Panes 678
Introducing five JavaFX layout panes 678
Creating layout panes 679
Combining layout panes 680
Using the HBox Layout 680
Spacing Things Out 682
Adding Space with Margins 684
Adding Space by Growing Nodes 685
Using the VBox Layout 687
Aligning Nodes in a Layout Pane 689
Using the Flow Layout 690
Using the Border Layout 694
Using the GridPane Layout 697
Sketching out a plan 697
Creating a grid pane 698
Working with grid pane constraints 701
Examining a grid pane example 703
Chapter 5: Getting Input from the User 709
Using Text Fields 710
Validating Numeric Data 717
Using Check Boxes 719
Using Radio Buttons 721
Chapter 6: Choosing from a List 725
Using Choice Boxes 725
Creating a choice box 727
Setting a default value 728
Getting the selected item 729
Working with Observable Lists 729
Listening for Selection Changes 732
Using Combo Boxes 734
Creating combo boxes 735
Getting the selected item 736
Handling combo box events 737
Using List Views 738
Creating a list view 739
Getting the selected items 740
Using Tree Views 740
Building a tree 742
Creating a TreeView control 745
Getting the selected node 746
Looking at a complete program that uses a tree view 748
Book 7: Web Programming 751
Chapter 1: Creating Servlets 753
Understanding Servlets 753
Using Tomcat 755
Installing Tomcat 755
Testing Tomcat 756
Creating a Simple Servlet 757
Creating the folder structure for a servlet 758
Creating the web xml file 758
Importing the servlet packages 760
Extending the HttpServlet class 760
Printing to a web page 761
Responding with HTML 762
Running a Servlet 764
Improving the HelloWorld Servlet 765
Getting Input from the User 767
Working with forms 767
Using the InputServlet servlet 768
Using Classes in a Servlet 769
Chapter 2: Using JavaServer Pages 775
Understanding JavaServer Pages 776
Using UEL Expressions 778
Unified Expression Language 778
JSP Standard Tag Library 780
Looking at Core Tags 782
Using c:out 783
Working with variables 783
Getting conditional 784
Creating loops 785
Formatting Numbers 786
Considering the Controller Servlet 788
Setting request attributes 788
Redirecting to the JSP page 789
The ListMovies Application Meets JSP 790
Chapter 3: Using JavaBeans 797
Getting to Know JavaBeans 797
Looking Over a Sample Bean 799
Using Beans with JSP Pages 801
Creating bean instances 802
Getting property values 803
Setting property values 803
Viewing a JSP page that uses a bean 804
Scoping Your Beans 806
A shopping cart application 807
The shopping cart page 808
The BookCart JavaBean 810
Chapter 4: Using HttpClient 815
Understanding HTTP 815
Diving into HTTP 817
Looking at a simple HTTP exchange 822
Getting Started with Java's HTTP Client Library 822
HttpClient 823
HttpRequest 824
HttpResponse 827
Using the send method 828
Putting It All Together 829
The HTTP Tester Program 831
Index 835