Java: A Beginner's Guide, Ninth Edition, 9th Edition

Book description

A practical introduction to Java programming—fully revised for the latest version, Java SE 17

Thoroughly updated for Java Platform Standard Edition 17, this hands-on resource shows, step by step, how to get started programming in Java from the very first chapter. Written by Java guru Herbert Schildt, the book starts with the basics, such as how to create, compile, and run a Java program. From there, you will learn essential Java keywords, syntax, and commands.

Java: A Beginner's Guide, Ninth Edition covers the basics and touches on advanced features, including multithreaded programming, generics, Lambda expressions, and Swing. Enumeration, modules, and interface methods are also clearly explained. This guide delivers the appropriate mix of theory and practical coding necessary to get you up and running developing Java applications in no time!
  • Clearly explains all of the new Java SE 17 features
  • Features self-tests, exercises, and downloadable code samples
  • Written by bestselling author and leading Java authority Herbert Schildt

Table of contents

  1. Cover
  2. Title Page
  3. Copyright Page
  4. Contents
  5. INTRODUCTION
  6. 1 Java Fundamentals
    1. The History and Philosophy of Java
      1. The Origins of Java
      2. Java’s Lineage: C and C++
      3. How Java Impacted the Internet
      4. Java’s Magic: The Bytecode
      5. Moving Beyond Applets
      6. A Faster Release Schedule
      7. The Java Buzzwords
    2. Object-Oriented Programming
      1. Encapsulation
      2. Polymorphism
      3. Inheritance
    3. The Java Development Kit
    4. A First Simple Program
      1. Entering the Program
      2. Compiling the Program
      3. The First Sample Program Line by Line
    5. Handling Syntax Errors
    6. A Second Simple Program
    7. Another Data Type
    8. Try This 1-1: Converting Gallons to Liters
    9. Two Control Statements
      1. The if Statement
      2. The for Loop
    10. Create Blocks of Code
    11. Semicolons and Positioning
    12. Indentation Practices
    13. Try This 1-2: Improving the Gallons-to-Liters Converter
    14. The Java Keywords
    15. Identifiers in Java
    16. The Java Class Libraries
    17. 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
      3. 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 Traditional 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
      2. Irregular Arrays
      3. Arrays of Three or More Dimensions
      4. Initializing Multidimensional Arrays
    4. Alternative Array Declaration Syntax
    5. Assigning Array References
    6. Using the length Member
    7. Try This 5-2: A Queue Class
    8. The For-Each Style for Loop
      1. Iterating Over Multidimensional Arrays
      2. Applying the Enhanced for
    9. Strings
      1. Constructing Strings
      2. Operating on Strings
      3. Arrays of Strings
      4. Strings Are Immutable
    10. Using a String to Control a switch Statement
    11. Using Command-Line Arguments
      1. Using Type Inference with Local Variables
      2. Local Variable Type Inference with Reference Types
      3. Using Local Variable Type Inference in a for Loop
      4. Some var Restrictions
    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, Annotations, and More
    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. Introducing instanceof
    15. 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. Local Variable Type Inference and Generics
    13. Erasure
    14. Ambiguity Errors
    15. Some Generic Restrictions
      1. Type Parameters Can’t Be Instantiated
      2. Restrictions on Static Members
      3. Generic Array Restrictions
      4. Generic Exception Restriction
      5. Continuing Your Study of Generics
      6. 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 Switch Expressions, Records, and Other Recently Added Features
    1. Enhancements to switch
      1. Use a List of case Constants
      2. Introducing the switch Expression and the yield Statement
      3. Introducing the Arrow in a case Statement
      4. A Closer Look at the Arrow case
    2. Try This 16-1: Use a switch Expression to Obtain a City’s Time Zone
    3. Records
      1. Record Basics
      2. Create Record Constructors
      3. A Closer Look at Record Getter Methods
    4. Pattern Matching with instanceof
    5. Sealed Classes and Interfaces
      1. Sealed Classes
      2. Sealed Interfaces
    6. Future Directions
    7. Chapter 16 Self Test
  22. 17 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 17-1: A Swing-Based File Comparison Utility
    11. Use Anonymous Inner Classes or Lambda Expressions to Handle Events
      1. 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, Annotations, and More
    13. Chapter 13: Generics
    14. Chapter 14: Lambda Expressions and Method References
    15. Chapter 15: Modules
    16. Chapter 16: Switch Expressions, Records, and Other Recently Added Features
    17. Chapter 17: Introducing Swing
  24. B Using Java’s Documentation Comments
    1. The javadoc Tags
      1. @author
      2. {@code}
      3. @deprecated
      4. {@docRoot}
      5. @exception
      6. @hidden
      7. {@index}
      8. {@inheritDoc}
      9. {@link}
      10. {@linkplain}
      11. {@literal}
      12. @param
      13. @provides
      14. @return
      15. @see
      16. @since
      17. {@summary}
      18. @throws
      19. @uses
      20. {@value}
      21. @version
    2. The General Form of a Documentation Comment
    3. What javadoc Outputs
    4. An Example That Uses Documentation Comments
  25. C Compile and Run Simple Single-File Programs in One Step
  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. strictfp
    3. assert
    4. Native Methods
    5. Another Form of this
  28. Index

Product information

  • Title: Java: A Beginner's Guide, Ninth Edition, 9th Edition
  • Author(s): Herbert Schildt
  • Release date: January 2022
  • Publisher(s): McGraw-Hill
  • ISBN: 9781260463569