Java ® A L L - I N - O N E 5th Edition by Doug Lowe Java® All-in-One For Dummies®, 5th Edition Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com Copyright © 2017 by John Wiley & Sons, Inc., Hoboken, New Jersey Published simultaneously in Canada Library of Congress Control Number: 2017934999 ISBN: 978-1-119-24779-1 (pbk); ISBN 978-1-119-24780-7 (ebk); ISBN 978-1-119-24781-4 (ebk) Manufactured in the United States of America Contents at a Glance Introduction ........................................................1 Book 1: Java Basics ................................................5 CHAPTER 1: Welcome to Java ................................................7 CHAPTER 2: Installing and Using Java Tools ...................................21 CHAPTER 3: Working with TextPad ..........................................35 Book 2: Programming Basics ...................................43 CHAPTER 1: Java Programming Basics .......................................45 CHAPTER 2: Working with Variables and Data Types ...........................65 CHAPTER 3: Working with Numbers and Expressions ..........................99 CHAPTER 4: Making Choices ...............................................129 CHAPTER 5: Going Around in Circles (Or, Using Loops) ........................151 CHAPTER 6: Pulling a Switcheroo ...........................................179 CHAPTER 7: Adding Some Methods to Your Madness .........................193 CHAPTER 8: Handling Exceptions ..........................................213 Book 3: Object-Oriented Programming ......................233 CHAPTER 1: Understanding Object-Oriented Programming ...................235 CHAPTER 2: Making Your Own Classes ......................................249 CHAPTER 3: Working with Statics ...........................................267 CHAPTER 4: Using Subclasses and Inheritance ...............................277 CHAPTER 5: Using Abstract Classes and Interfaces ...........................299 CHAPTER 6: Using the Object and Class Classes ..............................315 CHAPTER 7: Using Inner Classes, Anonymous Classes, and Lambda Expressions ......................................341 CHAPTER 8: Working with Packages and the New Java Module System ..........353 Book 4: Strings, Arrays, and Collections .....................373 CHAPTER 1: Working with Strings ..........................................375 CHAPTER 2: Using Arrays .................................................393 CHAPTER 3: Using the ArrayList Class .......................................423 CHAPTER 4: Using the LinkedList Class ......................................437 CHAPTER 5: Creating Generic Collection Classes .............................449 CHAPTER 6: Using Bulk Data Operations with Collections .....................463 Book 5: Programming Techniques. ........................... 473 CHAPTER 1: Programming Threads. ........................................ 475 CHAPTER 2: Using Regular Expressions ..................................... 505 CHAPTER 3: Using Recursion .............................................. 523 CHAPTER 4: Working with Dates and Times ................................. 539 Book 6: JavaFX .................................................... 561 CHAPTER 1: Hello, JavaFX!. ................................................ 563 CHAPTER 2: Handling Events .............................................. 583 CHAPTER 3: Setting the Stage and Scene Layout ............................. 603 CHAPTER 4: Using Layout Panes to Arrange Your Scenes. ..................... 625 CHAPTER 5: Getting Input from the User .................................... 659 CHAPTER 6: Choosing from a List .......................................... 681 Book 7: Web Programming. .................................... 707 CHAPTER 1: Using Java Web Start .......................................... 709 CHAPTER 2: Creating Servlets .............................................. 721 CHAPTER 3: Using JavaServer Pages ........................................ 741 CHAPTER 4: Using JavaBeans .............................................. 757 Book 8: Files and Databases ................................... 775 CHAPTER 1: Working with Files ............................................ 777 CHAPTER 2: Working with File Streams ..................................... 799 CHAPTER 3: Database for $100, Please ..................................... 827 CHAPTER 4: Using JDBC to Connect to a Database. ........................... 845 CHAPTER 5: Working with XML ............................................. 861 Index. .............................................................. 881 Contents 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 ..............................................11 Java versus Other Languages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12 Important Features of the Java Language ........................13 Type checking .............................................13 Automatic memory management ............................15 Exception handling ........................................16 On the Downside: Java’s Weaknesses ...........................16 Java Version Insanity ..........................................17 What’s in a Name? ............................................19 CHAPTER 2: Installing and Using Java Tools ........................21 Downloading and Installing the Java Development Kit .............22 Downloading the JDK ......................................22 Installing the JDK ..........................................23 Perusing the JDK folders ....................................23 Setting the path ...........................................24 Using Java’s Command-Line Tools ..............................26 Compiling a program ......................................26 Compiling more than one file ...............................27 Using Java compiler options .................................28 Running a Java program ....................................29 Using the javap command ..................................31 Using Java Documentation .....................................32 Java SE API Docs ...........................................32 Java Language Specification .................................33 Table of Contents v CHAPTER 3: Working with TextPad .................................. 35 Downloading and Installing TextPad ............................ 35 Editing Source Files. .......................................... 36 Compiling a Program ......................................... 39 Running a Java Program. ...................................... 40 BOOK 2: PROGRAMMING BASICS ............................. 43 CHAPTER 1: Java Programming Basics .............................. 45 Looking at the Infamous Hello, World! Program .................. 46 Dealing with Keywords ........................................ 49 Working with Statements. ..................................... 51 Types of statements ....................................... 51 White space .............................................. 52 Working with Blocks .......................................... 53 Creating Identifiers ........................................... 55 Crafting Comments ........................................... 55 End-of-line comments. ..................................... 56 Traditional comments. ..................................... 56 JavaDoc comments ........................................ 57 Introducing Object-Oriented Programming ...................... 57 Understanding classes and objects .......................... 57 Understanding static methods .............................. 58 Creating an object from a class. ............................. 59 Viewing a program that uses an object ....................... 60 So what’s the difference? ................................... 62 Importing Java API Classes ..................................... 63 CHAPTER 2: Working with Variables and Data Types. ............ 65 Declaring Variables ........................................... 66 Declaring two or more variables in one statement ............. 67 Declaring class variables ................................... 67 Declaring instance variables ................................ 68 Declaring local variables. ................................... 69 Initializing Variables .......................................... 70 Initializing variables with assignment s tatements .............. 71 Initializing variables with initializers. ......................... 72 Using Final Variables (Constants) ............................... 72 Working with Primitive Data Types. ............................. 73 Integer types ............................................. 75 Floating-point types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76 The char type ............................................. 78 The Boolean type. ......................................... 79 Wrapper classes ........................................... 79 Using Reference Types ..................................... 80 vi Java All-in-One For Dummies Working with Strings. ......................................... 82 Declaring and initializing strings. ............................ 82 Combining strings ......................................... 83 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 .................... 90 Using System.out and System.err ............................ 92 Getting Input with the Scanner Class ............................ 92 Importing the Scanner class ................................ 93 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 ..................... 97 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. . . . . . . . . . . . . . . . . . . . . .124 Integer overflow. ......................................... 125 Floating-point weirdness .................................. 125 Division by zero .......................................... 126 CHAPTER 4: Making Choices ......................................... 129 Using Simple Boolean Expressions. ............................ 130 Using if Statements. ......................................... 132 Simple if statements. ..................................... 132 if-else statements ........................................ 134 Table of Contents vii Nested if statements ......................................135 else-if statements ........................................ 139 Using Mr. Spock’s Favorite Operators (Logical Ones, of Course) .... 141 Using the ! operator ...................................... 142 Using the & and && operators ............................. 143 Using the | and || operators .............................. 144 Using the ^ operator. ..................................... 145 Combining logical operators ............................... 146 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. ................................ 157 Running do-while Loops. ..................................... 159 Validating Input from the User ................................ 161 Using the Famous for Loop ................................... 163 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 ..................................... 171 Breaking and continuing your for loops. . . . . . . . . . . . . . . . . . . . . .172 Nesting Your Loops. ......................................... 173 A simple nested for loop .................................. 173 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 ............... 185 Creating Character Cases .....................................186 Intentionally Leaving Out a Break Statement .................... 188 Switching with Strings. ....................................... 191 viii Java All-in-One For Dummies CHAPTER 7: Adding Some Methods to Your Madness. .......... 193 The Joy of Methods ..........................................194 The Basics of Making Methods ................................ 195 An example. ............................................. 196 Another example. ........................................ 197 Methods That Return Values. ................................. 199 Declaring the method’s return type ......................... 199 Using the return statement to return the value ...............200 Using a method that returns a type ......................... 201 You gotta have a proper return statement ................... 201 Trying another version of the guessing-game program ........ 203 Methods That Take Parameters ............................... 206 Declaring parameters .....................................207 Scoping out parameters. .................................. 208 Understanding pass-by-value .............................. 209 Trying yet another version of the guessing-game program ..... 210 CHAPTER 8: Handling Exceptions ................................... 213 Understanding Exceptions. ................................... 214 Witnessing an exception ...................................215 Finding the culprit ........................................ 215 Catching Exceptions ......................................... 216 A simple example ........................................ 217 Another example. ........................................ 218 Handling Exceptions with a Preemptive Strike. .................. 220 Catching All Exceptions at Once ............................... 221 Displaying the Exception Message ............................. 223 Using a finally Block ......................................... 223 Handling Checked Exceptions. ................................ 226 Viewing the catch-or-throw compiler error. .................. 226 Catching FileNotFoundException ........................... 227 Throwing the FileNotFoundException ....................... 228 Throwing an exception from main .......................... 229 Swallowing exceptions .................................... 229 Throwing Your Own Exceptions ............................... 231 Catching Multiple Exceptions. ................................. 232 BOOK 3: OBJECT-ORIENTED PROGRAMMING. ............. 233 Understanding Object-Oriented CHAPTER 1: Programming. ........................................... 235 What Is Object-Oriented Programming? ........................ 235 Understanding Objects. ...................................... 237 Objects have identity ..................................... 237 Objects have type ........................................ 238 Table of Contents ix Objects have state. ....................................... 239 Objects have behavior .................................... 240 Understanding the Life Cycle of an Object ...................... 240 Working with Related Classes ................................. 241 Inheritance .............................................. 242 Interfaces ............................................... 242 Designing a Program with Objects ............................. 243 Diagramming Classes with UML ............................... 244 Drawing classes. ......................................... 245 Drawing arrows .......................................... 246 CHAPTER 2: Making Your Own Classes. ............................ 249 Declaring a Class ............................................ 249 Picking class names. ...................................... 250 Knowing what goes in the class body. ....................... 250 Seeing where classes go. .................................. 251 Working with Members ...................................... 253 Understanding fields ......................................253 Understanding methods .................................. 254 Understanding visibility ................................... 254 Using Getters and Setters .................................... 255 Overloading Methods. ....................................... 257 Creating Constructors. ....................................... 259 Creating basic constructors. ............................... 259 Creating default constructors .............................. 260 Calling other constructors ................................. 262 Finding More Uses for the this Keyword ........................ 263 Using Initializers .............................................265 CHAPTER 3: Working with Statics ................................... 267 Understanding Static Fields and Methods .......................267 Working with Static Fields .................................... 269 Using Static Methods ........................................ 269 Counting Instances .......................................... 270 Preventing Instances. ........................................ 273 Using Static Initializers ....................................... 274 CHAPTER 4: Using Subclasses and Inheritance. .................. 277 Introducing Inheritance ...................................... 277 Motorcycles, trains, and automobiles ....................... 279 Game play. .............................................. 279 A businesslike example ................................... 280 Inheritance hierarchies ....................................280 Creating Subclasses ......................................... 281 Overriding Methods ......................................... 283 x Java All-in-One For Dummies