Java For Dummies

Java For Dummies

by Barry Burd

Paperback(7th ed.)

View All Available Formats & Editions
Choose Expedited Shipping at checkout for delivery by Thursday, August 5


A new edition of the bestselling guide to Java

If you want to learn to speak the world’s most popular programming language like a native, Java For Dummies is your ideal companion. With a focus on reusing existing code, it quickly and easily shows you how to create basic Java objects, work with Java classes and methods, understand the value of variables, learn to control program flow with loops or decision-making statements, and so much more!

Java is everywhere, runs on almost any computer, and is the engine that drives the coolest applications. Written for anyone who’s ever wanted to tackle programming with Java but never knew quite where to begin, this bestselling guide is your ticket to success! Featuring updates on everything you’ll encounter in Java 9—and brimming with tons of step-by-step instruction—it’s the perfect resource to get you up and running with Java in a jiffy!

  • Discover the latest features and tools in Java 9
  • Learn to combine several smaller programs to create a bigger program
  • Create basic Java objects and reuse code
  • Confidently handle exceptions and events

If you’re ready to jump into Java, this bestselling guide will help keep your head above water!

Related collections and offers

Product Details

ISBN-13: 9781119235552
Publisher: Wiley
Publication date: 03/20/2017
Series: For Dummies Books
Edition description: 7th ed.
Pages: 512
Sales rank: 223,971
Product dimensions: 7.40(w) x 9.20(h) x 1.00(d)

About the Author

Barry Burd, PhD, is a computer science professor at Drew University. The author of Java Programming for Android Developers For Dummies, Beginning Programming with Java For Dummies, and Android Application Development All-in-One For Dummies, Barry also writes for Server Side (, Android Authority (, and numerous other online publications.

Table of Contents

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

Access Modifiers 384

Classes, Access, and Multipart Programs 385

Members versus classes 385

Access modifiers for members 386

Putting a drawing on a frame 389

Directory structure 391

Making a frame 392

Sneaking Away from the Original Code 394

Default access 396

Crawling back into the package 399

Protected Access 400

Subclasses that aren’t in the same package 400

Classes that aren’t subclasses (but are in the same package) 402

Access Modifiers for Java Classes 406

Public classes 406

Nonpublic classes 406

Chapter 15: Fancy Reference Types 409

Java’s Types 409

The Java Interface 410

Two interfaces 411

Implementing interfaces 412

Putting the pieces together 415

Abstract Classes 417

Caring for your pet 420

Using all your classes 422

Relax! You’re Not Seeing Double! 424

Chapter 16: Responding to Keystrokes and Mouse Clicks 427

Go On Click That Button 428

Events and event handling 430

Threads of execution 431

The keyword this 432

Inside the actionPerformed method 434

The serialVersionUID 435

Responding to Things Other Than Button Clicks 436

Creating Inner Classes 441

Chapter 17: Using Java Database Connectivity 445

Creating a Database and a Table 446

What happens when you run the code 447

Using SQL commands 447

Connecting and disconnecting 449

Putting Data in the Table 450

Retrieving Data 451

Destroying Data 453

Part 5: The Part of Tens 455

Chapter 18: Ten Ways to Avoid Mistakes 457

Putting Capital Letters Where They Belong 457

Breaking Out of a switch Statement 458

Comparing Values with a Double Equal Sign 458

Adding Components to a GUI 459

Adding Listeners to Handle Events 459

Defining the Required Constructors 459

Fixing Non-Static References 460

Staying within Bounds in an Array 460

Anticipating Null Pointers 461

Helping Java Find Its Files 462

Chapter 19: Ten Websites for Java 463

This Book’s Website 463

The Horse’s Mouth 463

Finding News, Reviews, and Sample Code 464

Got a Technical Question? 464

Index 465

Customer Reviews