Table of Contents
Section 1 Essential concepts and skills
Chapter 1 An introduction to Java
An overview of Java 4
Java timeline 4
Java editions 4
How Java compares to C++ and C# 6
Types of Java applications 8
Two types of desktop applications 8
Web applications and mobile apps 8
An introduction to Java development 10
The code for a console application 10
How Java compiles and interprets code 12
How to use the command prompt to compile and run a Java application 14
An introduction to Java IDEs 16
How to get started with NetBeans 18
An introduction to NetBeans 18
How to open and close a project 20
How to compile and run a project 22
How to work with the Output window 22
How to create a new project 24
How to get started with Eclipse 26
An introduction to Eclipse 26
How to open and close a project 28
How to compile and run a project 30
How to work with the Console window 30
How to create a new project 32
Chapter 2 How to write your first applications
Basic coding skills 42
How to declare a class and a main() method 42
How to code statements 44
How to code comments 44
How to work with numeric variables 46
How to declare and initialize variables 46
Rules and recommendations for naming variables 48
How to code arithmetic expressions 50
How to work with string variables 52
How to declare and initialize a string 52
How to join and append strings 52
How to include special characters in strings 54
How to use classes, objects, and methods 56
How to import classes 56
How to create objects and call methods 58
How to use the console for input and output 60
How to print output to the console 60
How to read input from the console 62
How to code simple control statements 64
How to compare numeric variables 64
How to compare string variables 64
How to code if/else statements 66
How to code while statements 68
Two illustrative applications 70
The Invoice application 70
The Test Score application 72
Two more skills for the road 74
How to test and debug an application 74
How to view the documentation for a class 76
Chapter 3 How to work with the primitive data types
Basic skills for working with data 84
The eight primitive data types 84
How to declare and initialize variables 86
How to declare and initialize constants 88
How to code arithmetic expressions 90
How to use the binary operators 90
How to use the compound assignment operators 92
How to use the unary operators 94
How to work with the order of precedence 96
How to work with casting 98
More skills for working with numbers 100
How to use the Math class 100
How to use the NumberFormat class 102
How to print formatted output to the console 104
The Invoice application with formatted output 106
How to debug a rounding error 108
Chapter 4 How to code control statements
How to code Boolean expressions 114
How to use the relational operators 114
How to use the logical operators 116
How to work with if/else statements 118
How to code if/else statements 118
How to work with braces 120
How to code nested if/else statements 120
How to work with switch statements and expressions 122
How to code switch statements 122
How to use arrow labels with switch statements 124
How to code switch expressions 126
The Invoice application with a switch expression 128
How to work with loops 130
How to code while loops 130
How to code do-while loops 132
How to code for loops 134
The Future Value application 136
How to code nested loops 138
How to code break and continue statements 140
How to code break statements 140
How to code continue statements 140
The Guess the Number application 142
Chapter 5 How to code methods and handle exceptions
How to code and call static methods 148
How to code static methods 148
How to call static methods 148
The Future Value application with a static method 150
The Guess the Number application with static methods 152
How to handle exceptions 154
How exceptions work 154
How to catch exceptions 156
How to prevent exceptions 156
How to validate data 158
How to validate a single entry 158
How to code a method that validates an entry 160
The Future Value application with data validation 162
The console 162
The code 164
Chapter 6 How to test and debug an application
Basic skills for testing and debugging 172
Typical test phases 172
The three types of errors 172
Common Java errors 174
A simple way to trace code execution 176
How to use NetBeans to debug an application 178
How to set and remove breakpoints 178
How to step through code 180
How to inspect variables 180
How to inspect the stack trace 182
How to use Eclipse to debug an application 184
How to set and remove breakpoints 184
How to step through code 186
How to inspect variables 186
How to inspect the stack trace 188
Chapter 7 How to code classes
An introduction to classes and objects 194
How classes can be used to structure an application 194
How encapsulation works 196
The relationship between a class and its objects 198
How to code a class that defines an object 200
The Product class 200
How to code instance variables 202
How to code constructors 204
How to code methods 206
How to create an object and call its methods 208
How to work with static fields and methods 210
How to code static fields and methods 210
How to call static fields and methods 210
The ProductDB class 212
The Product Viewer application 214
The console 214
The code 214
More skills for working with objects and methods 216
Reference types compared to primitive types 216
How to code a copy constructor 218
How to overload methods 220
How to use the this keyword 222
The Line Item application 224
The console 224
The class diagram 224
The code 226
Chapter 8 How to work with arrays and array lists
How to work with an array 238
How to create an array 238
How to assign values to an array 240
How to use loops with arrays 242
How to work with rectangular arrays 244
How to use the Arrays class 246
How to fill, sort, and search arrays 246
How to refer to, copy, and compare arrays 248
The Number Cruncher application 250
How to work with an array list 252
A comparison of arrays and array lists 252
How to create an array list 254
How to add and get elements 256
How to replace, remove, and search for elements 258
How to store primitive values in an array list 260
The Invoice application 262
The console 262
The Invoice class 264
The InvoiceApp class 266
Chapter 9 How to work with file I/O and exceptions
Introduction to file I/O 274
How files and streams work 274
A file I/O example 276
How to use a try-with-resources statement to handle I/O exceptions 278
How to work with text files 280
How to connect a character output stream to a file 280
How to write to a text file 282
How to connect a character input stream to a file 284
How to read from a text file 286
The ProductDB class 288
The Product Manager application 290
The console 290
The ProductManagerApp class 292
How exceptions work 296
The exception hierarchy 296
How exceptions are propagated 298
More skills for working with exceptions 300
How to declare a method that throws an exception 300
How to throw an exception 302
How to use the methods of an exception 304
How to code a finally block 306
Section 2 Object-oriented programming
Chapter 10 How to work with inheritance
An introduction to inheritance 314
How inheritance works 314
How the Object class works 316
Basic skills for working with inheritance 318
How to create a superclass 318
How to create a subclass 320
How polymorphism works 322
The Product application 324
The console 324
The Product class 326
The Book and Software classes 328
The ProductDB class 330
The ProductApp class 332
More skills for working with inheritance 334
How to cast objects 334
How to check the type of an object 336
How to compare objects for equality 338
How to define a custom exception 340
How to work with abstract, final, and sealed classes 342
How to work with abstract classes 342
How to work with the final classes, methods, and parameters 344
How to work with sealed classes 346
How to work with records 348
An introduction to records 348
How to use and customize records 350
Inheritance vs composition 352
When to use inheritance 352
When to use composition 352
Chapter 11 More skills for object-oriented programming
An introduction to interfaces 360
A simple interface 360
Interfaces compared to abstract classes 362
How to work with interfaces 364
How to code an interface 364
How to implement an interface 366
How to inherit a class and implement an interface 368
How to use an interface as a parameter 370
How to work with default and static methods 372
How to work with enumerations 374
How to declare an enumeration 374
How to use an enumeration 374
How to enhance an enumeration 376
How to work with packages and libraries 378
An introduction to packages 378
How to use NetBeans to work with packages 380
How to use Eclipse to work with packages 382
How to work with libraries 384
How to work with modules 386
An introduction to the module system 386
How to create and use modules 388
How to use javadoc to document a package 390
How to add javadoc comments to a class 390
How to generate Java documentation 392
How to view the documentation 392
Section 3 GUI programming
Chapter 12 How to get started with JavaFX
An introduction to GUI programming 402
A GUI that displays ten controls 402
A summary of GUT frameworks 402
The inheritance hierarchy for JavaFX nodes 404
How to start a JavaFX project 406
How to use NetBeans 406
How to use Eclipse 408
How to create a GUI that accepts user input 410
How to create and display a window 410
How to work with labels 412
How to set alignment and padding 414
How to work with text fields 416
How to set column widths 418
How to create a GUI that handles events 420
How to work with buttons and boxes 420
How to handle action events 422
The Future Value application 424
How to validate user input 428
How to display an error message in a dialog box 428
How to validate the data entered into a text field 430
The Validation class 432
How to validate multiple entries 434
Chapter 13 More skills for working with JavaFX
Seven more JavaFX controls 440
How to work with check boxes 440
How to work with radio buttons 442
How to work with combo boxes 444
How to work with date pickers 446
How to work with list views 448
How to work with text areas and scroll bars 450
The Social Media Signup application 452
The user interface 452
The code 454
Section 4 More concepts and skills
Chapter 14 More skills for working with strings
How to work with the String class 466
How to compare strings 466
How to work with string indexes 468
How to modify strings 470
How to work with the characters in a string 472
The Create Account application 474
The user interface 474
The CreateAccountApp class 474
More skills for working with strings 478
How to work with text blocks 478
How to split and join strings 480
How to format strings 482
The Hangman application 484
The user interface 484
The WordList class 486
The Hangman class 488
The HangmanApp class 490
How to work with the StringBuilder class 492
An introduction to StringBuilder objects 492
How to use a StringBuilder object with a loop 494
Chapter 15 More skills for working with collections
An introduction to collections 500
The Java collection framework 500
More about generics 502
How to work with a linked list 504
How to create a linked list and add and get elements 504
How to replace, remove, and search for elements 506
The Invoice application 508
How to sort the items in a collection 510
How to sort a collection of numbers or strings 510
How to sort objects by implementing the Comparable interface 512
How to sort objects with a custom comparator 514
How to work with stacks and queues 516
How to work with a stack 516
How to work with a queue 518
How to use generics to create a custom collection 520
How to work with maps 522
The HashMap and TreeMap classes 522
Code examples that work with maps 524
The Word Counter application 526
Chapter 16 How to work with lambda expressions and streams
How to work with lambda expressions 534
Anonymous classes compared to lambdas 534
Pros and cons of lambda expressions 534
A method that doesn't use lambdas 536
A method that uses lambdas 538
The syntax of a lambda expression 540
How to use functional interfaces from the Java API 542
How to use the Predicate interface 542
How to use the Consumer interface 544
How to use the Function interface 546
How to work with multiple functional interfaces 548
How to work with streams 550
How to filter a list 550
How to map a list 552
How to reduce a list 554
Chapter 17 How to work with recursion and algorithms
An introduction to recursion 560
How to add a range of numbers 560
How to compute the factorial of a number 562
Common recursive algorithms 564
How to implement the binary search algorithm 564
How to calculate a value in the Fibonacci series 568
How to decide if you should use recursion 570
The Directory Search application 572
How to use the File class 572
The algorithm 574
A directory tree 574
The console 574
The code 574
Chapter 18 How to work with dates and times
An introduction to date/time APIs 582
The date/time API prior to Java 8 582
The date/time API for Java 8 and later 582
How to use the new date/time API 584
How to create date and time objects 584
How to get date and time parts 586
How to compare dates and times 588
How to adjust dates and times 590
How to add or subtract a period of time 592
How to get the time between two dates 592
How to format dates and times 594
An Invoice class that includes an invoice date 596
Chapter 19 How to work with a database
How a relational database is organized 602
How a table is organized 602
How the columns in a table are defined 602
How tables are related 604
How to use SQL to work with a database 606
How to query a single table 606
How to join data from two or more tables 608
How to add, update, and delete data in a table 610
How to use DB Browser for SQLite 612
An introduction to SQLite 612
How to use DB Browser to view and edit a table in a SQLite database 614
How to use DB Browser to run SQL statements 616
How to create a SQLite database 618
An introduction to database drivers 620
Four types of JDBC database drivers 620
How to download a database driver 620
How to add a database driver to a project 622
How to use JDBC to work with a database 624
How to connect to a database 624
How to return a result set and move the cursor through it 626
How to get data from a result set 628
How to work with prepared statements 630
How to insert, update, and delete data 632
A class for working with a database 634
The ProductDB class 634
Code that uses the ProductDB class 638
Appendix A How to set up Windows for this book
How to install the JDK 646
How to install the source code for this book 648
How to install Netbeans 650
How to install Eclipse 652
How to install DB Browser for SQLite 654
Appendix B How to set up macOS for this book
How to install the JDK 658
How to install the source code for this book 660
How to install Netbeans 662
How to install Eclipse 664
How to install DB Browser for SQLite 666