Java: A Beginner's Guide, 7th Edition

Book description

Up-to-Date, Essential Java Programming Skills—Made Easy!

Fully updated for Java Platform, Standard Edition 9 (Java SE 9), Java: A Beginner’s Guide, Seventh 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. The book also covers some of Java’s more advanced features, including multithreaded programming, generics, lambda expressions, Swing, and JavaFX. This practical Oracle Press guide features details on Java SE 9’s innovative new module system, and, as an added bonus, it includes an introduction to JShell, Java’s new interactive programming tool.

Designed for Easy Learning:

• Key Skills and 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

Companion material for this title can be found here.

Table of contents

  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. Contents
  5. INTRODUCTION
  6. 1 Java Fundamentals
    1. The Origins of Java
    2. Java’s Lineage: C and C++
    3. How Java Impacted the Internet
      1. Java Simplified Web-Based Programming
      2. Java Applets
      3. Security
      4. Portability
    4. Java’s Magic: The Bytecode
    5. Moving Beyond Applets
    6. The Java Buzzwords
    7. Object-Oriented Programming
      1. Encapsulation
      2. Polymorphism
      3. Inheritance
    8. Obtaining the Java Development Kit
    9. A First Simple Program
      1. Entering the Program
      2. Compiling the Program
      3. The First Sample Program Line by Line
    10. Handling Syntax Errors
    11. A Second Simple Program
    12. Another Data Type
    13. Try This 1-1: Converting Gallons to Liters
    14. Two Control Statements
      1. The if Statement
      2. The for Loop
    15. Create Blocks of Code
    16. Semicolons and Positioning
    17. Indentation Practices
    18. Try This 1-2: Improving the Gallons-to-Liters Converter
    19. The Java Keywords
    20. Identifiers in Java
    21. The Java Class Libraries
    22. Chapter 1 Self Test
  7. 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
  8. 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
  9. 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 this Keyword
    15. Chapter 4 Self Test
  10. 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
  11. 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
  12. 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
  13. 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. Private Interface Methods
    15. Final Thoughts on Packages and Interfaces
    16. Chapter 8 Self Test
  14. 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 Additional 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
  15. 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
  16. 11 Multithreaded Programming
    1. Multithreading Fundamentals
    2. The Thread Class and Runnable Interface
    3. Creating a Thread
      1. One Improvement and Two Simple Variations
    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
  17. 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
  18. 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
  19. 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
  20. 15 Modules
    1. Module Basics
      1. A Simple Module Example
      2. Compile and Run the First Module Example
      3. A Closer Look at requires and exports
    2. java.base and the Platform Modules
    3. Legacy Code and the Unnamed Module
    4. Exporting to a Specific Module
    5. Using requires transitive
    6. Try This 15-1: Experiment with requires transitive
    7. Use Services
      1. Service and Service Provider Basics
      2. The Service-Based Keywords
      3. A Module-Based Service Example
    8. Additional Module Features
      1. Open Modules
      2. The opens Statement
      3. requires static
    9. Continuing Your Study of Modules
    10. Chapter 15 Self Test
  21. 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. Swing Event Handling
      1. Events
      2. Event Sources
      3. Event Listeners
      4. Event Classes and Listener Interfaces
    6. Use JButton
    7. Work with JTextField
    8. Create a JCheckBox
    9. Work with JList
    10. Try This 16-1: A Swing-Based File Comparison Utility
    11. Use Anonymous Inner Classes or Lambda Expressions to Handle Events
    12. Chapter 16 Self Test
  22. 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
  23. 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: Modules
    16. Chapter 16: Introducing Swing
    17. Chapter 17: Introducing JavaFX
  24. B Using Java’s Documentation Comments
    1. The javadoc Tags
      1. @author
      2. {@code}
      3. @deprecated
      4. {@docRoot}
      5. @exception
      6. {@index}
      7. {@inheritDoc}
      8. {@link}
      9. {@linkplain}
      10. {@literal}
      11. @param
      12. @return
      13. @see
      14. @serial
      15. @serialData
      16. @serialField
      17. @since
      18. @throws
      19. {@value}
      20. @version
    2. The General Form of a Documentation Comment
    3. What javadoc Outputs
    4. An Example That Uses Documentation Comments
  25. C An Overview of Java Web Start
    1. What Is Java Web Start?
    2. Four Key Java Web Start Essentials
      1. Java Web Start Apps Require a JAR File
      2. Java Web Start Apps Are Signed
      3. Java Web Start Relies on JNLP
      4. Linking to the Java Web Start JNLP File
    3. Experimenting with Java Web Start Using the Local File System
      1. Create a JAR File for ButtonDemo
      2. Create a Keystore and Sign ButtonDemo.jar
      3. Create a JNLP file for ButtonDemo
      4. Create a Short HTML File Called StartBD.html
      5. Add ButtonDemo.jnlp to the Exception Site List in the Java Control Panel
      6. Execute ButtonDemo via Your Browser
    4. Running a Java Web Start Application Using javaws
    5. Using Java Web Start with an Applet
  26. D Introducing JShell
    1. JShell Basics
    2. List, Edit, and Rerun Code
    3. Add a Method
    4. Create a Class
    5. Use an Interface
    6. Evaluate Expressions and Use Built-in Variables
    7. Importing Packages
    8. Exceptions
    9. Some More JShell Commands
    10. Exploring JShell Further
  27. E More Java Keywords
    1. The transient and volatile Modifiers
    2. instanceof
    3. strictfp
    4. assert
    5. Native Methods
    6. Another Form of this
  28. Index

Product information

  • Title: Java: A Beginner's Guide, 7th Edition
  • Author(s): Herbert Schildt
  • Release date: October 2017
  • Publisher(s): McGraw-Hill
  • ISBN: 9781259589324