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 TextPad 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 Ex.
Java All-In-One for Dummies