Introduction 1 How to Use This Book 1 Conventions Used in This Book 2 What You Don''t Have to Read 2 Foolish Assumptions 3 How This Book Is Organized 4 Part 1: Getting Started with Java 4 Part 2: Writing Your Own Java Program 4 Part 3: Working with the Big Picture: Object-Oriented Programming 5 Part 4: Smart Java Techniques 5 Part 5: The Part of Tens 5 Icons Used in This Book 5 Beyond the Book 6 Where to Go from Here 7 Part 1: Getting Started with Java 9 Chapter 1: All about Java 11 What You Can Do with Java 12 Why You Should Use Java 13 Getting Perspective: Where Java Fits In 14 Object-Oriented Programming (OOP) 16 Object-oriented languages 16 Objects and their classes 18 What''s so good about an object-oriented language? 19 Refining your understanding of classes and objects 21 What''s Next? 23 Chapter 2: All about Software 25 Quick-Start Instructions 25 What You Install on Your Computer 28 What is a compiler? 29 What is a Java Virtual Machine? 32 Developing software 39 What is an integrated development environment? 40 Chapter 3: Using the Basic Building Blocks 43 Speaking the Java Language 43 The grammar and the common names 44 The words in a Java program 45 Checking Out Java Code for the First Time 47 Understanding a Simple Java Program 48 The Java class 49 The Java method 50 The main method in a program 52 How you finally tell the computer to do something 53 Curly braces 55 And Now, a Few Comments 59 Adding comments to your code 60 What''s Barry''s excuse? 63 Using comments to experiment with your code 63 Part 2: Writing Your Own Java Programs 65 Chapter 4: Making the Most of Variables and Their Values 67 Varying a Variable 68 Assignment statements 70 The types of values that variables may have 71 Displaying text 74 Numbers without decimal points 75 Combining declarations and initializing variables 77 Experimenting with JShell 78 What Happened to All the Cool Visual Effects? 82 The Atoms: Java''s Primitive Types 82 The char type 83 The boolean type 85 The Molecules and Compounds: Reference Types 87 An Import Declaration 91 Creating New Values by Applying Operators 93 Initialize once, assign often 97 The increment and decrement operators 98 Assignment operators 102 Chapter 5: Controlling Program Flow with Decision-Making Statements 105 Making Decisions (Java if Statements) 106 Guess the number 106 She controlled keystrokes from the keyboard 107 Creating randomness 110 The if statement 111 The double equal sign 112 Brace yourself 112 Indenting if statements in your code 113 Elseless in Ifrica 114 Using Blocks in JShell 116 Forming Conditions with Comparisons and Logical Operators 117 Comparing numbers; comparing characters 117 Comparing objects 118 Importing everything in one fell swoop 121 Java''s logical operators 121 Vive les nuls! 124 (Conditions in parentheses) 125 Building a Nest 127 Choosing among Many Alternatives (Java switch Statements) 130 Your basic switch statement 130 To break or not to break 134 Strings in a switch statement 136 Chapter 6: Controlling Program Flow with Loops 139 Repeating Instructions Over and Over Again (Java while Statements) 140 Repeating a Certain Number of Times (Java for Statements) 143 The anatomy of a for statement 145 The world premiere of "Al''s All Wet" 147 Repeating until You Get What You Want (Java do Statements) 150 Reading a single character 154 File handling in Java 154 Variable declarations and blocks 156 Part 3: Working with the Big Picture: Object-Oriented Programming 159 Chapter 7: Thinking in Terms of Classes and Objects 161 Defining a Class (What It Means to Be an Account) 162 Declaring variables and creating objects 164 Initializing a variable 167 Using an object''s fields 167 One program; several classes 168 Public classes 168 Defining a Method within a Class (Displaying an Account) 169 An account that displays itself 171 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 Making Numbers Look Good 180 Hiding Details with Accessor Methods 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 197 Defining a Class (What It Means to Be an Employee) 198 The last word on employees 198 Putting your class to good use 200 Cutting a check 204 Working with Disk Files (a Brief Detour) 205 Storing data in a file 205 Copying and pasting code 206 Reading from a file 208 Who moved my file? 210 Adding directory names to your filenames 211 Reading a line at a time 212 Closing the connection to a disk file 213 Defining Subclasses (What It Means to Be a Full-Time or Part-Time Employee) 214 Creating a subclass 216 Creating subclasses is habit-forming 219 Using Subclasses 219 Making types match 221 The second half of the story 222 Overriding Existing Methods (Changing the Payments for Some Employees) 224 A Java annotation 226 Using methods from classes and subclasses 226 Chapter 9: Constructing New Objects 231 Defining Constructors (What It Means to Be a Temperature) 232 What is a temperature? 233 What is a temperature scale? (Java''s enum type) 233 Okay, so then what is a temperature? 234 What you can do with a temperature 236 Calling new Temperature(32.0): A case study 239 Some things never change 241 More Subclasses (Doing Something about the Weather) 243 Building better temperatures 243 Constructors for subclasses 245 Using all this stuff 246 The default constructor 247 A Constructor That Does More 250 Classes and methods from the Java API 253 The SuppressWarnings annotation 254 Part 4: Smart Java Techniques 257 Chapter 10: Putting Variables and Methods Where They Belong 259 Defining a Class (What It Means to Be a Baseball Player) 260 Another way to beautify your numbers 261 Using the Player class 261 One class; nine objects 264 Don''t get all GUI on me 265 Tossing an exception from method to method 266 Making Static (Finding the Team Average) 267 Why is there so much static? 269 Meet the static initializer 270 Displaying the overall team average 271 The static keyword is yesterday''s news 273 Could cause static; handle with care 274 Experiments with Variables 277 Putting a variable in its place 277 Telling a variable where to go 280 Passing Parameters 285 Pass by value 285 Returning a result 287 Pass by reference 287 Returning an object from a method 289 Epilogue 292 Chapter 11: Using Arrays to Juggle Values 293 Getting Your Ducks All in a Row 293 Creating an array in two easy steps 296 Storing values 297 Tab stops and other special things 299 Using an array initializer 299 Stepping through an array with the enhanced for loop 300 Searching 302 Writing to a file 305 When to close a file 306 Arrays of Objects 307 Using the Room class 309 Yet another way to beautify your numbers 312 The conditional operator 313 Command Line Arguments 315 Using command line arguments in a Java program 317 Checking for the right number of command line arguments 319 Chapter 12: Using Collections and Streams (When Arrays Aren''t Good Enough) 321 Understanding the Limitations of Arrays 321 Collection Classes to the Rescue 323 Using an ArrayList 323 Using generics 325 Wrapper classes 328 Testing for the presence of more data 330 Using an iterator 330 Java''s many collection classes 331 Functional Programming 333 Solving a problem the old-fashioned way 336 Streams 338 Lambda expressions 339 A taxonomy of lambda expressions 342 Using streams and lambda expressions 342 Why bother? 348 Method references 350 Chapter 13: Looking Good When Things Take Unexpected Turns 351 Handling Exceptions 352 The parameter in a catch clause 356 Exception types 357 Who''s going to catch the exception? 359 Catching two or more exceptions at a time 365 Throwing caution to the wind 366 Doing useful things 367 Our friends, the good exceptions 368 Handle an Exception or Pass the Buck 369 Finishing the Job with a finally Clause 376 A try Statement with Resources 379 Chapter 14: Sharing Names among the Parts of a Java Program 383.
Java for Dummies