Table Of ContentJAVA DEMYSTIFIED
JIM KEOGH
McGraw-Hill/Osborne
P:\010Comp\DeMYST\454-8\fm.vp
Monday, April 12, 2004 1:11:04 PM
Copyright © 2004 by The McGraw-Hill Companies
0-07-146997-4
The material in this eBook also appears in the print version of this title: 0-07-225454-8
DOI: 10.1036/0071469974
Color profile: Generic CMYK printer profile Demystified / Java Demystified / Keogh/ 225454-8 / FM
Composite Default screen
CONTENTS AT A GLANCE
CHAPTER 1 Inside Java 1
CHAPTER 2 Data Types and Variables 17
CHAPTER 3 Expressions and Statements 41
CHAPTER 4 Control Structures 65
CHAPTER 5 Arrays 91
CHAPTER 6 Methods and Polymorphism 109
CHAPTER 7 Classes 125
CHAPTER 8 Inheritance 145
CHAPTER 9 Exception Handling 165
CHAPTER 10 Multithreading 181
CHAPTER 11 Files and Streams 209
CHAPTER 12 Graphical User Interface 223
CHAPTER 13 JDBC and Java Data Objects 255
CHAPTER 14 Java Applets 289
Final Exam 303
Answers to Quizzes and Final Exam 309
Index 325
P:\010Comp\DeMYST\454-8\fm.vp
Monday, April 12, 2004 1:11:05 PM
CONTENTS
Introduction xv
CHAPTER 1 Inside Java 1
Computer Programs 1
Data 2
Programming Languages 3
In the Beginning 5
How a Computer Language Becomes
a Standard 7
And Then Java 7
An Inside Look at Java 9
Writing a Java Program from Scratch 10
Compiling a Java Program 11
Running a Java Program 12
Taking Apart a Java Program 13
Class Definition 13
Method Definition 14
Statement 15
Quiz 16
CHAPTER 2 Data Types and Variables 17
Data and Numbers 17
Playing with Numbering Systems 20
Numbers and Characters 20
Looking Up the Unicode Value 22
P:\010Comp\DeMYST\454-8\fm.vp
Monday, April 12, 2004 1:11:05 PM
Literals 22
Integer Literals 23
Floating-point Literals 24
Boolean Literals 25
Character Literals 26
String Literals 28
Data Types 28
Integer Data Types 29
Floating-point Data Types 31
Character Data Type 31
Boolean Data Type 32
Casting Data Types 32
Variables 33
Declaring a Variable 34
Declaring Multiple Variables 36
Initializing a Variable 36
Scope of a Variable 38
The Life of a Variable 39
Quiz 40
CHAPTER 3 Expressions and Statements 41
Expressions 41
Types of Expressions 43
Operators 45
Arithmetic Operators 46
Relational Operators 50
Logical Operators 52
Bitwise Operators 56
Statements 63
Quiz 63
CHAPTER 4 Control Structures 65
Program Flow 65
Control Statements 66
P:\010Comp\DeMYST\454-8\fm.vp
Monday, April 12, 2004 1:11:05 PM
Selection Statements 67
TheifStatement 67
TheswitchStatement 76
Iteration Statements 80
TheforLoop 80
ThewhileLoop 85
ThedowhileLoop 87
Jump Statements 88
break 88
continue 89
return 90
Quiz 90
CHAPTER 5 Arrays 91
Inside an Array 91
Allocating Memory for an Array 93
Initializing Arrays 94
Multidimensional Arrays 94
Creating a Multidimensional Array 95
Values Assigned to Array Elements 96
The Length Data Member 96
Passing an Array to a Method 98
Returning an Array from a Method 100
Alternate Ways of Creating an Array 101
The Arrays Class 102
equals() 102
fill() 104
sort() 106
binarySearch() 106
Quiz 107
CHAPTER 6 Methods and Polymorphism 109
An Inside Look at Methods 110
Types of Methods 110
P:\010Comp\DeMYST\454-8\fm.vp
Monday, April 12, 2004 1:11:05 PM
The Method Definition 111
The Method Header 111
The Method Body 112
The Method Return Value 113
The Argument List 114
Elements of an Argument List 115
How an Argument List Works 115
Command-Line Arguments 116
Passing Command-Line Arguments 117
Calling a Method 119
Polymorphism 120
The Method Signature 121
Quiz 123
CHAPTER 7 Classes 125
Class Definition 125
Defining a Method Member 127
Access Specifiers 127
Declaring Instance Variables 129
Constructor 130
Declaring an Instance of a Class 131
Accessing Members of a Class 132
Overloading Member Methods 133
Overloading a Constructor 134
The this Keyword 135
Garbage Collection 136
The finalize() Method 137
Inner Classes 138
Static Initializers 139
Packages 140
Using a Package 141
CLASSPATH 142
Packages and Access Protection 143
Quiz 143
P:\010Comp\DeMYST\454-8\fm.vp
Monday, April 12, 2004 1:11:05 PM
Color profile: Generic CMYK printer profile Demystified / Java Demystified / Keogh/ 225454-8 / FM
Composite Default screen
CHAPTER 8 Inheritance 145
What Is Inheritance? 145
When to Use Inheritance 146
Inside Inherence 147
Accessing Members of an Inherited Class 147
The Superclass Can Be Instantiated 149
One-way Inheritance 149
Calling Constructors 150
Using the super Keyword 150
Multilevel Inheritance 152
Overriding Method Members Using Inheritance 154
Dynamic Method Dispatch 156
Abstract Classes 159
The final Keyword and Inheritance 161
The Object Class and Subclasses 162
Quiz 163
CHAPTER 9 Exception Handling 165
What Is an Exception? 165
Exception Handlers 166
Basic Exception Handling 167
Multiple Catch Blocks 168
The Finally Block 169
Working with Uncaught Exceptions 170
Nested Try Statements 171
Throwing an Exception 172
Methods That Don't Handle Exceptions 174
Checked and Unchecked Exceptions 175
Creating an Exception Subclass 176
Quiz 180
CHAPTER 10 Multithreading 181
Multitasking 181
Overhead 182
Threads 183
Synchronization 184
P:\010Comp\DeMYST\454-8\fm.vp
Monday, April 12, 2004 1:11:05 PM
Color profile: Generic CMYK printer profile Demystified / Java Demystified / Keogh/ 225454-8 / FM
Composite Default screen
The Thread Classes and the Runnable
Interface 185
The Main Thread 185
Creating Your Own Thread 187
Creating a Thread by Using extends 189
Using Multiple Threads in a Program 190
Using isAlive() and join() 192
Setting Thread Priorities 195
Synchronizing Threads 197
The Synchronized Method 198
Using the Synchronized Statement 201
Communicating Between Threads 202
Suspending and Resuming Threads 206
Quiz 208
CHAPTER 11 Files and Streams 209
Files and File Systems 209
The File Class 210
Listing Files Contained in a Directory 213
Streams 214
Writing to a File 214
Reading from a File 216
Appending to a File 217
Reading and Writing an Object to a File 219
Quiz 222
CHAPTER 12 Graphical User Interface 223
What Is a User Interface? 223
What Is a GUI? 225
A Simple GUI 226
swing 228
Content Container 230
Java Layout Managers 232
Push Buttons 236
Labels and Text Fields 237
Radio Buttons and Check Boxes 238
P:\010Comp\DeMYST\454-8\fm.vp
Monday, April 12, 2004 1:11:05 PM
Combo Boxes 240
Text Area 242
Scroll Pane 243
Getting Data from GUI Components 245
Reading a Push Button 247
Reading Radio Buttons and Check Boxes 249
Reading a Combo Box 251
Disabling and Enabling GUI Elements 253
Quiz 253
CHAPTER 13 JDBC and Java Data Objects 255
Database 101 255
The Concept of JDBC 256
JDBC Driver Types 257
JDBC API Packages 258
The JDBC Process 258
Load the JDBC Driver 258
Connect to the DBMS 259
Create and Execute an SQL Query 260
Terminate the Connection to the DBMS 263
Trapping Exceptions 264
Avoid Timing Out 265
More on Statement Objects 265
TheStatementObject 266
ThePreparedStatementObject 268
TheCallableStatementObject 270
The ResultSet Object 272
Reading the Resultset 273
Positioning the Virtual Cursor 275
Fetching Rows 278
Updatable Resultset 280
Metadata 284
Data Types 285
Exceptions 287
Quiz 287
P:\010Comp\DeMYST\454-8\fm.vp
Monday, April 12, 2004 1:11:06 PM