SKIP THE SHIPPING
Use code NOSHIP during checkout to save 40% on eligible eBooks, now through January 5. Shop now.
Register your product to gain access to bonus material or receive a coupon.
This eBook includes the following formats, accessible from your Account page after purchase:
EPUB The open industry format known for its reflowable content and usability on supported mobile devices.
PDF The popular standard, used most often with the free Acrobat® Reader® software.
This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.
In just 21 days you can acquire the knowledge and skills necessary to develop applications on your computer and apps that run on Android phones and tablets. With this complete tutorial you’ll quickly master the basics and then move on to more advanced features and concepts.
Completely updated for Java 8, this book teaches you about the Java language and how to use it to create applications for any computing environment and Android apps. By the time you have finished the book, you’ll have well-rounded knowledge of Java and the Java class libraries. Using your new skills, you will be able to develop your own programs for tasks such as web services, database connectivity, XML processing, and mobile programming.
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.
Please visit the website associated with Sams Teach Yourself Java in 21 Days at www.java21days.com.
Download the sample pages (includes Chapter 3 and Index)
Introduction
WEEK I: The Java Language
DAY 1: Getting Started with Java
The Java Language
History of the Language
Introduction to Java
Selecting a Development Tool
Object-Oriented Programming
Objects and Classes
Attributes and Behavior
Attributes of a Class of Objects
Behavior of a Class of Objects
Creating a Class
Running the Program
Organizing Classes and Class Behavior
Inheritance
Creating a Class Hierarchy
Inheritance in Action
Interfaces
Packages
DAY 2: The ABCs of Programming
Statements and Expressions
Variables and Data Types
Creating Variables
Naming Variables
Variable Types
Assigning Values to Variables
Constants
Comments
Literals
Number Literals
Boolean Literals
Character Literals
String Literals
Expressions and Operators
Arithmetic
More About Assignment
Incrementing and Decrementing
Comparisons
Logical Operators
Operator Precedence
String Arithmetic
DAY 3: Working with Objects
Creating New Objects
How Objects Are Constructed
A Note on Memory Management
Using Class and Instance Variables
Getting Values
Setting Values
Class Variables
Calling Methods
Formatting Strings
Nesting Method Calls
Class Methods
References to Objects
Casting Objects and Primitive Types
Casting Primitive Types
Casting Objects
Converting Primitive Types to Objects and Vice Versa
Comparing Object Values and Classes
Comparing Objects
Determining the Class of an Object
DAY 4: Lists, Logic, and Loops
Arrays
Declaring Array Variables
Creating Array Objects
Accessing Array Elements
Changing Array Elements
Multidimensional Arrays
Block Statements
If Conditionals
Switch Conditionals
The Ternary Operator
For Loops
While and Do Loops
While Loops
Do-While Loops
Breaking Out of Loops
Labeled Loops
DAY 5: Creating Classes and Methods
Defining Classes
Creating Instance and Class Variables
Defining Instance Variables
Class Variables
Creating Methods
Defining Methods
The this Keyword
Variable Scope and Method Definitions
Passing Arguments to Methods
Class Methods
Creating Java Applications
Helper Classes
Java Applications and Arguments
Passing Arguments to Java Applications
Handling Arguments in Your Java Application
Creating Methods with the Same Name
Constructors
Basic Constructors
Calling Another Constructor
Overloading Constructors
Overriding Methods
Creating Methods That Override Existing Methods
Calling the Original Method
Overriding Constructors
DAY 6: Packages, Interfaces, and Other Class Features
Modifiers
Access Control for Methods and Variables
Static Variables and Methods
Final Classes, Methods, and Variables
Variables
Methods
Classes
Abstract Classes and Methods
Packages
The import Declaration
Class Name Conflicts
Creating Your Own Packages
Picking a Package Name
Creating the Folder Structure
Adding a Class to a Package
Packages and Class Access Control
Interfaces
The Problem of Single Inheritance
Interfaces and Classes
Implementing and Using Interfaces
Implementing Multiple Interfaces
Other Uses of Interfaces
Creating and Extending Interfaces
New Interfaces
Methods Inside Interfaces
Extending Interfaces
Creating an Online Storefront
DAY 7: Exceptions and Threads
Exceptions
Exception Classes
Managing Exceptions
Exception Consistency Checking
Protecting Code and Catching Exceptions
The finally Clause
Declaring Methods That Might Throw Exceptions
The throws Clause
Which Exceptions Should You Throw
Passing on Exceptions
throws and Inheritance
Creating and Throwing Exceptions
Throwing Exceptions
Creating Your Own Exceptions
Combining throws, try, and throw
When Not to Use Exceptions
Bad Style Using Exceptions
Threads
Writing a Threaded Program
A Threaded Application
Stopping a Thread
WEEK II: The Java Class Library
DAY 8: Data Structures
Moving Beyond Arrays
Java Structures
Iterator
Bit Sets
Array Lists
Looping Through Data Structures
Stacks
Map
Hash Maps
Generics
Enumerations
DAY 9: Working with Swing
Creating an Application
Creating an Interface
Developing a Framework
Creating a Component
Adding Components to a Container
Working with Components
Image Icons
Labels
Text Fields
Text Areas
Scrolling Panes
Check Boxes and Radio Buttons
Combo Boxes
Lists
The Java Class Library
DAY 10: Building a Swing Interface
Swing Features
Standard Dialog Boxes
Using Dialog Boxes
Sliders
Scroll Panes
Toolbars
Progress Bars
Menus
Tabbed Panes
DAY 11: Arranging Components on a User Interface
Basic Interface Layout
Laying Out an Interface
Flow Layout
Box Layout
Grid Layout
Border Layout
Mixing Layout Managers
Card Layout
Using Card Layout in an Application
Cell Padding and Insets
DAY 12: Responding to User Input
Event Listeners
Setting Up Components
Event-Handling Methods
Working with Methods
Action Events
Focus Events
Item Events
Key Events
Mouse Events
Mouse Motion Events
Window Events
Using Adapter Classes
Using Inner Classes
DAY 13: Creating Java2D Graphics
The Graphics2D Class
The Graphics Coordinate System
Drawing Text
Improving Fonts and Graphics with Antialiasing
Finding Information About a Font
Color
Using Color Objects
Testing and Setting the Current Colors
Drawing Lines and Polygons
User and Device Coordinate Spaces
Specifying the Rendering Attributes
Creating Objects to Draw
Drawing Objects
DAY 14: Developing Swing Applications
Java Web Start
Using Java Web Start
Creating a JNLP File
Supporting Web Start on a Server
Additional JNLP Elements
Improving Performance with SwingWorker
WEEK III: Java Programming
DAY 15: Working with Input and Output
Introduction to Streams
Using a Stream
Filtering a Stream
Handling Exceptions
Byte Streams
File Streams
Filtering a Stream
Byte Filters
Character Streams
Reading Text Files
Writing Text Files
Files and Paths
DAY 16: Using Inner Classes and Closures
Inner Classes
Anonymous Inner Classes
Closures
DAY 17: Communicating Across the Internet
Networking in Java
Opening a Stream Over the Net
Sockets
Socket Servers
Testing the Server
The java.nio Package
Buffers
Channels
DAY 18: Accessing Databases with JDBC 4.2 and Derby
Java Database Connectivity
Database Drivers
Examining a Database
Reading Records from a Database
Writing Records to a Database
Moving Through Resultsets
DAY 19: Reading and Writing RSS Feeds
Using XML
Designing an XML Dialect
Processing XML with Java
Processing XML with XOM
Creating an XML Document
Modifying an XML Document
Formatting an XML Document
Evaluating XOM
DAY 20: XML Web Services
Introduction to XML-RPC
Communicating with XML-RPC
Sending a Request
Responding to a Request
Choosing an XML-RPC Implementation
Using an XML-RPC Web Service
Creating an XML-RPC Web Service
DAY 21: Writing Android Apps with Java
The History of Android
Writing an Android App
Organizing an Android Project
Creating the Program
Running the App
Designing an Android App
Preparing Resources
Configuring a Manifest File
Designing the Graphical User Interface
Writing Code
APPENDIXES
APPENDIX A: Using the NetBeans Integrated Development Environment
Installing NetBeans
Creating a New Project
Creating a New Java Class
Running the Application
Fixing Errors
Expanding and Shrinking a Pane
Exploring NetBeans
APPENDIX B: This Book’s Website
APPENDIX C: Fixing a Problem with the Android Studio Emulator
Problems Running an App
Install HAXM in Android Studio
Install HAXM on Your Computer
Checking BIOS Settings
APPENDIX D: Using the Java Development Kit
Choosing a Java Development Tool
Installing the Java Development Kit
Configuring the Java Development Kit
Using a Text Editor
Creating a Sample Program
Compiling and Running the Program in Windows
Setting Up the CLASSPATH Variable
APPENDIX E: Programming with the Java Development Kit
Overview of the JDK
The java Virtual Machine
The javac Compiler
The appletviewer Browser
The javadoc Documentation Tool
The jar Java File Archival Tool
The jdb Debugger
Debugging Applications
Debugging Applets
Advanced Debugging Commands
Using System Properties
The keytool and jarsigner Code Signing Tools