Java
Java
net
ing
r
Sams.
Learn
Cente
Teach Yourself
JAVA
in 21 Days
S
S
F
Laura Lemay
Charles L. Perkins R
W
T
M
net
ing
r
Sams.
Learn
Cente
About This Book
This book teaches you all about the Java language and how to use it to create
applets and applications. By the time you get through with this book, you’ll know
enough about Java to do just about anything, inside an applet or out.
Conventions
Note: A Note box presents interesting pieces of information related to the surround-
ing discussion.
Technical Note: A Technical Note presents specific technical information related to
the surrounding discussion.
Tip: A Tip box offers advice or teaches an easier way to do something.
Caution: A Caution box alerts you to a possible problem and gives you advice to
! avoid it.
Warning: A Warning box advises you about potential problems and helps you steer
clear of disaster.
NEW☛
TERM New terms are introduced in New Term boxes, with the term in italics.
Type A type icon identifies some new HTML code that you can type in yourself.
Output An Output icon highlights what the same HTML code looks like when viewed by
either Netscape or Mosaic.
ii
vi
net
ing
r
Sams.
Learn
Cente
Overview
Introduction xxi
Week 1 at a Glance
Day 1 An Introduction to Java Programming 3
S
2 Object-Oriented Programming and Java 19
3 Java Basics 41
S
4 Working with Objects 61
5 Arrays, Conditionals, and Loops 79
F
6 Creating Classes and Applications in Java 95
7 More About Methods 111
Week 2 at a Glance
Day 8 Java Applet Basics R
129
9
10
Graphics, Fonts, and Color
Simple Animation and Threads
W 149
173
11
12
More Animation, Images, and Sound
Managing Simple Events and Interactivity T 195
217
M
13 User Interfaces with the Java Abstract Windowing Toolkit 237
14 Windows, Networking, and Other Tidbits 279
Week 3 at a Glance
Day 15 Modifiers 305
16 Packages and Interfaces 323
17 Exceptions 341
18 Multithreading 353
19 Streams 375
20 Native Methods and Libraries 403
21 Under the Hood 421
Appendixes
A Language Summary 473
B The Java Class Library 483
C How Java Differs from C and C++ 497
D How Java Differs from C and C++ 507
Index 511
vii
net
ing
r
Sams.
Learn
Cente
Contents
Introduction xxi
Week 1 at a Glance 1
Day 1 An Introduction to Java Programming 3
What Is Java? .............................................................................................. 4
Java’s Past, Present, and Future ................................................................... 6 S
Why Learn Java? ......................................................................................... 7
Java Is Platform-Independent................................................................. 7
S
F
Java Is Object-Oriented ......................................................................... 9
Java Is Easy to Learn .............................................................................. 9
Getting Started with
Programming in Java .............................................................................. 10
Getting the Software ............................................................................ 10
Applets and Applications ..................................................................... 11 R
W
Creating a Java Application .................................................................. 11
Creating a Java Applet ......................................................................... 13
T
Summary .................................................................................................. 16
Q&A ........................................................................................................ 16
M
Day 2 Object-Oriented Programming and Java 19
Thinking in Objects: An Analogy ............................................................. 20
Objects and Classes .................................................................................. 21
Behavior and Attributes ............................................................................ 23
Attributes ............................................................................................. 23
Behavior .............................................................................................. 24
Creating a Class ................................................................................... 24
Inheritance, Interfaces, and Packages ........................................................ 28
Inheritance........................................................................................... 29
Creating a Class Hierarchy................................................................... 30
How Inheritance Works ...................................................................... 32
Single and Multiple Inheritance ........................................................... 34
Interfaces and Packages ........................................................................ 34
Creating a Subclass ................................................................................... 35
Summary .................................................................................................. 38
Q&A ........................................................................................................ 39
Day 3 Java Basics 41
Statements and Expressions ...................................................................... 42
Variables and Data Types ......................................................................... 43
Declaring Variables .............................................................................. 43
Notes on Variable Names .................................................................... 44
ix
net
ing
r
Sams.
Learn
Cente
Day 5 Arrays, Conditionals, and Loops 79
Arrays ....................................................................................................... 80
Declaring Array Variables .................................................................... 80
Creating Array Objects ........................................................................ 81
Accessing Array Elements ..................................................................... 81
Changing Array Elements .................................................................... 82
Multidimensional Arrays...................................................................... 83
Block Statements ...................................................................................... 83
if Conditionals .......................................................................................... 83
The Conditional Operator ................................................................... 84
switch Conditionals ................................................................................... 85
for Loops .................................................................................................. 86
while and do Loops ................................................................................... 88
while Loops .......................................................................................... 88
do...while Loops ................................................................................... 89
Breaking Out of Loops ............................................................................. 89
Labeled Loops ...................................................................................... 90
Summary .................................................................................................. 91
Q&A ........................................................................................................ 92
Day 6 Creating Classes and Applications in Java 95
Defining Classes ....................................................................................... 96
Creating Instance and Class Variables ....................................................... 96
Defining Instance Variables ................................................................. 97
Constants ............................................................................................. 97
Class Variables ..................................................................................... 98
Creating Methods ..................................................................................... 99
Defining Methods ............................................................................... 99
The this Keyword ............................................................................... 101
Variable Scope and Method Definitions ............................................ 101
Passing Arguments to Methods .......................................................... 102
Class Methods ................................................................................... 104
Creating Java Applications ...................................................................... 105
Java Applications and Command-Line Arguments ................................. 106
Passing Arguments to Java Programs .................................................. 106
Handling Arguments in Your Java Program ....................................... 106
Summary ................................................................................................ 108
Q&A ...................................................................................................... 109
Day 7 More About Methods 111
Creating Methods with the Same Name, Different Arguments ............... 112
Constructor Methods ............................................................................. 115
Basic Constructors ............................................................................. 116
Calling Another Constructor ............................................................. 117
Overloading Constructors .................................................................. 117
xi
net
ing
r
Sams.
Learn
Cente
Day 10 Simple Animation and Threads 173
Creating Animation in Java .................................................................... 174
Painting and Repainting .................................................................... 174
Starting and Stopping
an Applet’s Execution ...................................................................... 175
Putting It Together ............................................................................ 175
Threads: What They Are
and Why You Need Them ................................................................... 177
The Problem with the Digital Clock Applet ...................................... 178
Writing Applets with Threads ............................................................ 179
Fixing The Digital Clock ................................................................... 180
Reducing Animation Flicker ................................................................... 182
Flicker and How to Avoid It .............................................................. 182
How to Override Update ................................................................... 183
Solution One: Don’t Clear the Screen ............................................... 183
Solution Two: Redraw
Only What You Have To ................................................................ 186
Summary ................................................................................................ 192
Q&A ...................................................................................................... 192
Day 11 More Animation, Images, and Sound 195
Retrieving and Using Images .................................................................. 196
Getting Images .................................................................................. 196
Drawing Images ................................................................................. 198
Modifying Images .............................................................................. 201
Creating Animation Using Images .......................................................... 201
An Example: Neko............................................................................. 201
Retrieving and Using Sounds .................................................................. 209
Sun’s Animator Applet ........................................................................... 211
More About Flicker: Double-Buffering................................................... 212
Creating Applets with Double-Buffering............................................ 212
An Example: Checkers Revisited ........................................................ 213
Summary ................................................................................................ 214
Q&A ...................................................................................................... 215
Day 12 Managing Simple Events and Interactivity 217
Mouse Clicks .......................................................................................... 218
mouseDown and mouseUp ................................................................... 219
An Example: Spots ............................................................................. 220
Mouse Movements ................................................................................. 223
mouseDrag and mouseMove ................................................................. 223
mouseEnter and mouseExit .................................................................. 223
An Example: Drawing Lines .............................................................. 224
Keyboard Events ..................................................................................... 228
The keyDown Method ....................................................................... 228
Default Keys ...................................................................................... 229
xiii