Home Page Icon
Home Page
Table of Contents for
Java® For Dummies®, 6th Edition
Close
Java® For Dummies®, 6th Edition
by Barry Burd
Java For Dummies, 6th Edition
Introduction
Introduction
How to Use This Book
Conventions Used in This Book
What You Don’t Have to Read
Foolish Assumptions
How This Book Is Organized
Part I: Getting Started with Java
Part II: Writing Your Own Java Program
Part III: Working with the Big Picture: Object-Oriented Programming
Part IV: Savvy Java Techniques
Part V: The Part of Tens
Icons Used in This Book
Beyond the Book
Where to Go from Here
Part I: Getting Started with Java
Chapter 1: All about Java
What You Can Do with Java
Why You Should Use Java
Getting Perspective: Where Java Fits In
Object-Oriented Programming (OOP)
Object-oriented languages
Objects and their classes
What’s so good about an object-oriented language?
Refining your understanding of classes and objects
What’s Next?
Chapter 2: All about Software
Quick-Start Instructions
What You Install on Your Computer
What is a compiler?
What is a Java Virtual Machine?
Developing software
What is an Integrated Development Environment?
Chapter 3: Using the Basic Building Blocks
Speaking the Java Language
The grammar and the common names
The words in a Java program
Checking Out Java Code for the First Time
Understanding a Simple Java Program
The Java class
The Java method
The main method in a program
How you finally tell the computer to do something
Curly braces
And Now, a Few Comments
Adding comments to your code
What’s Barry’s excuse?
Using comments to experiment with your code
Part II: Writing Your Own Java Programs
Chapter 4: Making the Most of Variables and Their Values
Varying a Variable
Assignment Statements
Understanding the Types of Values That Variables May Have
Displaying Text
Numbers without Decimal Points
Combining Declarations and Initializing Variables
The Atoms: Java’s Primitive Types
The char type
The boolean type
The Molecules and Compounds: Reference Types
An Import Declaration
Creating New Values by Applying Operators
Initialize once, assign often
The increment and decrement operators
Assignment operators
Chapter 5: Controlling Program Flow with Decision-Making Statements
Making Decisions (Java if Statements)
Guess the number
She controlled keystrokes from the keyboard
Creating randomness
The if statement
The double equal sign
Brace yourself
Indenting if statements in your code
Elseless in Ifrica
Forming Conditions with Comparisons and Logical Operators
Comparing numbers; comparing characters
Comparing objects
Importing everything in one fell swoop
Java’s logical operators
Vive les nuls!
(Conditions in parentheses)
Building a Nest
Choosing among Many Alternatives (Java switch Statements)
Your basic switch statement
To break or not to break
The new and improved switch
Chapter 6: Controlling Program Flow with Loops
Repeating Instructions Over and Over Again (Java while Statements)
Repeating a Certain Number of Times (Java for Statements)
The anatomy of a for statement
The world premiere of “Al’s All Wet”
Repeating Until You Get What You Want (Java do Statements)
Reading a single character
File handling in Java
Variable declarations and blocks
Part III: Working with the Big Picture: Object-Oriented Programming
Chapter 7: Thinking in Terms of Classes and Objects
Defining a Class (What It Means to Be an Account)
Declaring variables and creating objects
Initializing a variable
Using an object’s fields
One program; several classes
Public classes
Defining a Method within a Class (Displaying an Account)
An account that displays itself
The display method’s header
Sending Values to and from Methods (Calculating Interest)
Passing a value to a method
Returning a value from the getInterest method
Making Numbers Look Good
Hiding Details with Accessor Methods
Good programming
Public lives and private dreams: Making a field inaccessible
Enforcing rules with accessor methods
Chapter 8: Saving Time and Money: Reusing Existing Code
Defining a Class (What It Means to Be an Employee)
The last word on employees
Putting your class to good use
Cutting a check
Working with Disk Files (A Brief Detour)
Storing data in a file
Copying and pasting code
Reading from a file
Who moved my file?
Adding directory names to your filenames
Reading a line at a time
Closing the connection to a disk file
Defining Subclasses (What It Means to Be a Full-Time or Part-Time Employee)
Creating a subclass
Creating subclasses is habit-forming
Using Subclasses
Overriding Existing Methods (Changing the Payments for Some Employees)
A Java annotation
Using methods from classes and subclasses
Chapter 9: Constructing New Objects
Defining Constructors (What It Means to Be a Temperature)
What is a temperature?
What is a temperature scale? (Java's enum type)
Okay, so then what is a temperature?
What you can do with a temperature
Calling new Temperature(32.0): A case study
Some things never change
More Subclasses (Doing Something about the Weather)
Building better temperatures
Constructors for subclasses
Using all this stuff
The default constructor
A Constructor That Does More
Classes and methods from the Java API
The SuppressWarnings annotation
Part IV: Savvy Java Techniques
Chapter 10: Putting Variables and Methods Where They Belong
Defining a Class (What It Means to Be a Baseball Player)
Another way to beautify your numbers
Using the Player class
Nine, count ’em, nine
Don’t get all GUI on me
Tossing an exception from method to method
Making Static (Finding the Team Average)
Why is there so much static?
Meet the static initializer
Displaying the overall team average
Static is old hat
Could cause static; handle with care
Experiments with Variables
Putting a variable in its place
Telling a variable where to go
Passing Parameters
Pass by value
Returning a result
Pass by reference
Returning an object from a method
Epilogue
Chapter 11: Using Arrays to Juggle Values
Getting Your Ducks All in a Row
Creating an array in two easy steps
Storing values
Tab stops and other special things
Using an array initializer
Stepping through an array with the enhanced for loop
Searching
Writing to a file
When to close a file
Arrays of Objects
Using the Room class
Yet another way to beautify your numbers
The conditional operator
Command Line Arguments
Using command line arguments in a Java program
Checking for the right number of command line arguments
Chapter 12: Using Collections and Streams (When Arrays Aren't Good Enough)
Understanding the Limitations of Arrays
Collection Classes to the Rescue
Using an ArrayList
Using generics
Testing for the presence of more data
Using an iterator
Java's many collection classes
New in Java 8: Functional Programming
Solving a problem the old-fashioned way
Streams
Lambda expressions
A taxonomy of lambda expressions
Using streams and lambda expressions
Why bother?
Method references
Chapter 13: Looking Good When Things Take Unexpected Turns
Handling Exceptions
The parameter in a catch clause
Exception types
Who’s going to catch the exception?
The multi-catch clause
Throwing caution to the wind
Doing useful things
Our friends, the good exceptions
Handle an Exception or Pass the Buck
Finishing the Job with a finally Clause
A try Statement with Resources
Chapter 14: Sharing Names among the Parts of a Java Program
Access Modifiers
Classes, Access, and Multipart Programs
Members versus classes
Access modifiers for members
Putting a drawing on a frame
Directory structure
Making a frame
Sneaking Away from the Original Code
Default access
Crawling back into the package
Protected Access
Putting non-subclasses in the same package
Access Modifiers for Java Classes
Public classes
Nonpublic classes
Chapter 15: Responding to Keystrokes and Mouse Clicks
Go On . . . Click That Button
Events and event handling
The Java interface
Threads of execution
The keyword this
Inside the actionPerformed method
The serialVersionUID
Responding to Things Other Than Button Clicks
Creating Inner Classes
Chapter 16: Writing Java Applets
Applets 101
Waiting to be called
A public class
The Java API (again)
Making Things Move
The methods in an applet
What to put into all these methods
Responding to Events in an Applet
Chapter 17: Using Java Database Connectivity
JDBC and Java DB
Creating Data
Using SQL commands
Connecting and disconnecting
Retrieving Data
Part V: The Part of Tens
Chapter 18: Ten Ways to Avoid Mistakes
Putting Capital Letters Where They Belong
Breaking Out of a switch Statement
Comparing Values with a Double Equal Sign
Adding Components to a GUI
Adding Listeners to Handle Events
Defining the Required Constructors
Fixing Non-Static References
Staying within Bounds in an Array
Anticipating Null Pointers
Helping Java Find Its Files
Chapter 19: Ten Websites for Java
This Book’s Website
The Horse’s Mouth
Finding News, Reviews, and Sample Code
Looking for Java Jobs
Everyone’s Favorite Sites
About the Author
Cheat Sheet
More Dummies Products
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Prev
Previous Chapter
01_9781118407806-ffirs.xhtml
Next
Next Chapter
03_9781118407806-intro.xhtml
Java® For Dummies®, 6th Edition
Visit
www.dummies.com/cheatsheet/java
to view this book's cheat sheet.
Table of Contents
Introduction
How to Use This Book
Conventions Used in This Book
What You Don’t Have to Read
Foolish Assumptions
How This Book Is Organized
Part I: Getting Started with Java
Part II: Writing Your Own Java Program
Part III: Working with the Big Picture: Object-Oriented Programming
Part IV: Savvy Java Techniques
Part V: The Part of Tens
Icons Used in This Book
Beyond the Book
Where to Go from Here
Part I: Getting Started with Java
Chapter 1: All about Java
What You Can Do with Java
Why You Should Use Java
Getting Perspective: Where Java Fits In
Object-Oriented Programming (OOP)
Object-oriented languages
Objects and their classes
What’s so good about an object-oriented language?
Refining your understanding of classes and objects
What’s Next?
Chapter 2: All about Software
Quick-Start Instructions
What You Install on Your Computer
What is a compiler?
What is a Java Virtual Machine?
Developing software
What is an Integrated Development Environment?
Chapter 3: Using the Basic Building Blocks
Speaking the Java Language
The grammar and the common names
The words in a Java program
Checking Out Java Code for the First Time
Understanding a Simple Java Program
The Java class
The Java method
The main method in a program
How you finally tell the computer to do something
Curly braces
And Now, a Few Comments
Adding comments to your code
What’s Barry’s excuse?
Using comments to experiment with your code
Part II: Writing Your Own Java Programs
Chapter 4: Making the Most of Variables and Their Values
Varying a Variable
Assignment Statements
Understanding the Types of Values That Variables May Have
Displaying Text
Numbers without Decimal Points
Combining Declarations and Initializing Variables
The Atoms: Java’s Primitive Types
The char type
The boolean type
The Molecules and Compounds: Reference Types
An Import Declaration
Creating New Values by Applying Operators
Initialize once, assign often
The increment and decrement operators
Assignment operators
Chapter 5: Controlling Program Flow with Decision-Making Statements
Making Decisions (Java if Statements)
Guess the number
She controlled keystrokes from the keyboard
Creating randomness
The if statement
The double equal sign
Brace yourself
Indenting if statements in your code
Elseless in Ifrica
Forming Conditions with Comparisons and Logical Operators
Comparing numbers; comparing characters
Comparing objects
Importing everything in one fell swoop
Java’s logical operators
Vive les nuls!
(Conditions in parentheses)
Building a Nest
Choosing among Many Alternatives (Java switch Statements)
Your basic switch statement
To break or not to break
The new and improved switch
Chapter 6: Controlling Program Flow with Loops
Repeating Instructions Over and Over Again (Java while Statements)
Repeating a Certain Number of Times (Java for Statements)
The anatomy of a for statement
The world premiere of “Al’s All Wet”
Repeating Until You Get What You Want (Java do Statements)
Reading a single character
File handling in Java
Variable declarations and blocks
Part III: Working with the Big Picture: Object-Oriented Programming
Chapter 7: Thinking in Terms of Classes and Objects
Defining a Class (What It Means to Be an Account)
Declaring variables and creating objects
Initializing a variable
Using an object’s fields
One program; several classes
Public classes
Defining a Method within a Class (Displaying an Account)
An account that displays itself
The display method’s header
Sending Values to and from Methods (Calculating Interest)
Passing a value to a method
Returning a value from the getInterest method
Making Numbers Look Good
Hiding Details with Accessor Methods
Good programming
Public lives and private dreams: Making a field inaccessible
Enforcing rules with accessor methods
Chapter 8: Saving Time and Money: Reusing Existing Code
Defining a Class (What It Means to Be an Employee)
The last word on employees
Putting your class to good use
Cutting a check
Working with Disk Files (A Brief Detour)
Storing data in a file
Copying and pasting code
Reading from a file
Who moved my file?
Adding directory names to your filenames
Reading a line at a time
Closing the connection to a disk file
Defining Subclasses (What It Means to Be a Full-Time or Part-Time Employee)
Creating a subclass
Creating subclasses is habit-forming
Using Subclasses
Overriding Existing Methods (Changing the Payments for Some Employees)
A Java annotation
Using methods from classes and subclasses
Chapter 9: Constructing New Objects
Defining Constructors (What It Means to Be a Temperature)
What is a temperature?
What is a temperature scale? (Java's enum type)
Okay, so then what is a temperature?
What you can do with a temperature
Calling new Temperature(32.0): A case study
Some things never change
More Subclasses (Doing Something about the Weather)
Building better temperatures
Constructors for subclasses
Using all this stuff
The default constructor
A Constructor That Does More
Classes and methods from the Java API
The SuppressWarnings annotation
Part IV: Savvy Java Techniques
Chapter 10: Putting Variables and Methods Where They Belong
Defining a Class (What It Means to Be a Baseball Player)
Another way to beautify your numbers
Using the Player class
Nine, count ’em, nine
Don’t get all GUI on me
Tossing an exception from method to method
Making Static (Finding the Team Average)
Why is there so much static?
Meet the static initializer
Displaying the overall team average
Static is old hat
Could cause static; handle with care
Experiments with Variables
Putting a variable in its place
Telling a variable where to go
Passing Parameters
Pass by value
Returning a result
Pass by reference
Returning an object from a method
Epilogue
Chapter 11: Using Arrays to Juggle Values
Getting Your Ducks All in a Row
Creating an array in two easy steps
Storing values
Tab stops and other special things
Using an array initializer
Stepping through an array with the enhanced for loop
Searching
Writing to a file
When to close a file
Arrays of Objects
Using the Room class
Yet another way to beautify your numbers
The conditional operator
Command Line Arguments
Using command line arguments in a Java program
Checking for the right number of command line arguments
Chapter 12: Using Collections and Streams (When Arrays Aren't Good Enough)
Understanding the Limitations of Arrays
Collection Classes to the Rescue
Using an ArrayList
Using generics
Testing for the presence of more data
Using an iterator
Java's many collection classes
New in Java 8: Functional Programming
Solving a problem the old-fashioned way
Streams
Lambda expressions
A taxonomy of lambda expressions
Using streams and lambda expressions
Why bother?
Method references
Chapter 13: Looking Good When Things Take Unexpected Turns
Handling Exceptions
The parameter in a catch clause
Exception types
Who’s going to catch the exception?
The multi-catch clause
Throwing caution to the wind
Doing useful things
Our friends, the good exceptions
Handle an Exception or Pass the Buck
Finishing the Job with a finally Clause
A try Statement with Resources
Chapter 14: Sharing Names among the Parts of a Java Program
Access Modifiers
Classes, Access, and Multipart Programs
Members versus classes
Access modifiers for members
Putting a drawing on a frame
Directory structure
Making a frame
Sneaking Away from the Original Code
Default access
Crawling back into the package
Protected Access
Putting non-subclasses in the same package
Access Modifiers for Java Classes
Public classes
Nonpublic classes
Chapter 15: Responding to Keystrokes and Mouse Clicks
Go On . . . Click That Button
Events and event handling
The Java interface
Threads of execution
The keyword this
Inside the actionPerformed method
The serialVersionUID
Responding to Things Other Than Button Clicks
Creating Inner Classes
Chapter 16: Writing Java Applets
Applets 101
Waiting to be called
A public class
The Java API (again)
Making Things Move
The methods in an applet
What to put into all these methods
Responding to Events in an Applet
Chapter 17: Using Java Database Connectivity
JDBC and Java DB
Creating Data
Using SQL commands
Connecting and disconnecting
Retrieving Data
Part V: The Part of Tens
Chapter 18: Ten Ways to Avoid Mistakes
Putting Capital Letters Where They Belong
Breaking Out of a switch Statement
Comparing Values with a Double Equal Sign
Adding Components to a GUI
Adding Listeners to Handle Events
Defining the Required Constructors
Fixing Non-Static References
Staying within Bounds in an Array
Anticipating Null Pointers
Helping Java Find Its Files
Chapter 19: Ten Websites for Java
This Book’s Website
The Horse’s Mouth
Finding News, Reviews, and Sample Code
Looking for Java Jobs
Everyone’s Favorite Sites
About the Author
Cheat Sheet
More Dummies Products
Guide
Table of Contents
Begin Reading
Pages
1
2
3
4
5
6
7
8
9
10
11
12
13
14
16
17
18
19
20
21
22
23
24
25
26
27
29
30
32
33
34
35
37
38
39
40
41
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
59
60
61
63
64
65
66
67
69
70
71
73
74
75
77
78
79
80
81
82
83
84
85
86
87
88
90
91
92
93
94
95
96
97
98
99
101
102
104
105
106
107
108
109
110
111
112
113
115
116
118
121
122
123
124
125
126
127
129
130
132
133
134
135
137
138
139
140
141
142
143
144
145
146
147
149
150
151
153
154
155
156
157
158
159
160
161
163
164
165
166
168
169
170
171
172
173
174
175
176
177
178
179
180
182
183
185
186
187
188
189
190
191
192
193
194
195
196
197
199
200
201
202
203
204
205
206
208
209
210
211
212
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
251
252
253
254
255
256
257
258
259
260
263
264
265
266
267
268
269
271
272
273
274
275
276
278
279
280
281
282
284
285
286
287
288
289
290
291
292
293
294
295
296
297
299
300
301
302
303
304
305
306
307
308
309
310
311
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
345
346
347
348
349
352
353
354
355
356
357
358
359
361
362
363
364
365
366
367
369
370
371
372
373
374
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
393
394
395
396
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset