Introduction 1
PartⅠ Studying for the Programmer s Exam 7
Chapter 1 Programmer Exam Roadmap 8
What Is the Programmer Exam Like? 8
What s on the Exam 8
Declarations and Access Control 9
Flow Control and Exception Handling 9
Garbage Collection 9
Language Fundamentals 9
Operators and Assignments 9
Overloading,Overriding Runtime Type,and Object Orientation 10
Threads 10
The java.awt Package-Layout 10
The jave.lang Package 10
The java.util Package 10
The java.io Package 11
How Does the Text Map to the Programmer Exam Objectives? 11
How the Test Is Administered 11
How to Sign up for and Take the Exams 11
Test-Taking Tips for the Programmer Exam 12
What the Test Program Looks Like for the Programmer Exam 12
Be Careful! 13
Take Your Time and Answer the Easy Ones First 13
Beware of Tricks 14
Understand Why the Question Is on the Test 14
Know Your Test Center 15
What Happens When You re Done? 16
Sun s Take on Certification 16
How Sun Manages the Certification Process 16
Where Certification Is Heading 17
Chapter 2 Java Programming Fundamentals 18
Java Programming Concepts 18
Objects 18
Classes 19
Packages 19
Inheritance 19
Encapsulation 20
Access Controls 20
Java s Characteristics 21
Design for Distributed Networks 22
Design for Multithreading 22
Design for Ease of Use and Reuse 22
Design for Security 23
Working with Java Applets and Applications 23
The Java Specification and the API 23
Java1.1 24
Java2:JDK1.2 24
Java2:JDK1.3 25
Review Questions 25
Answers to Review Questions 26
Chapter 3 Java Keywords 27
An Alphabetical List of All the Keywords 27
Organizing Classes 27
Defining Classes 28
Keywords for Classes and Members 28
Simple Data Types 30
Values and Variables 30
Exception Handing 31
Instance Creation and Testing 32
Control Flow 32
Exerciese 34
Exercise3-1 34
Exercise3-2 34
Exercise3-3 34
Answers to the Exercises 34
Review Questions 35
Answers to Review Questions 36
Chapter 4 Java Building Blocks 37
Identifiers 37
Literals 38
Boolean Literals 38
Character Literals 38
Exercise4-1 39
String Literals 39
Exercise4-2 40
Integer Literals 40
Floating-Point Literals 41
Separators 41
Operators 41
Exercise4-3 42
Exercise4-4 43
Comments 43
Answers to the Exercises 44
Review Questions 44
Answers to Review Questions 46
Chapter 5 Memory and Garbage Collection 47
Garbage Collection 47
When Does an Object Become Eligible for Garbage Collection? 48
Invoking the Garbage Collector 48
Finalization 49
Exercise5-1 49
Exercise5-2 49
Accessing Members 50
Exercise5-3 51
Passing Parameters to a Method 51
Exercise5-4 54
Forcing Garbage Collection 55
Answers to the Exercises 55
Review Questions 57
Answers to Review Questions 59
Chapter 6 Data Types and Vaues 60
Data Types 60
Variables and Data Types 61
Primitive Data Types 61
Integer Data Types 62
Character Values 63
Floating-Point Arithmetic 64
Booleans 65
Casting 65
Exercises 66
Exercise6.1 66
Exercise6.2 66
Exercise6.3 66
Arrays 66
Declaring and Initializing Arrays 67
One-Dimensional Arrays 67
Initializing an Array When it is Allocated 68
Exercise6.4 69
Arrays of Arrays 69
Where Arrays Fit into the Class Hierarchy 70
Answers to the Exercises 71
Review Questions 72
Answers to Review Questions 73
Chapter 7 Operators 74
Operators and Expressions 74
Arithmetic Operators 75
Assignment Operators 77
Comparison Operators 78
Logical Operators 79
Bitwise Operators 80
The >>Operator 81
The >>>Operator 82
The <<Operator 83
The?,I,and?Operators 84
Exercises 85
Exercise7.1 85
Testing for an Object s Class Type 85
Exercise7.2 86
equals() and== 86
Exercise7.3 87
Exercise7.4 87
Operator Precedence and Evaluation Order 87
Using Arrays in Expressions 89
Using chars in Expressions 89
Answers to the Exercises 89
Review Questions 92
Answers to Review Questions 94
Chapter8 Control Flow 96
Using if and if...else Statements 96
if Statements 96
if...eles Statements 97
Nested if Statements 97
Legal Values for if Statements 98
Exercise8.1 98
Looping Expressions 99
while Statements 99
do-while Statements 100
Legal Values for while and do-while Statements 101
Iteration with for loops 101
for Statements 101
Controlling for Loops with Expressions 102
Nested for Loops 102
Switch and Case Statements 103
switch case 103
Nested switch case 104
Using Jump Statements 105
break 105
continue 105
labeled loops 106
Exercise8.2 108
Using ??and ‖ 109
Exercise8.3 109
Answers to the Exercises 109
Review Questions 110
Answers to Review Questions 111
Chapter9 Methods 112
Defining Methods 112
Methods Definitions 112
Return Types 113
Method Modifiers 114
Exercise9.1 116
Overloading a Method 117
Exercise9.2 118
Overriding a Method 118
Access Control to Subclasses 119
Inheriting Synchronized and Abstract 119
Return Types 119
Parameter Types 120
Exceptions 120
Exercise9.3 120
Object References to Base and Derived Classes 121
Answers to the Exercises 122
Review Questions 124
Answers to Review Questions 125
Chapter10 Constructors 126
Constructor Essentials 126
The Default Constructor 127
Defining,Overloading,and Overriding a Constructor 128
Invoking Another Constructor 130
Instance Variables in Constructors 130
Exercise10.1 132
Exercise10.2 132
Exercise10.3 133
Answers to the Exercises 133
Review Questions 134
Answers to Review Questions 136
Chapter11 Objects and Classes 137
Objects 137
Creating and Destroying Objects 138
Classes 139
Declaring a Class 139
Application Class Structures 140
Applet Class Structures 140
Class Variables 143
Instance Variables 144
Static Variables 145
Predefined Instances 145
null 145
this 145
super 146
Object-Oriented Relationships Using‘is a’and ‘has a’ 146
Exercises 148
Exercise11.1 148
Exercise11.2 148
Class Keywords 148
public 149
abstract 149
strictfp 150
final 150
Exercise11.3 151
Exercise11.4 151
Variable Keywords 151
static 151
final 151
transient 152
volatile 152
Exercise11.5 152
Access Control Keywords 152
public 152
private 153
protected 153
Exercise11.6 153
Exercise11.7 154
Exercise11.8 154
Exercise11.9 154
Exercise11.10 154
Casting Classes 155
Inner Classes 156
Creating Inner Classes 156
Anonymous Classes 160
Static Inner Classes 161
Exercise11.11 161
Answers to the Exercises 162
Review Questions 168
Answers to Review Questions 171
Chapter12 Packages,Inheritance,and Interfaces 172
Packages 172
Declaring a Package 172
The CLASSPATH Environmental Variable 174
Import Statements 174
Package Naming Conventions 175
Using Packages 176
Exercise12.1 177
Exercise12.2 177
Exercise12.3 177
Exercise12.4 178
Creating a Package 178
Ordering of a Java Source File 180
Exercise12.5 180
Exercise12.6 181
Inheritance 182
Declaring Inheritance 183
Using Inheritance 184
Interfaces 185
Using Interfaces 185
Defining Interfaces 186
Using the Cloneable Interface 187
Exercise12.7 188
Answers to the Exercises 188
Review Questions 190
Answers to Review Questions 191
Chapter13 Passing Arguments to Programs 192
The main() Method 192
Command Line Arguments 193
Exercise13.1 194
When main() Ends 195
Embedding an Applet in an HTML Page 195
Passing Parameters to an Applet 196
Retrieving Parameters 197
Exercise13.2 198
Customizing an Applet 198
Answers to the Exercises 201
Review Questions 203
Answers to Review Questions 204
Chapter14 Exceptions 205
Exception Basics 205
Working with Exceptions 206
Uncaught Exceptions 207
Checked and Unchecked Exceptions 207
Using try,catch,and finally 208
Exercise14.1 209
Common Java Exceptions 210
Using Methods Defined by Exception and Throwable 211
Rethrowing an Exception 211
Exercise14.2 212
Which Exceptions a Method Can Throw 213
Exceptions in an Overriding Method in a Subclass 213
Exercise14.3 214
Creating and Throwing an Exception 214
Exercise14.4 215
Answers to the Exercises 215
Review Questions 218
Answers to Review Questions 223
Chapter15 Multithreading 224
An Overview of Threads 224
Thread States 224
Thread Life Cycles 225
Thread Scheduling and Priorities 226
Creating Threads With java.lang.Thread and java.lang.Runnable 227
Subclassing Thread 227
Implementing Runnable 229
Exercise15.1 230
Monitors and Synchronization 232
Understanding Synchronization 232
Exercise15.2 234
Using wait(),notify(),and notifyAll() 234
Exercise15.3 238
Managing Threads With Wait-Notify and Sleep 238
Why a Thread Might Not Execute 241
Exercise15.4 242
Other Useful Thread Methods 242
Answers to the Exercises 242
Review Questions 247
Answers to Review Questions 248
Chapter16 Essential java.lang and java.util 249
Wrapper Classes 249
The Boolean Class 249
The Character Class 250
The Byte,Short,Integer,and Long Classes 250
The Float and Double Classes 251
Using Wrapper Classes 251
Exercise16.1 252
Working with the Math Class 252
abs() 252
ceil() 253
floor() 253
max() 254
min() 254
random() 254
round() 254
sqrt() 255
sin() 255
cos() 255
tan() 255
Exercise16.2 255
The String and StringBuffer Classes 255
String Operators 255
The Immutability of Strings 256
Methods of the String Class 257
Exercise16.3 258
String Buffers 258
Exercise16.4 259
Collections 260
Overview of the Collections API 260
Collection API Essentials 262
Answers to the Exercises 263
Review Questions 264
Answers to Review Questions 267
Chapter17 java.io 268
The java.io Package 268
InputStream and OutputStream 269
FilterInputStream and FilterOutputStream 269
DataInput and DataOutput 271
Reader and Writer 272
Working with Files 273
Testing to See if a File Exists 273
Finding Information about a File Node 274
Deleting and Renaming Files 274
Creating Directories 274
Navigating the File System 274
Creating Files 274
Exercise17.1 275
Creating Temporary Files 275
Streams 276
Sequencing Stream Filters 276
Exercise17.2 278
Reading and Writing Data Streams 278
Encoded Text Streams 279
Writing To and Reading From Files 281
Exercise17.3 282
Random Access Files 282
File Descriptors 284
Exercise17.4 285
Using Buffered Readers to Read Text Input 285
Using Print Writers to Write Text Output 286
Answers to the Exercises 288
Review Questions 290
Answers to Review Questions 291
Chapter18 java.awt:Graphics and Components 293
The Abstract Windowing Toolkit(AWT) 293
AWT Essentials 295
Applets and AWT 296
The paint() Method 297
Repainting 297
paint()and the Graphics Class 298
Smoother Graphics 298
Drawing Using a Graphics Object 299
drawString() 300
drawLine() 300
drawRect(),fillRect() 301
drawPolygon(),fillPolygon() 302
drawArc(),fillArc() 302
drawImage() 304
Exercise18.1 304
The Image Class 304
Creating a Blank Image 304
Loading or Transferring an Image 305
Using an ImageObserver 306
Exercise18.2 308
Working with Frame Windows 308
Constructing the Frame 308
Sizing the Frame 308
Setting the Frame Title 309
Closing the Frame Window 309
Creating a Frame Window in An Application 310
Creating a Frame Window in An Applet 311
How Java Arranges Components Within Containers 313
The Component Class,Subclasses,and Methods 313
TextArea 313
TextField 315
List 315
Exercise18.3 316
Key Component Methods 317
Answers to the Exercises 319
Review Questions 321
Answers to Review Questions 322
Chapter19 java.awt:Layout 323
Components and Layout Managers 323
Layout Manager Essentials 323
Using Layout Managers 324
FlowLayout 324
BorderLayout 326
GridLayout 330
Other Types of Layout Managers 332
Default Layout Managers 332
Exercise19.1 332
Answers to the Exercises 333
Review Questions 334
Answers to Review Questions 335
Chapter20 java.awt:Event Handling 336
Event Classes 336
Listening for Events 337
The Listener Interfaces 339
ActionListener 339
Interface Method 339
AdjustmentListener 340
ComponentListener 340
ContainerListener 340
FocusListener 341
ItemListener 341
KeyListener 341
MouseListener 341
MouseMotionListener 342
TextListener 342
WindowListener 342
Implementing a Listener Interface 343
Exercise20.1 344
Semantic and Low-Level Events 344
ActionEvent 344
AdjustmentEvent 344
ItemEvent 345
AWTEvent Subclasses 345
AWTEvent 345
ActionEvent 345
AdjustmentEvent 346
ComponentEvent 346
ContainerEvent 346
FocusEvent 346
InputEvent 346
ItemEvent 346
KeyEvent 346
MouseEvent 347
PaintEvent 347
TextEvent 347
WindowEvent 347
Exercise20.2 347
Extending Adapter Classes 347
Exercise20.3 348
Answers to the Exercises 348
Review Questions 351
Answers to Review Questions 352
Chapter21 Sun Certified Programmer Practice Exam1 353
Practice Exam Number1 353
Questions 353
Answers to Exam Questions 374
Chapter22 Sun Certified Programmer Practice Exam2 377
Practice Exam Number2 377
Questions 377
Answers to Exam Questions 393
Chapter23 Sun Certified Programmer Practice Exam3 396
Practice Exam Number3 396
Questions 396
Answers to Exam Questions 416
PartⅡ Studying for The Developer s Exam 419
Chapter24 Developer Exam Roadmap 420
A Quick Overview 420
Concepts You Should Know Before Getting Started 421
The Programming Assignment 421
The Essay Exam 423
How to Sign Up For and Take the Exams 424
How to Acquire the Programming 424
Strategies for Approaching the Developer Assignment 425
Chapter25 Developing with Java 426
Styles and Conventions 427
Identifiers 427
Comments 427
Error Handling 427
Java s Documentation Problem 428
javadoc 429
Helping javadoc 429
javadoc Tags 432
Variables 432
Classes 434
Methods 434
Rules for Using javadoc Comments and Tags 435
Examples of javadoc Comments and Tags 435
Abstract Classes 438
Interfaces 438
Grouping Behavior 438
Enforcing Your API 438
Identifying Intent 439
Exercies25.1 439
Packages 439
Creating Packages 439
Restricting Access 440
Defining Subsystems 440
Exercise25.2 441
Defining Access Methods 441
Exercise25.3 443
Answers to the Exercises 444
Review Questions 446
Answers to Review Questions 447
Chapter26 Accessing and Managing Databases 448
Roll-Your-Own Databases 449
Accessing Databases from Multiple Clients 451
Two-Tiered and Three-Tiered Architectures 452
Using Java APIs 453
Remote Method Invocation 453
Defining a Remote Interface 454
Defining a Remote Class 454
Defining the Class,Interface,and Constructor 455
Installing the Security Manager and Registering the Bootstrap Naming Service 458
Invoking Remote Objects 459
Stubs and Skeletons 461
Getting Things Going 461
Exercise26.1 462
JDBC 462
The Need for Drivers 462
Using ODBC 463
Opening a Connection to a Database 464
Interacting with a Database 464
An Example of Using ODBC with Microsoft Access 465
Exercise26.2 466
Answers to the Exercises 467
Review Questions 468
Answers to Review Questions 469
Chapter27 Network Programming and Communication 470
ATCP/IP Primer 470
Ports and Sockets 472
Streams 473
Designing Servers and Clients 473
Handling Multiple Clients 474
Client Issues 475
The Networking Package 476
InetAddress 476
Socket and ServerSocket 476
Internet Streams 478
Client-Server Examples 479
Writing a Single-Client Server in Java 479
server() 487
client() 488
action() 488
run()for SingleChatReceive 488
Cleaning Up 489
Writing a Multi-Client Server in Java 489
server() 494
run() 494
broadcast() 495
cleanUp() 495
The Client 495
Cleaning Up 499
Exercise27.1 499
Exercise27.2 500
Uniform Resource Locator 500
Answers to the Exercises 501
Review Questions 511
Answers to Review Questions 512
Chapter28 Designing Application Interfaces 513
Mix and Match! 513
Using One Layout Manager 514
Exercise28.1 520
Changing Your Look 520
Text 520
Font and Color 521
Replacing a Component 521
Exercise 28.2 521
Working with Frames 522
Closing the Frame 522
Making an Applet a Standalone Application 523
Exercise28.3 523
Answers to the Exercises 523
Review Questions 527
Answers to Review Questions 528
Chapter29 Sun Certified Developer Practice Programming Assignment 529
The Scenario 529
The Prototype Specifications 530
The Design Completed So Far 530
The New Design 532
The Code Completed So Far 533
What You Will Add to the Code 537
The Database 538
The Server 538
The Client 538
The User Interface 538
The Protocol 538
Making a New Reservation 539
Delete a Passenger s Reservation 539
Finding a Passenger s Seat 539
Finding All Open Seats 539
Retrieving the List of Passengers 539
What the User Interface Should Look Like when You re Done 539
Updating the Passenger List 541
Final Words of Advice 541
One Possible Answer for the Client Side 542
Client.java 542
TopFrame.java 545
EdnaUI.java 545
NewPassenger.java 548
DeletePassenger.java 549
One Possible Answer for the Server Side 551
Server.java(Including ClientServerThread) 551
The Three Missing Methods from DB.java 558
Chapter30 Sun Certified Developer Practice Essay Essay Exam 560
Questions 560
Answers 561
PartⅢ Appendices 563
AppendixA What s on the CD-ROM 564
How the CD is Organized 564
Source Code 564
Sun s JDK 564
Where to Find More 564
What Is the Test Engine? 565
Appendix B The JDK 566
The Pieces of the JDK 566
The Compiler 566
The Runtime Environment for Stand-Alone Programs 567
The Documentation Tool 567
The Applet Viewer 568