You are previewing Sams Teach Yourself Java™ in 21 Days (Covering Java 8), Seventh Edition.
O'Reilly logo
Sams Teach Yourself Java™ in 21 Days (Covering Java 8), Seventh Edition

Book Description

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.

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.

  • Completely revised, updated, and expanded to cover the latest features of Java 8

  • Learn to develop Java applications and Android apps using NetBeans and Google’s new Android Studio—two excellent (and free!) programming platforms

  • Covers new features of Java 8 such as closures, the most eagerly anticipated language feature in years

  • Easy-to-understand, practical examples clearly illustrate the fundamentals of Java programming

  • Discover how Swing can help you quickly develop programs with a graphical user interface

  • Find out about JDBC 4.2 programming with the Derby database and XML parsing with the open source XOM class library

  • Learn how to use streams to write programs that communicate with the Internet, including socket programming, buffers, channels, and URL handling.

  • Register your book at informit.com/register for convenient access to downloads, updates, and corrections as they become available.

    Table of Contents

    1. About This E-Book
    2. Title Page
    3. Copyright Page
    4. Contents at a Glance
    5. Table of Contents
    6. About the Author
    7. Dedication
    8. Acknowledgments
    9. We Want to Hear from You!
    10. Reader Services
    11. Introduction
      1. How This Book Is Organized
      2. Who Should Read This Book
      3. Conventions Used in This Book
    12. Week I: The Java Language
      1. Day 1. Getting Started with Java
        1. The Java Language
          1. History of the Language
          2. Introduction to Java
          3. Selecting a Development Tool
        2. Object-Oriented Programming
        3. Objects and Classes
        4. Attributes and Behavior
          1. Attributes of a Class of Objects
          2. Behavior of a Class of Objects
          3. Creating a Class
          4. Running the Program
        5. Organizing Classes and Class Behavior
          1. Inheritance
          2. Creating a Class Hierarchy
          3. Inheritance in Action
          4. Interfaces
          5. Packages
        6. Summary
        7. Q&A
        8. Quiz
          1. Questions
          2. Answers
        9. Certification Practice
        10. Exercises
      2. Day 2. The ABCs of Programming
        1. Statements and Expressions
        2. Variables and Data Types
          1. Creating Variables
          2. Naming Variables
          3. Variable Types
          4. Assigning Values to Variables
          5. Constants
        3. Comments
        4. Literals
          1. Number Literals
          2. Boolean Literals
          3. Character Literals
          4. String Literals
        5. Expressions and Operators
          1. Arithmetic
          2. More About Assignment
          3. Incrementing and Decrementing
          4. Comparisons
          5. Logical Operators
          6. Operator Precedence
        6. String Arithmetic
        7. Summary
        8. Q&A
        9. Quiz
          1. Questions
          2. Answers
        10. Certification Practice
        11. Exercises
      3. Day 3. Working with Objects
        1. Creating New Objects
          1. Using new
          2. How Objects Are Constructed
          3. A Note on Memory Management
        2. Using Class and Instance Variables
          1. Getting Values
          2. Setting Values
          3. Class Variables
        3. Calling Methods
          1. Formatting Strings
          2. Nesting Method Calls
          3. Class Methods
        4. References to Objects
        5. Casting Objects and Primitive Types
          1. Casting Primitive Types
          2. Casting Objects
          3. Converting Primitive Types to Objects and Vice Versa
        6. Comparing Object Values and Classes
          1. Comparing Objects
          2. Determining the Class of an Object
        7. Summary
        8. Q&A
        9. Quiz
          1. Questions
          2. Answers
        10. Certification Practice
        11. Exercises
      4. Day 4. Lists, Logic, and Loops
        1. Arrays
          1. Declaring Array Variables
          2. Creating Array Objects
          3. Accessing Array Elements
          4. Changing Array Elements
          5. Multidimensional Arrays
          6. Block Statements
        2. If Conditionals
        3. Switch Conditionals
        4. The Ternary Operator
        5. For Loops
        6. While and Do Loops
          1. While Loops
          2. Do-While Loops
        7. Breaking Out of Loops
          1. Labeled Loops
        8. Summary
        9. Q&A
        10. Quiz
          1. Questions
          2. Answers
        11. Certification Practice
        12. Exercises
      5. Day 5. Creating Classes and Methods
        1. Defining Classes
        2. Creating Instance and Class Variables
          1. Defining Instance Variables
          2. Class Variables
        3. Creating Methods
          1. Defining Methods
          2. The this Keyword
          3. Variable Scope and Method Definitions
          4. Passing Arguments to Methods
          5. Class Methods
        4. Creating Java Applications
          1. Helper Classes
        5. Java Applications and Arguments
          1. Passing Arguments to Java Applications
          2. Handling Arguments in Your Java Application
        6. Creating Methods with the Same Name
        7. Constructors
          1. Basic Constructors
          2. Calling Another Constructor
          3. Overloading Constructors
        8. Overriding Methods
          1. Creating Methods That Override Existing Methods
          2. Calling the Original Method
          3. Overriding Constructors
        9. Summary
        10. Q&A
        11. Quiz
          1. Questions
          2. Answers
        12. Certification Practice
        13. Exercises
      6. Day 6. Packages, Interfaces, and Other Class Features
        1. Modifiers
          1. Access Control for Methods and Variables
        2. Static Variables and Methods
        3. Final Classes, Methods, and Variables
          1. Variables
          2. Methods
          3. Classes
        4. Abstract Classes and Methods
        5. Packages
          1. The import Declaration
          2. Class Name Conflicts
        6. Creating Your Own Packages
          1. Picking a Package Name
          2. Creating the Folder Structure
          3. Adding a Class to a Package
          4. Packages and Class Access Control
        7. Interfaces
          1. The Problem of Single Inheritance
          2. Interfaces and Classes
          3. Implementing and Using Interfaces
          4. Implementing Multiple Interfaces
          5. Other Uses of Interfaces
        8. Creating and Extending Interfaces
          1. New Interfaces
          2. Methods Inside Interfaces
          3. Extending Interfaces
          4. Creating an Online Storefront
        9. Summary
        10. Q&A
        11. Quiz
          1. Questions
          2. Answers
        12. Certification Practice
        13. Exercises
      7. Day 7. Exceptions and Threads
        1. Exceptions
          1. Exception Classes
        2. Managing Exceptions
          1. Exception Consistency Checking
          2. Protecting Code and Catching Exceptions
          3. The finally Clause
        3. Declaring Methods That Might Throw Exceptions
          1. The throws Clause
          2. Which Exceptions Should You Throw?
          3. Passing on Exceptions
          4. throws and Inheritance
        4. Creating and Throwing Exceptions
          1. Throwing Exceptions
          2. Creating Your Own Exceptions
          3. Combining throws, try, and throw
        5. When Not to Use Exceptions
          1. Bad Style Using Exceptions
        6. Threads
          1. Writing a Threaded Program
          2. A Threaded Application
          3. Stopping a Thread
        7. Summary
        8. Q&A
        9. Quiz
          1. Questions
          2. Answers
        10. Certification Practice
        11. Exercises
    13. Week II: The Java Class Library
      1. Day 8. Data Structures
        1. Moving Beyond Arrays
        2. Java Structures
          1. Iterator
          2. Bit Sets
          3. Array Lists
          4. Looping Through Data Structures
          5. Stacks
          6. Map
          7. Hash Maps
        3. Generics
        4. Enumerations
        5. Summary
        6. Q&A
        7. Quiz
          1. Questions
          2. Answers
        8. Certification Practice
        9. Exercises
      2. Day 9. Working with Swing
        1. Creating an Application
          1. Creating an Interface
          2. Developing a Framework
          3. Creating a Component
          4. Adding Components to a Container
        2. Working with Components
          1. Image Icons
          2. Labels
          3. Text Fields
          4. Text Areas
          5. Scrolling Panes
          6. Check Boxes and Radio Buttons
          7. Combo Boxes
        3. Lists
        4. The Java Class Library
        5. Summary
        6. Q&A
        7. Quiz
          1. Questions
          2. Answers
        8. Certification Practice
        9. Exercises
      3. Day 10. Building a Swing Interface
        1. Swing Features
          1. Standard Dialog Boxes
          2. Using Dialog Boxes
          3. Sliders
          4. Scroll Panes
          5. Toolbars
          6. Progress Bars
          7. Menus
          8. Tabbed Panes
        2. Summary
        3. Q&A
        4. Quiz
          1. Questions
          2. Answers
        5. Certification Practice
        6. Exercises
      4. Day 11. Arranging Components on a User Interface
        1. Basic Interface Layout
          1. Laying Out an Interface
          2. Flow Layout
          3. Box Layout
          4. Grid Layout
          5. Border Layout
        2. Mixing Layout Managers
        3. Card Layout
          1. Using Card Layout in an Application
          2. Cell Padding and Insets
        4. Summary
        5. Q&A
        6. Quiz
          1. Questions
          2. Answers
        7. Certification Practice
        8. Exercises
      5. Day 12. Responding to User Input
        1. Event Listeners
          1. Setting Up Components
          2. Event-Handling Methods
        2. Working with Methods
          1. Action Events
          2. Focus Events
          3. Item Events
          4. Key Events
          5. Mouse Events
          6. Mouse Motion Events
          7. Window Events
          8. Using Adapter Classes
          9. Using Inner Classes
        3. Summary
        4. Q&A
        5. Quiz
          1. Questions
          2. Answers
        6. Certification Practice
        7. Exercises
      6. Day 13. Creating Java2D Graphics
        1. The Graphics2D Class
          1. The Graphics Coordinate System
        2. Drawing Text
          1. Improving Fonts and Graphics with Antialiasing
          2. Finding Information About a Font
        3. Color
          1. Using Color Objects
          2. Testing and Setting the Current Colors
        4. Drawing Lines and Polygons
          1. User and Device Coordinate Spaces
          2. Specifying the Rendering Attributes
          3. Creating Objects to Draw
          4. Drawing Objects
        5. Summary
        6. Q&A
        7. Quiz
          1. Questions
          2. Answers
        8. Certification Practice
        9. Exercises
      7. Day 14. Developing Swing Applications
        1. Java Web Start
        2. Using Java Web Start
          1. Creating a JNLP File
          2. Supporting Web Start on a Server
          3. Additional JNLP Elements
        3. Improving Performance with SwingWorker
        4. Summary
        5. Q&A
        6. Quiz
          1. Questions
          2. Answers
        7. Certification Practice
        8. Exercises
    14. Week III: Java Programming
      1. Day 15. Working with Input and Output
        1. Introduction to Streams
          1. Using a Stream
          2. Filtering a Stream
          3. Handling Exceptions
        2. Byte Streams
          1. File Streams
        3. Filtering a Stream
          1. Byte Filters
        4. Character Streams
          1. Reading Text Files
          2. Writing Text Files
        5. Files and Paths
        6. Summary
        7. Q&A
        8. Quiz
          1. Questions
          2. Answers
        9. Certification Practice
        10. Exercises
      2. Day 16. Using Inner Classes and Closures
        1. Inner Classes
          1. Anonymous Inner Classes
        2. Closures
        3. Summary
        4. Q&A
        5. Quiz
          1. Questions
          2. Answers
        6. Certification Practice
        7. Exercises
      3. Day 17. Communicating Across the Internet
        1. Networking in Java
          1. Opening a Stream Over the Net
          2. Sockets
          3. Socket Servers
          4. Testing the Server
        2. The java.nio Package
          1. Buffers
          2. Channels
        3. Summary
        4. Q&A
        5. Quiz
          1. Questions
          2. Answers
        6. Certification Practice
        7. Exercises
      4. Day 18. Accessing Databases with JDBC 4.2 and Derby
        1. Java Database Connectivity
          1. Database Drivers
          2. Examining a Database
          3. Reading Records from a Database
          4. Writing Records to a Database
          5. Moving Through Resultsets
        2. Summary
        3. Q&A
        4. Quiz
          1. Questions
          2. Answers
        5. Certification Practice
        6. Exercises
      5. Day 19. Reading and Writing RSS Feeds
        1. Using XML
        2. Designing an XML Dialect
        3. Processing XML with Java
        4. Processing XML with XOM
          1. Creating an XML Document
          2. Modifying an XML Document
          3. Formatting an XML Document
          4. Evaluating XOM
        5. Summary
        6. Q&A
        7. Quiz
          1. Questions
          2. Answers
        8. Certification Practice
        9. Exercises
      6. Day 20. XML Web Services
        1. Introduction to XML-RPC
        2. Communicating with XML-RPC
          1. Sending a Request
          2. Responding to a Request
        3. Choosing an XML-RPC Implementation
        4. Using an XML-RPC Web Service
        5. Creating an XML-RPC Web Service
        6. Summary
        7. Q&A
        8. Quiz
          1. Questions
          2. Answers
        9. Certification Practice
        10. Exercises
      7. Day 21. Writing Android Apps with Java
        1. The History of Android
        2. Writing an Android App
          1. Organizing an Android Project
          2. Creating the Program
        3. Running the App
        4. Designing an Android App
          1. Preparing Resources
          2. Configuring a Manifest File
          3. Designing the Graphical User Interface
          4. Writing Code
        5. Summary
        6. Q&A
        7. Quiz
          1. Questions
          2. Answers
        8. Certification Practice
        9. Exercises
    15. Week IV: Appendices
      1. Appendix A. Using the NetBeans Integrated Development Environment
        1. Installing NetBeans
        2. Creating a New Project
        3. Creating a New Java Class
        4. Running the Application
        5. Fixing Errors
        6. Expanding and Shrinking a Pane
        7. Exploring NetBeans
      2. Appendix B. This Book’s Website
      3. Appendix C. Fixing a Problem with the Android Studio Emulator
        1. Problems Running an App
          1. Install HAXM in Android Studio
          2. Install HAXM on Your Computer
          3. Checking BIOS Settings
      4. Appendix D. Using the Java Development Kit
        1. Choosing a Java Development Tool
          1. Installing the Java Development Kit
        2. Configuring the Java Development Kit
          1. Using a Command-Line Interface
          2. Opening Folders in MS-DOS
          3. Creating Folders in MS-DOS
          4. Running Programs in MS-DOS
          5. Correcting Configuration Errors
        3. Using a Text Editor
        4. Creating a Sample Program
          1. Compiling and Running the Program in Windows
        5. Setting Up the CLASSPATH Variable
          1. Setting the Classpath on Most Windows Versions
          2. Setting the CLASSPATH on Windows 98 or Me
      5. Appendix E. Programming with the Java Development Kit
        1. Overview of the JDK
        2. The java Virtual Machine
        3. The javac Compiler
        4. The appletviewer Browser
        5. The javadoc Documentation Tool
        6. The jar Java File Archival Tool
        7. The jdb Debugger
          1. Debugging Applications
          2. Debugging Applets
          3. Advanced Debugging Commands
        8. Using System Properties
        9. The keytool and jarsigner Code Signing Tools
    16. Index
    17. Code Snippets