Java : a beginner's guide

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Explains the origins of Java, how it relates to object-oriented programming, the basics of Java programming, and advanced topics including exception handling and the I/O system. 1 Java Fundamentals 1 -- Create Blocks of Code 27 -- 2 Introducing Data Types and Operators 35 -- 3 Program Control Statements 71 -- 4 Introducing Classes, Objects, and Methods 115 -- 5 More Data Types and Operators 151 -- 6 A Closer Look at Methods and Classes 201 -- 7 Inheritance 251 -- 8 Packages and Interfaces 301 -- 9 Exception Handling 333 -- 10 Using I/O 365 -- 11 Multithreaded Programming 407 -- 12 Enumerations, Autoboxing, and Static Import 449 -- 13 Generics 483 -- 14 Applets, Events, and Miscellaneous Topics 527 -- 15 Introducing Swing 559 -- B Using Java's Documentation Comments 655

Author(s): Herbert Schildt
Series: Essential skills made easy
Publisher: McGraw-Hill
Year: 2007

Language: English
Pages: 697

Contents......Page 10
Preface......Page 20
1 Java Fundamentals......Page 26
The Origins of Java......Page 27
How Java Relates to C and C++......Page 28
How Java Relates to C#......Page 29
Security......Page 30
Java’s Magic: The Bytecode......Page 31
The Java Buzzwords......Page 32
Object-Oriented Programming......Page 34
Polymorphism......Page 35
Inheritance......Page 36
A First Simple Program......Page 37
Entering the Program......Page 38
The First Sample Program Line by Line......Page 39
Handling Syntax Errors......Page 42
A Second Simple Program......Page 43
Another Data Type......Page 45
Project 1-1 Converting Gallons to Liters......Page 47
The if Statement......Page 48
The for Loop......Page 50
Create Blocks of Code......Page 52
Indentation Practices......Page 54
Project 1-2 Improving the Gallons-to-Liters Converter......Page 55
Identifiers in Java......Page 57
The Java Class Libraries......Page 58
Module 1 Mastery Check......Page 59
2 Introducing Data Types and Operators......Page 60
Java’s Primitive Types......Page 61
Integers......Page 62
Characters......Page 64
The Boolean Type......Page 66
Project 2-1 How Far Away Is the Lightning?......Page 68
Hexadecimal and Octal Constants......Page 69
String Literals......Page 70
Initializing a Variable......Page 72
Dynamic Initialization......Page 73
The Scope and Lifetime of Variables......Page 74
Arithmetic Operators......Page 77
Increment and Decrement......Page 79
Relational and Logical Operators......Page 80
Short-Circuit Logical Operators......Page 82
The Assignment Operator......Page 83
Shorthand Assignments......Page 85
Type Conversion in Assignments......Page 86
Casting Incompatible Types......Page 87
Operator Precedence......Page 89
Project 2-2 Display a Truth Table for the Logical Operators......Page 90
Type Conversion in Expressions......Page 91
Spacing and Parentheses......Page 93
Module 2 Mastery Check......Page 94
3 Program Control Statements......Page 96
Input Characters from the Keyboard......Page 97
The if Statement......Page 99
Nested ifs......Page 100
The if-else-if Ladder......Page 101
The switch Statement......Page 103
Nested switch Statements......Page 107
Project 3-1 Start Building a Java Help System......Page 108
The for Loop......Page 111
Some Variations on the for Loop......Page 112
Missing Pieces......Page 113
Loops with No Body......Page 115
Declaring Loop Control Variables Inside the for Loop......Page 116
The while Loop......Page 117
The do-while Loop......Page 119
Project 3-2 Improve the Java Help System......Page 122
Use break to Exit a Loop......Page 125
Use break as a Form of goto......Page 127
Use continue......Page 131
Project 3-3 Finish the Java Help System......Page 134
Nested Loops......Page 137
Module 3 Mastery Check......Page 138
4 Introducing Classes, Objects, and Methods......Page 140
The General Form of a Class......Page 141
Defining a Class......Page 142
Reference Variables and Assignment......Page 146
Methods......Page 147
Adding a Method to the Vehicle Class......Page 148
Returning from a Method......Page 150
Returning a Value......Page 151
Using Parameters......Page 154
Adding a Parameterized Method to Vehicle......Page 155
Project 4-1 Creating a Help Class......Page 158
Constructors......Page 164
Parameterized Constructors......Page 165
Adding a Constructor to the Vehicle Class......Page 166
The new Operator Revisited......Page 167
Garbage Collection and Finalizers......Page 168
The finalize( ) Method......Page 169
Project 4-2 Demonstrate Finalization......Page 170
The this Keyword......Page 172
Module 4 Mastery Check......Page 174
5 More Data Types and Operators......Page 176
One-Dimensional Arrays......Page 177
Project 5-1 Sorting an Array......Page 181
Two-Dimensional Arrays......Page 183
Irregular Arrays......Page 185
Initializing Multidimensional Arrays......Page 186
Alternative Array Declaration Syntax......Page 188
Assigning Array References......Page 189
Using the length Member......Page 190
Project 5-2 A Queue Class......Page 193
The For-Each Style for Loop......Page 197
Iterating Over Multidimensional Arrays......Page 200
Applying the Enhanced for......Page 202
Constructing Strings......Page 203
Operating on Strings......Page 204
Arrays of Strings......Page 206
Strings Are Immutable......Page 207
Using Command-Line Arguments......Page 208
The Bitwise Operators......Page 210
The Bitwise AND, OR, XOR, and NOT Operators......Page 211
The Shift Operators......Page 216
Project 5-3 A ShowBits Class......Page 218
The ? Operator......Page 221
Module 5 Mastery Check......Page 223
6 A Closer Look at Methods and Classes......Page 226
Java’s Access Specifiers......Page 227
Project 6-1 Improving the Queue Class......Page 233
Pass Objects to Methods......Page 234
How Arguments Are Passed......Page 236
Returning Objects......Page 239
Method Overloading......Page 241
Overloading Constructors......Page 247
Project 6-2 Overloading the Queue Constructor......Page 250
Recursion......Page 253
Understanding static......Page 255
Static Blocks......Page 258
Project 6-3 The Quicksort......Page 260
Introducing Nested and Inner Classes......Page 263
Varargs Basics......Page 267
Overloading Varargs Methods......Page 271
Varargs and Ambiguity......Page 272
Module 6 Mastery Check......Page 274
7 Inheritance......Page 276
Inheritance Basics......Page 277
Member Access and Inheritance......Page 280
Constructors and Inheritance......Page 283
Using super to Call Superclass Constructors......Page 285
Using super to Access Superclass Members......Page 291
Project 7-1 Extending the Vehicle Class......Page 292
Creating a Multilevel Hierarchy......Page 295
When Are Constructors Called?......Page 298
Superclass References and Subclass Objects......Page 299
Method Overriding......Page 305
Overridden Methods Support Polymorphism......Page 308
Applying Method Overriding to TwoDShape......Page 310
Using Abstract Classes......Page 315
final Prevents Inheritance......Page 320
Using final with Data Members......Page 321
The Object Class......Page 323
Module 7 Mastery Check......Page 324
8 Packages and Interfaces......Page 326
Defining a Package......Page 327
A Short Package Example......Page 329
Packages and Member Access......Page 331
A Package Access Example......Page 332
Understanding Protected Members......Page 334
Importing Packages......Page 336
Java’s Class Library Is Contained in Packages......Page 339
Interfaces......Page 340
Implementing Interfaces......Page 341
Using Interface References......Page 345
Project 8-1 Creating a Queue Interface......Page 347
Variables in Interfaces......Page 353
Interfaces Can Be Extended......Page 354
Module 8 Mastery Check......Page 355
9 Exception Handling......Page 358
Exception Handling Fundamentals......Page 359
Using try and catch......Page 360
A Simple Exception Example......Page 361
The Consequences of an Uncaught Exception......Page 364
Exceptions Enable You to Handle Errors Gracefully......Page 365
Using Multiple catch Statements......Page 367
Catching Subclass Exceptions......Page 368
Try Blocks Can Be Nested......Page 369
Rethrowing an Exception......Page 371
A Closer Look at Throwable......Page 373
Using finally......Page 375
Using throws......Page 377
Java’s Built-in Exceptions......Page 379
Creating Exception Subclasses......Page 381
Project 9-1 Adding Exceptions to the Queue Class......Page 384
Module 9 Mastery Check......Page 387
10 Using I/O......Page 390
Byte Streams and Character Streams......Page 391
The Predefined Streams......Page 392
Reading Console Input......Page 395
Writing Console Output......Page 397
Reading and Writing Files Using Byte Streams......Page 398
Inputting from a File......Page 399
Writing to a File......Page 401
Reading and Writing Binary Data......Page 403
Project 10-1 A File Comparison Utility......Page 407
Random Access Files......Page 409
Using Java’s Character-Based Streams......Page 413
Console Input Using Character Streams......Page 414
Console Output Using Character Streams......Page 417
Using a FileWriter......Page 419
Using a FileReader......Page 420
Using Java’s Type Wrappers to Convert Numeric Strings......Page 422
Project 10-2 Creating a Disk-Based Help System......Page 425
Module 10 Mastery Check......Page 431
11 Multithreaded Programming......Page 432
Multithreading Fundamentals......Page 433
The Thread Class and Runnable Interface......Page 434
Creating a Thread......Page 435
Some Simple Improvements......Page 438
Project 11-1 Extending Thread......Page 440
Creating Multiple Threads......Page 443
Determining When a Thread Ends......Page 446
Thread Priorities......Page 449
Using Synchronized Methods......Page 453
The synchronized Statement......Page 456
Thread Communication Using notify( ), wait( ), and notifyAll( )......Page 459
An Example That Uses wait( ) and notify( )......Page 460
Suspending, Resuming, and Stopping Threads......Page 466
Project 11-2 Using the Main Thread......Page 470
Module 11 Mastery Check......Page 472
12 Enumerations, Autoboxing, and Static Import......Page 474
Enumerations......Page 475
Enumeration Fundamentals......Page 476
The values( ) and valueOf( ) Methods......Page 479
Constructors, Methods, Instance Variables, and Enumerations......Page 481
Enumerations Inherit Enum......Page 483
Project 12-1 A Computer-Controlled Traffic Light......Page 485
Autoboxing......Page 491
Type Wrappers......Page 492
Autoboxing Fundamentals......Page 494
Autoboxing and Methods......Page 495
Autoboxing/Unboxing Occurs in Expressions......Page 497
A Word of Warning......Page 498
Static Import......Page 499
Annotations (Metadata)......Page 503
Module 12 Mastery Check......Page 506
13 Generics......Page 508
Generics Fundamentals......Page 509
A Simple Generics Example......Page 510
Generic Types Differ Based on Their Type Arguments......Page 514
A Generic Class with Two Type Parameters......Page 515
Bounded Types......Page 517
Using Wildcard Arguments......Page 521
Bounded Wildcards......Page 525
Generic Methods......Page 528
Generic Constructors......Page 531
Generic Interfaces......Page 532
Project 13-1 Create a Generic Queue......Page 535
Raw Types and Legacy Code......Page 540
Erasure......Page 543
Ambiguity Errors......Page 544
Type Parameters Can’t Be Instantiated......Page 546
Generic Array Restrictions......Page 547
Module 13 Mastery Check......Page 549
14 Applets, Events, and Miscellaneous Topics......Page 552
Applet Basics......Page 553
A Complete Applet Skeleton......Page 557
Applet Initialization and Termination......Page 559
Requesting Repainting......Page 560
Project 14-1 A Simple Banner Applet......Page 561
Using the Status Window......Page 565
Passing Parameters to Applets......Page 566
Event Handling......Page 568
Events......Page 570
Event Classes......Page 571
Event Listener Interfaces......Page 572
Handling Mouse Events......Page 574
A Simple Mouse Event Applet......Page 575
The transient and volatile Modifiers......Page 578
strictfp......Page 579
Native Methods......Page 580
Module 14 Mastery Check......Page 582
15 Introducing Swing......Page 584
The Origins and Design Philosophy of Swing......Page 585
Components......Page 588
The Top-Level Container Panes......Page 589
Layout Managers......Page 590
A First Simple Swing Program......Page 591
The First Swing Example Line by Line......Page 593
Use JButton......Page 598
Work with JTextField......Page 602
Create a JCheckBox......Page 607
Work with JList......Page 610
Project 15-1 A Swing-Based File Comparison Utility......Page 614
Create a Swing Applet......Page 622
What Next?......Page 625
Module 15 Mastery Check......Page 626
A: Answers to Mastery Checks......Page 628
B: Using Java’s Documentation Comments......Page 680
The javadoc Tags......Page 681
@deprecated......Page 682
{@literal}......Page 683
@serial......Page 684
{@value}......Page 685
What javadoc Outputs......Page 686
An Example that Uses Documentation Comments......Page 687
A......Page 690
D......Page 691
G......Page 692
J......Page 693
M......Page 694
R......Page 695
T......Page 696
X......Page 697