You are previewing Java A Beginner's Guide, 6th Edition.
O'Reilly logo
Java A Beginner's Guide, 6th Edition

Book Description

Essential Java Programming Skills--Made Easy!

Fully updated for Java Platform, Standard Edition 8 (Java SE 8), Java: A Beginner's Guide, Sixth Edition gets you started programming in Java right away. Bestselling programming author Herb Schildt begins with the basics, such as how to create, compile, and run a Java program. He then moves on to the keywords, syntax, and constructs that form the core of the Java language. This Oracle Press resource also covers some of Java's more advanced features, including multithreaded programming, generics, and Swing. Of course, new Java SE 8 features such as lambda expressions and default interface methods are described. An introduction to JavaFX, Java's newest GUI, concludes this step-by-step tutorial.

Designed for Easy Learning:

  • Key Skills & Concepts -- Chapter-opening lists of specific skills covered in the chapter
  • Ask the Expert -- Q&A sections filled with bonus information and helpful tips
  • Try This -- Hands-on exercises that show you how to apply your skills
  • Self Tests -- End-of-chapter quizzes to reinforce your skills
  • Annotated Syntax -- Example code with commentary that describes the programming techniques being illustrated

The book's code examples are available FREE for download.

Table of Contents

  1. Cover 
  2. About the Author
    1. About the Technical Reviewer
  3. Title Page
  4. Copyright Page
  5. Contents at a Glance
  6. Contents 
  7. Introduction
  8. Chapter 1: Java Fundamentals
    1. The Origins of Java
      1. How Java Relates to C and C++
      2. How Java Relates to C#
    2. Java’s Contribution to the Internet
      1. Java Applets
      2. Security
      3. Portability
    3. Java’s Magic: The Bytecode
    4. The Java Buzzwords
    5. Object-Oriented Programming
      1. Encapsulation
      2. Polymorphism
      3. Inheritance
    6. Obtaining the Java Development Kit
    7. A First Simple Program
      1. Entering the Program
      2. Compiling the Program
      3. The First Sample Program Line by Line
    8. Handling Syntax Errors
    9. A Second Simple Program
    10. Another Data Type
    11. Try This 1-1: Converting Gallons to Liters
    12. Two Control Statements
      1. The if Statement
      2. The for Loop
    13. Create Blocks of Code
    14. Semicolons and Positioning
    15. Indentation Practices
    16. Try This 1-2: Improving the Gallons-to-Liters Converter
    17. The Java Keywords
    18. Identifiers in Java
    19. The Java Class Libraries
    20. Chapter 1: Self Test
  9. Chapter 2: Introducing Data Types and Operators
    1. Why Data Types Are Important
    2. Java’s Primitive Types
      1. Integers
      2. Floating-Point Types
      3. Characters
    3. The Boolean Type
    4. Try This 2-1: How Far Away Is the Lightning?
    5. Literals
      1. Hexadecimal, Octal, and Binary Literals
      2. Character Escape Sequences
      3. String Literals
    6. A Closer Look at Variables
      1. Initializing a Variable
      2. Dynamic Initialization
    7. The Scope and Lifetime of Variables
    8. Operators
    9. Arithmetic Operators
      1. Increment and Decrement
    10. Relational and Logical Operators
    11. Short-Circuit Logical Operators
    12. The Assignment Operator
    13. Shorthand Assignments
    14. Type Conversion in Assignments
    15. Casting Incompatible Types
    16. Operator Precedence
    17. Try This 2-2: Display a Truth Table for the Logical Operators
    18. Expressions
      1. Type Conversion in Expressions
      2. Spacing and Parentheses
    19. Chapter 2: Self Test
  10. Chapter 3: Program Control Statements
    1. Input Characters from the Keyboard
    2. The if Statement
    3. Nested ifs
    4. The if-else-if Ladder
    5. The switch Statement
    6. Nested switch Statements
    7. Try This 3-1: Start Building a Java Help System
    8. The for Loop
    9. Some Variations on the for Loop
    10. Missing Pieces
      1. The Infinite Loop
    11. Loops with No Body
    12. Declaring Loop Control Variables Inside the for Loop
    13. The Enhanced for Loop
    14. The while Loop
    15. The do-while Loop
    16. Try This 3-2: Improve the Java Help System
    17. Use break to Exit a Loop
    18. Use break as a Form of goto
    19. Use continue
    20. Try This 3-3: Finish the Java Help System
    21. Nested Loops
    22. Chapter 3: Self Test
  11. Chapter 4: Introducing Classes, Objects, and Methods
    1. Class Fundamentals
      1. The General Form of a Class
      2. Defining a Class
    2. How Objects Are Created
    3. Reference Variables and Assignment
    4. Methods
      1. Adding a Method to the Vehicle Class
    5. Returning from a Method
    6. Returning a Value
    7. Using Parameters
      1. Adding a Parameterized Method to Vehicle
    8. Try This 4-1: Creating a Help Class
    9. Constructors
    10. Parameterized Constructors
    11. Adding a Constructor to the Vehicle Class
    12. The new Operator Revisited
    13. Garbage Collection
    14. The finalize( ) Method
    15. Try This 4-2: Demonstrate Garbage Collection and Finalization
    16. The this Keyword
    17. Chapter 4: Self Test
  12. Chapter 5: More Data Types and Operators
    1. Arrays
      1. One-Dimensional Arrays
    2. Try This 5-1: Sorting an Array
    3. Multidimensional Arrays
      1. Two-Dimensional Arrays
    4. Irregular Arrays
      1. Arrays of Three or More Dimensions
      2. Initializing Multidimensional Arrays
    5. Alternative Array Declaration Syntax
    6. Assigning Array References
    7. Using the length Member
    8. Try This 5-2: A Queue Class
    9. The For-Each Style for Loop
      1. Iterating Over Multidimensional Arrays
      2. Applying the Enhanced for
    10. Strings
      1. Constructing Strings
      2. Operating on Strings
      3. Arrays of Strings
      4. Strings Are Immutable
      5. Using a String to Control a switch Statement
    11. Using Command-Line Arguments
    12. The Bitwise Operators
      1. The Bitwise AND, OR, XOR, and NOT Operators
      2. The Shift Operators
      3. Bitwise Shorthand Assignments
    13. Try This 5-3: A ShowBits Class
    14. The ? Operator
    15. Chapter 5: Self Test
  13. Chapter 6: A Closer Look at Methods and Classes
    1. Controlling Access to Class Members
      1. Java’s Access Modifiers
    2. Try This 6-1: Improving the Queue Class
    3. Pass Objects to Methods
      1. How Arguments Are Passed
    4. Returning Objects
    5. Method Overloading
    6. Overloading Constructors
    7. Try This 6-2: Overloading the Queue Constructor
    8. Recursion
    9. Understanding static
      1. Static Blocks
    10. Try This 6-3: The Quicksort
    11. Introducing Nested and Inner Classes
    12. Varargs: Variable-Length Arguments
      1. Varargs Basics
      2. Overloading Varargs Methods
      3. Varargs and Ambiguity
    13. Chapter 6: Self Test
  14. Chapter 7: Inheritance
    1. Inheritance Basics
    2. Member Access and Inheritance
    3. Constructors and Inheritance
    4. Using super to Call Superclass Constructors
    5. Using super to Access Superclass Members
    6. Try This 7-1: Extending the Vehicle Class
    7. Creating a Multilevel Hierarchy
    8. When Are Constructors Executed?
    9. Superclass References and Subclass Objects
    10. Method Overriding
    11. Overridden Methods Support Polymorphism
    12. Why Overridden Methods?
      1. Applying Method Overriding to TwoDShape
    13. Using Abstract Classes
    14. Using final
      1. final Prevents Overriding
      2. final Prevents Inheritance
      3. Using final with Data Members
    15. The Object Class
    16. Chapter 7: Self Test
  15. Chapter 8: Packages and Interfaces
    1. Packages
      1. Defining a Package
      2. Finding Packages and CLASSPATH
      3. A Short Package Example
    2. Packages and Member Access
      1. A Package Access Example
    3. Understanding Protected Members
    4. Importing Packages
    5. Java’s Class Library Is Contained in Packages
    6. Interfaces
    7. Implementing Interfaces
    8. Using Interface References
    9. Try This 8-1: Creating a Queue Interface
    10. Variables in Interfaces
    11. Interfaces Can Be Extended
    12. Default Interface Methods
      1. Default Method Fundamentals
      2. A More Practical Example of a Default Method
      3. Multiple Inheritance Issues
    13. Use static Methods in an Interface
    14. Final Thoughts on Packages and Interfaces
    15. Chapter 8: Self Test
  16. Chapter 9: Exception Handling
    1. The Exception Hierarchy
    2. Exception Handling Fundamentals
      1. Using try and catch
      2. A Simple Exception Example
    3. The Consequences of an Uncaught Exception
      1. Exceptions Enable You to Handle Errors Gracefully
    4. Using Multiple catch Statements
    5. Catching Subclass Exceptions
    6. Try Blocks Can Be Nested
    7. Throwing an Exception
      1. Rethrowing an Exception
    8. A Closer Look at Throwable
    9. Using finally
    10. Using throws
    11. Three Recently Added Exception Features
    12. Java’s Built-in Exceptions
    13. Creating Exception Subclasses
    14. Try This 9-1: Adding Exceptions to the Queue Class
    15. Chapter 9: Self Test
  17. Chapter 10: Using I/O
    1. Java’s I/O Is Built upon Streams
    2. Byte Streams and Character Streams
    3. The Byte Stream Classes
    4. The Character Stream Classes
    5. The Predefined Streams
    6. Using the Byte Streams
      1. Reading Console Input
      2. Writing Console Output
    7. Reading and Writing Files Using Byte Streams
      1. Inputting from a File
      2. Writing to a File
    8. Automatically Closing a File
    9. Reading and Writing Binary Data
    10. Try This 10-1: A File Comparison Utility
    11. Random-Access Files
    12. Using Java’s Character-Based Streams
      1. Console Input Using Character Streams
      2. Console Output Using Character Streams
    13. File I/O Using Character Streams
      1. Using a FileWriter
      2. Using a FileReader
    14. Using Java’s Type Wrappers to Convert Numeric Strings
    15. Try This 10-2: Creating a Disk-Based Help System
    16. Chapter 10: Self Test
  18. Chapter 11: Multithreaded Programming
    1. Multithreading Fundamentals
    2. The Thread Class and Runnable Interface
    3. Creating a Thread
      1. Some Simple Improvements
    4. Try This 11-1: Extending Thread
    5. Creating Multiple Threads
    6. Determining When a Thread Ends
    7. Thread Priorities
    8. Synchronization
    9. Using Synchronized Methods
    10. The synchronized Statement
    11. Thread Communication Using notify( ), wait( ), and notifyAll( )
      1. An Example That Uses wait( ) and notify( )
    12. Suspending, Resuming, and Stopping Threads
    13. Try This 11-2: Using the Main Thread
    14. Chapter 11: Self Test
  19. Chapter 12: Enumerations, Autoboxing, Static Import, and Annotations
    1. Enumerations
      1. Enumeration Fundamentals
    2. Java Enumerations Are Class Types
    3. The values( ) and valueOf( ) Methods
    4. Constructors, Methods, Instance Variables, and Enumerations
      1. Two Important Restrictions
    5. Enumerations Inherit Enum
    6. Try This 12-1: A Computer-Controlled Traffic Light
    7. Autoboxing
    8. Type Wrappers
    9. Autoboxing Fundamentals
    10. Autoboxing and Methods
    11. Autoboxing/Unboxing Occurs in Expressions
      1. A Word of Warning
    12. Static Import
    13. Annotations (Metadata)
    14. Chapter 12: Self Test
  20. Chapter 13: Generics
    1. Generics Fundamentals
    2. A Simple Generics Example
      1. Generics Work Only with Reference Types
      2. Generic Types Differ Based on Their Type Arguments
      3. A Generic Class with Two Type Parameters
      4. The General Form of a Generic Class
    3. Bounded Types
    4. Using Wildcard Arguments
    5. Bounded Wildcards
    6. Generic Methods
    7. Generic Constructors
    8. Generic Interfaces
    9. Try This 13-1: Create a Generic Queue
    10. Raw Types and Legacy Code
    11. Type Inference with the Diamond Operator
    12. Erasure
    13. Ambiguity Errors
    14. Some Generic Restrictions
      1. Type Parameters Can’t Be Instantiated
      2. Restrictions on Static Members
      3. Generic Array Restrictions
      4. Generic Exception Restriction
    15. Continuing Your Study of Generics
    16. Chapter 13: Self Test
  21. Chapter 14: Lambda Expressions and Method References
    1. Introducing Lambda Expressions
      1. Lambda Expression Fundamentals
      2. Functional Interfaces
      3. Lambda Expressions in Action
    2. Block Lambda Expressions
    3. Generic Functional Interfaces
    4. Try This 14-1: Pass a Lambda Expression as an Argument
    5. Lambda Expressions and Variable Capture
    6. Throw an Exception from Within a Lambda Expression
    7. Method References
      1. Method References to static Methods
      2. Method References to Instance Methods
    8. Constructor References
    9. Predefined Functional Interfaces
    10. Chapter 14: Self Test
  22. Chapter 15: Applets, Events, and Miscellaneous Topics
    1. Applet Basics
    2. Applet Organization and Essential Elements
    3. The Applet Architecture
    4. A Complete Applet Skeleton
    5. Applet Initialization and Termination
    6. Requesting Repainting
      1. The update( ) Method
    7. Try This 15-1: A Simple Banner Applet
    8. Using the Status Window
    9. Passing Parameters to Applets
    10. The Applet Class
    11. Event Handling
    12. The Delegation Event Model
    13. Events
      1. Event Sources
      2. Event Listeners
      3. Event Classes
      4. Event Listener Interfaces
    14. Using the Delegation Event Model
      1. Handling Mouse and Mouse Motion Events
      2. A Simple Mouse Event Applet
    15. More Java Keywords
      1. The transient and volatile Modifiers
      2. instanceof
      3. strictfp
      4. assert
      5. Native Methods
    16. Chapter 15: Self Test
  23. Chapter 16: Introducing Swing
    1. The Origins and Design Philosophy of Swing
    2. Components and Containers
      1. Components
      2. Containers
      3. The Top-Level Container Panes
    3. Layout Managers
    4. A First Simple Swing Program
      1. The First Swing Example Line by Line
    5. Use JButton
    6. Work with JTextField
    7. Create a JCheckBox
    8. Work with JList
    9. Try This 16-1: A Swing-Based File Comparison Utility
    10. Use Anonymous Inner Classes or Lambda Expressions to Handle Events
    11. Create a Swing Applet
    12. Chapter 16: Self Test
  24. Chapter 17: Introducing JavaFX
    1. JavaFX Basic Concepts
      1. The JavaFX Packages
      2. The Stage and Scene Classes
      3. Nodes and Scene Graphs
      4. Layouts
      5. The Application Class and the Life-cycle Methods
      6. Launching a JavaFX Application
    2. A JavaFX Application Skeleton
    3. Compiling and Running a JavaFX Program
    4. The Application Thread
    5. A Simple JavaFX Control: Label
    6. Using Buttons and Events
      1. Event Basics
      2. Introducing the Button Control
      3. Demonstrating Event Handling and the Button
    7. Three More JavaFX Controls
      1. CheckBox
    8. Try This 17-1: Use the CheckBox Indeterminate State
      1. ListView
      2. TextField
    9. Introducing Effects and Transforms
      1. Effects
      2. Transforms
      3. Demonstrating Effects and Transforms
    10. What Next?
    11. Chapter 17: Self Test
  25. Appendix A: Answers to Self Tests
    1. Chapter 1: Java Fundamentals
    2. Chapter 2: Introducing Data Types and Operators
    3. Chapter 3: Program Control Statements
    4. Chapter 4: Introducing Classes, Objects, and Methods
    5. Chapter 5: More Data Types and Operators
    6. Chapter 6: A Closer Look at Methods and Classes
    7. Chapter 7: Inheritance
    8. Chapter 8: Packages and Interfaces
    9. Chapter 9: Exception Handling
    10. Chapter 10: Using I/O
    11. Chapter 11: Multithreaded Programming
    12. Chapter 12: Enumerations, Autoboxing, Static Import, and Annotations
    13. Chapter 13: Generics
    14. Chapter 14: Lambda Expressions and Method References
    15. Chapter 15: Applets, Events, and Miscellaneous Topics
    16. Chapter 16: Introducing Swing
    17. Chapter 17: Introducing JavaFX
  26. Appendix B: Using Java’s Documentation Comments
    1. The javadoc Tags
      1. @author
      2. {@code}
      3. @deprecated
      4. {@docRoot}
      5. @exception
      6. {@inheritDoc}
      7. {@link}
      8. {@linkplain}
      9. {@literal}
      10. @param
      11. @return
      12. @see
      13. @serial
      14. @serialData
      15. @serialField
      16. @since
      17. @throws
      18. {@value}
      19. @version
    2. The General Form of a Documentation Comment
    3. What javadoc Outputs
    4. An Example That Uses Documentation Comments
  27. Index