HAPPY BOOKSGIVING
Use code BOOKSGIVING during checkout to save 40%-55% on books and eBooks. Shop now.
Register your product to gain access to bonus material or receive a coupon.
Sams Teach Yourself Java in 21 Days
Covering Java 7 and Android App Development
Sams Teach Yourself Java in 21 Days continues to be one of the most popular, best-selling Java tutorials on the market. Written by an expert technical writer, it has been acclaimed for its clear and personable writing, for its extensive use of examples, and for its logical and complete organization.
The sixth edition of Sams Teach Yourself Java in 21 Days adds coverage of Java 7 and places a special emphasis on Android programming, capitalizing on the fastest-growing area of Java programming. There will be a new chapter on Android development and additional material where appropriate throughout the book. This edition also includes new material on using NetBeans, the free integrated IDE for Java.
No previous programming experience required. By following the 21 carefully organized lessons in this book, anyone can learn the basics of Java programming.
Learn at your own pace. You can work through each chapter sequentially to make sure you thoroughly understand all the concepts and methodologies, or you can focus on specific lessons to learn the techniques that interest you most.
Test your knowledge. Each chapter ends with a Workshop section filled with questions, answers, and exercises for further study. There are even certification practice questions.
Overcoming Android's Problems with JDK 7, Part 1
Overcoming Android's Problems with JDK 7, Part 2
Download the sample pages (includes Chapter 3 and Index)
Introduction 1
WEEK 1: The Java Language 7
1 Getting Started with Java 9
The Java Language 10
History of the Language 10
Introduction to Java 11
Selecting a Development Tool 12
Object-Oriented Programming 13
Objects and Classes 14
Attributes and Behavior 17
Attributes of a Class of Objects 17
Behavior of a Class of Objects 18
Creating a Class 19
Running the Program 23
Organizing Classes and Class Behavior 25
Inheritance
Creating a Class Hierarchy 27
Inheritance in Action 29
Interfaces 31
Packages 32
Summary 32
Q&A
Quiz 34
Questions 34
Certification Practice 34
Exercises 35
2 The ABCs of Programming 37
Statements and Expressions 38
Variables and Data Types 38
Creating Variables 39
Naming Variables 40
Variable Types 41
Assigning Values to Variables 43
Constants 43
Comments 45
Literals 46
Number Literals 47
Boolean Literals 48
Character Literals 49
String Literals 50
Expressions and Operators 51
Arithmetic 51
More About Assignment 53
Incrementing and Decrementing 54
Comparisons 56
Logical Operators 57
Operator Precedence 58
String Arithmetic 60
Summary 61
Q&A 62
Quiz 62
Questions 62
Certification Practice 63
Exercises 63
3 Working with Objects 65
Creating New Objects 66
Using new 66
How Objects Are Constructed 68
A Note on Memory Management 69
Using Class and Instance Variables 70
Getting Values 70
Setting Values 70
Class Variables 72
Calling Methods 73
Formatting Strings 75
Nesting Method Calls 75
Class Methods 76
References to Objects 77
Casting Objects and Primitive Types 79
Casting Primitive Types 80
Casting Objects 81
Converting Primitive Types to Objects and Vice Versa 83
Comparing Object Values and Classes 84
Comparing Objects 85
Determining the Class of an Object 87
Summary 87
Q&A 88
Quiz 88
Questions 89
Certification Practice 89
Exercises 90
4 Lists, Logic, and Loops 91
Arrays 92
Declaring Array Variables 92
Creating Array Objects 93
Accessing Array Elements 94
Changing Array Elements 95
Multidimensional Arrays 97
Block Statements 98
If Conditionals 99
Switch Conditionals 101
The Ternary Operator 107
For Loops 108
While and Do Loops 111
While Loops 111
Do-While Loops 113
Breaking Out of Loops 114
Labeled Loops 114
Summary 115
Q&A 115
Quiz 116
Questions 116
Certification Practice 117
Exercises 117
5 Creating Classes and Methods 119
Defining Classes 120
Creating Instance and Class Variables 120
Defining Instance Variables 120
Class Variables 121
Creating Methods 121
Defining Methods 122
The this Keyword 124
Variable Scope and Method Definitions 125
Passing Arguments to Methods 126
Class Methods 127
Creating Java Applications 129
Helper Classes 130
Java Applications and Command-Line Arguments 130
Passing Arguments to Java Applications 130
Handling Arguments in Your Java Application 131
Creating Methods with the Same Name 133
Constructors 136
Basic Constructors 137
Calling Another Constructor 138
Overloading Constructors 138
Overriding Methods 140
Creating Methods That Override Existing Methods 140
Calling the Original Method 142
Overriding Constructors 143
Summary 144
Q&A 145
Quiz 146
Questions 146
Certification Practice 146
Exercises 148
6 Packages, Interfaces, and Other Class Features 149
Modifiers 150
Access Control for Methods and Variables 151
Static Variables and Methods 157
Final Classes, Methods, and Variables 159
Variables 159
Methods 160
Classes 160
Abstract Classes and Methods 161
Packages 162
The import Declaration 163
Class Name Conflicts 165
Creating Your Own Packages 165
Picking a Package Name 165
Creating the Folder Structure 166
Adding a Class to a Package 166
Packages and Class Access Control 166
Interfaces 167
The Problem of Single Inheritance 167
Interfaces and Classes 168
Implementing and Using Interfaces 168
Implementing Multiple Interfaces 169
Other Uses of Interfaces 169
Creating and Extending Interfaces 170
New Interfaces 170
Methods Inside Interfaces 171
Extending Interfaces 172
Creating an Online Storefront 172
Inner Classes 179
Summary 181
Q&A 182
Quiz 183
Questions 183
Certification Practice 183
Exercises 185
7 Exceptions and Threads 187
Exceptions 188
Exception Classes 190
Managing Exceptions 191
Exception Consistency Checking 191
Protecting Code and Catching Exceptions 192
The finally Clause 195
Declaring Methods That Might Throw Exceptions 198
The throws Clause 199
Which Exceptions Should You Throw? 200
Passing on Exceptions 201
throws and Inheritance 202
Creating and Throwing Your Own Exceptions 203
Throwing Exceptions 203
Creating Your Own Exceptions 204
Combining throws, try, and throw 204
When and When Not to Use Exceptions 205
When to Use Exceptions 205
When Not to Use Exceptions 206
Bad Style Using Exceptions 206
Threads 207
Writing a Threaded Program 207
A Threaded Application 209
Stopping a Thread 214
Summary 215
Q&A 215
Quiz 217
Questions 217
Certification Practice 217
Exercises 218
WEEK 2: The Java Class Library 219
8 Data Structures 221
Moving Beyond Arrays 222
Java Structures 222
Iterator 224
Bit Sets 225
Array Lists 228
Looping Through Data Structures 231
Stacks 233
Map 235
Hash Maps 236
Generics 241
Summary 245
Q&A 245
Quiz 246
Questions 246
Certification Practice 246
Exercises 247
9 Working with Swing 249
Creating an Application 250
Creating an Interface 251
Developing a Framework 254
Creating a Component 255
Adding Components to a Container 256
Working with Components 258
Image Icons 259
Labels 261
Text Fields 262
Text Areas 263
Scrolling Panes 265
Check Boxes and Radio Buttons 266
Combo Boxes 269
Lists 271
Summary 273
Q&A 273
Quiz 274
Questions 274
Certification Practice 274
Exercises 275
10 Building a Swing Interface 277
Swing Features 278
Standard Dialog Boxes 278
Using Dialog Boxes 283
Sliders 286
Scroll Panes 288
Toolbars 289
Progress Bars 292
Menus 295
Tabbed Panes 299
Summary 301
Q&A 301
Quiz 302
Questions 302
Certification Practice 303
Exercises 304
11 Arranging Components on a User Interface 305
Basic Interface Layout 306
Laying Out an Interface 306
Flow Layout 307
Box Layout 309
Grid Layout 311
Border Layout 314
Mixing Layout Managers 316
Card Layout 317
Using Card Layout in an Application 319
Grid Bag Layout 325
Designing the Grid 327
Creating the Grid 329
Cell Padding and Insets 334
Summary 335
Q&A 335
Quiz 336
Questions 336
Certification Practice 337
Exercises 338
12 Responding to User Input 339
Event Listeners 340
Setting Up Components 341
Event-Handling Methods 342
Working with Methods 345
Action Events 345
Focus Events 346
Item Events 349
Key Events 351
Mouse Events 352
Mouse Motion Events 352
Window Events 357
Using Adapter Classes 357
Using Inner Classes 359
Summary 362
Q&A 362
Quiz 363
Questions 363
Certification Practice 363
Exercises 365
13 Creating Java2D Graphics 367
The Graphics2D Class 368
The Graphics Coordinate System 369
Drawing Text 370
Improving Fonts and Graphics with Antialiasing 372
Finding Information About a Font 372
Color 375
Using Color Objects 375
Testing and Setting the Current Colors 376
Drawing Lines and Polygons 377
User and Device Coordinate Spaces 377
Specifying the Rendering Attributes 378
Creating Objects to Draw 381
Drawing Objects 384
Summary 387
Q&A 387
Quiz 388
Questions 388
Certification Practice 388
Exercises 389
14 Developing Swing Applications 391
Java Web Start 392
Using Java Web Start 395
Creating a JNLP File 396
Supporting Web Start on a Server 405
Additional JNLP Elements 406
Improving Performance with SwingWorker 408
Summary 413
Q&A 414
Quiz 414
Questions 414
Certification Practice 415
Exercises 416
WEEK 3: Java Programming 417
15 Working with Input and Output 419
Introduction to Streams 420
Using a Stream 420
Filtering a Stream 421
Handling Exceptions 421
Byte Streams 422
File Streams 422
Filtering a Stream 426
Byte Filters 427
Character Streams 436
Reading Text Files 436
Writing Text Files 439
Files and Paths 440
Summary 443
Q&A 443
Quiz 444
Questions 444
Certification Practice 445
Exercises 446
16 Serializing and Examining Objects 447
Object Serialization 448
Object Output Streams 449
Object Input Streams 452
Transient Variables 455
Checking an Object’s Serialized Fields 456
Inspecting Classes and Methods with Reflection 457
Inspecting and Creating Classes 457
Working with Each Part of a Class 459
Inspecting a Class 461
Summary 463
Q&A 463
Quiz 464
Questions 464
Certification Practice 465
Exercises 465
17 Communicating Across the Internet 467
Networking in Java 468
Opening a Stream Over the Net 468
Sockets 473
Socket Servers 476
Testing the Server 479
The java.nio Package 481
Buffers 481
Channels 485
Summary 495
Q&A 496
Quiz 496
Questions 496
Certification Practice 497
Exercises 498
18 Accessing Databases with JDBC 4.1 and Derby 499
Java Database Connectivity 500
Database Drivers 501
Examining a Database 501
Reading Records from a Database 503
Writing Records to a Database 509
Moving Through Resultsets 516
Summary 517
Q&A 518
Quiz 518
Questions 518
Certification Practice 519
Exercises 519
19 Reading and Writing RSS Feeds 521
Using XML 522
Designing an XML Dialect 525
Processing XML with Java 526
Processing XML with XOM 526
Creating an XML Document 528
Modifying an XML Document 532
Formatting an XML Document 536
Evaluating XOM 538
Summary 540
Q&A 541
Quiz 542
Questions 542
Certification Practice. 542
Exercises 543
20 XML Web Services 545
Introduction to XML-RPC 546
Communicating with XML-RPC 547
Sending a Request 548
Responding to a Request 549
Choosing an XML-RPC Implementation 550
Using an XML-RPC Web Service 552
Creating an XML-RPC Web Service 555
Summary 560
Q&A 561
Quiz 561
Questions 561
Certification Practice 562
Exercises 563
21 Writing Android Apps for Java 565
The History of Android 566
Writing an Android App 567
Organizing an Android Project 569
Creating the Program 570
Using an Android Emulator 573
Creating a Debug Configuration 574
Running the App 575
Designing an Android App 577
Preparing Resources 578
Configuring a Manifest File 582
Designing the Graphical User Interface 582
Writing Code 585
Summary 591
Q&A 591
Quiz 592
Questions 592
Certification Practice 592
Exercises 592
Appendixes 593
A Using the NetBeans Integrated Development Environment 595
B This Book’s Website 603
C Setting Up an Android Development Environment 605
D Using the Java Development Kit 613
E Programming with the Java Development Kit 635
Quiz Answers 659