Learning Java, 6th Edition

Book description

Ideal for working programmers new to Java, this best-selling book guides you through the language features and APIs of Java 21. Through fun, compelling, and realistic examples, authors Marc Loy, Patrick Niemeyer, and Dan Leuck introduce you to Java's fundamentals, including its class libraries, programming techniques, and idioms, with an eye toward building real applications.

This updated sixth edition expands the content to continue covering lambdas and streams, and shows you how to use a functional paradigm in Java. You'll learn about the latest Java features introduced since the book's fifth edition, from JDK 15 through 21. You'll also take a deep dive into virtual threads (introduced as Project Loom in Java 19).

This guide helps you:

  • Learn the structure of the Java language and Java applications
  • Write, compile, and execute Java applications
  • Understand the basics of Java threading and concurrent programming
  • Learn Java I/O basics, including local files and network resources
  • Create compelling interfaces with an eye toward usability
  • Learn how functional features have been integrated in Java
  • Keep up with Java developments as new versions are released

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Who Should Read This Book
    2. New Developments
      1. New in This Edition (Java 15, 16, 17, 18, 19, 20, 21)
    3. Using This Book
    4. Online Resources
    5. Conventions Used in This Book
    6. Using Code Examples
    7. O’Reilly Online Learning
    8. How to Contact Us
    9. Acknowledgments
  2. 1. A Modern Language
    1. Enter Java
      1. Java’s Origins
      2. Growing Up
    2. A Virtual Machine
    3. Java Compared with Other Languages
    4. Safety of Design
      1. Simplify, Simplify, Simplify…​
      2. Type Safety and Method Binding
      3. Incremental Development
      4. Dynamic Memory Management
      5. Error Handling
      6. Threads
      7. Scalability
    5. Safety of Implementation
      1. The Verifier
      2. Class Loaders
    6. Application and User-Level Security
    7. A Java Road Map
      1. The Past: Java 1.0–Java 20
      2. The Present: Java 21
      3. The Future
    8. Exercises
  3. 2. A First Application
    1. Java Tools and Environment
      1. Installing the JDK
      2. Installing IntelliJ IDEA and Creating a Project
      3. Running the Project
      4. Grabbing the Examples
    2. HelloJava
      1. Classes
      2. The main() Method
      3. Classes and Objects
      4. Variables and Class Types
      5. HelloComponent
      6. Inheritance
      7. The JComponent Class
      8. Relationships and Finger-Pointing
      9. Packages and Imports
      10. The paintComponent() Method
    3. HelloJava2: The Sequel
      1. Instance Variables
      2. Constructors
      3. Events
      4. The repaint() Method
      5. Interfaces
    4. Goodbye and Hello Again
      1. Review Questions
      2. Code Exercises
  4. 3. Tools of the Trade
    1. JDK Environment
    2. The Java VM
    3. Running Java Applications
      1. System Properties
    4. The Classpath
      1. CLASSPATH on Unix and macOS
      2. CLASSPATH on Windows
      3. CLASSPATH Wildcards
      4. Modules
    5. The Java Compiler
    6. Trying Java
    7. JAR Files
      1. The jar Utility
    8. Tool Wrap-Up
      1. Review Questions
      2. Code Exercises
      3. Advanced Code Exercises
  5. 4. The Java Language
    1. Text Encoding
    2. Comments
      1. Javadoc Comments
      2. Annotations
    3. Variables and Constants
    4. Types
      1. Primitive Types
      2. Reference Types
      3. A Word About Strings
    5. Statements and Expressions
      1. Statements
      2. Expressions
    6. Arrays
      1. Array Types
      2. Array Creation and Initialization
      3. Using Arrays
      4. Anonymous Arrays
      5. Multidimensional Arrays
    7. Types and Classes and Arrays, Oh My!
      1. Review Questions
      2. Code Exercises
      3. Advanced Exercises
  6. 5. Objects in Java
    1. Classes
      1. Declaring and Instantiating Classes
      2. Accessing Fields and Methods
      3. Static Members
    2. Methods
      1. Local Variables
      2. Shadowing
      3. Static Methods
      4. Initializing Local Variables
      5. Argument Passing and References
      6. Wrappers for Primitive Types
      7. Method Overloading
    3. Object Creation
      1. Constructors
      2. Working with Overloaded Constructors
    4. Object Destruction
      1. Garbage Collection
    5. Packages
      1. Importing Classes
      2. Custom Packages
      3. Member Visibility and Access
      4. Compiling with Packages
    6. Advanced Class Design
      1. Subclassing and Inheritance
      2. Abstract Classes and Methods
      3. Interfaces
      4. Inner Classes
      5. Anonymous Inner Classes
    7. Organizing Content and Planning for Failure
      1. Review Questions
      2. Code Exercises
      3. Advanced Exercises
  7. 6. Error Handling
    1. Exceptions
      1. Exceptions and Error Classes
      2. Exception Handling
      3. Bubbling Up
      4. Stack Traces
      5. Checked and Unchecked Exceptions
      6. Throwing Exceptions
      7. try Creep
      8. The finally Clause
      9. try with Resources
      10. Performance Issues
    2. Assertions
      1. Enabling and Disabling Assertions
      2. Using Assertions
    3. Real-World Exceptions
      1. Review Questions
      2. Code Exercises
      3. Advanced Exercises
  8. 7. Collections and Generics
    1. Collections
      1. The Collection Interface
      2. Collection Types
      3. The Map Interface
    2. Type Limitations
      1. Containers: Building a Better Mousetrap
      2. Can Containers Be Fixed?
    3. Enter Generics
      1. Talking About Types
    4. “There Is No Spoon”
      1. Erasure
      2. Raw Types
    5. Parameterized Type Relationships
      1. Why Isn’t a List<Date> a List<Object>?
    6. Casts
      1. Converting Between Collections and Arrays
      2. Iterator
    7. A Closer Look: The sort() Method
    8. Application: Trees on the Field
    9. Useful Features
      1. Review Questions
      2. Code Exercises
      3. Advanced Exercises
  9. 8. Text and Core Utilities
    1. Strings
      1. Constructing Strings
      2. Strings from Things
      3. Comparing Strings
      4. Searching
      5. String Method Summary
    2. Things from Strings
      1. Parsing Primitive Numbers
      2. Tokenizing Text
    3. Regular Expressions
      1. Regex Notation
      2. The java.util.regex API
    4. Math Utilities
      1. The java.lang.Math Class
      2. Big/Precise Numbers
    5. Dates and Times
      1. Local Dates and Times
      2. Comparing and Manipulating Dates and Times
      3. Time Zones
      4. Parsing and Formatting Dates and Times
      5. Formatting Dates and Times
      6. Timestamps
    6. Other Useful Utilities
      1. Review Questions
      2. Code Exercises
  10. 9. Threads
    1. Introducing Threads
      1. The Thread Class and the Runnable Interface
      2. Controlling Threads
      3. Revisiting Animation with Threads
    2. Death of a Thread
    3. Virtual Threads
      1. Preview Feature Tangent
      2. A Quick Comparison
    4. Synchronization
      1. Serializing Access to Methods
      2. Accessing Class and Instance Variables from Multiple Threads
    5. Scheduling and Priority
      1. Thread State
      2. Time-Slicing
      3. Priorities
    6. Thread Performance
      1. The Cost of Synchronization
      2. Thread Resource Consumption
      3. Virtual Thread Performance
    7. Concurrency Utilities
      1. Upgrading Our Queue Demo
      2. Structured Concurrency
    8. So Many Threads to Pull
      1. Review Questions
      2. Code Exercises
  11. 10. File Input and Output
    1. Streams
      1. Basic I/O
      2. Character Streams
      3. Stream Wrappers
      4. The java.io.File Class
      5. File Streams
      6. RandomAccessFile
    2. The New I/O File API
      1. FileSystem and Path
      2. NIO File Operations
    3. The NIO Package
      1. Asynchronous I/O
      2. Performance
      3. Mapped and Locked Files
      4. Channels
      5. Buffers
      6. Character Encoders and Decoders
      7. FileChannel
      8. FileChannel Example
    4. wrap() Up
      1. Review Questions
      2. Code Exercises
      3. Advanced Exercises
  12. 11. Functional Approaches in Java
    1. Functions 101
    2. Lambdas
      1. Lambda Expressions
      2. Functional Interfaces
      3. Method References
      4. Practical Lambdas: Sorting
    3. Streams
      1. Sources and Operations
      2. Filtering Streams
      3. Mapping Streams
      4. Flatmaps
      5. Reducing and Collecting
    4. Using Lambdas Directly
    5. Next Steps
      1. Review Questions
      2. Code Exercises
      3. Advanced Exercises
  13. 12. Desktop Applications
    1. Buttons and Sliders and Text Fields, Oh My!
      1. Component Hierarchies
      2. Model View Controller Architecture
      3. Labels and Buttons
      4. Text Components
      5. Other Components
    2. Containers and Layouts
      1. Frames and Windows
      2. JPanel
      3. Layout Managers
    3. Events
      1. Mouse Events
      2. Action Events
      3. Change Events
      4. Other Events
    4. Threading Considerations
      1. SwingUtilities and Component Updates
      2. Timers
    5. But Wait, There’s More
      1. Menus
      2. Modals and Pop-Ups
    6. User Interface and User Experience
      1. Review Questions
      2. Code Exercises
      3. Advanced Exercises
  14. 13. Network Programming in Java
    1. Uniform Resource Locators
    2. The URL Class
      1. Stream Data
      2. Getting the Content as an Object
      3. Managing Connections
    3. Talking to Web Applications
      1. Using the GET Method
      2. Using the POST Method
      3. The HttpURLConnection
      4. SSL and Secure Web Communications
    4. Network Programming
    5. Sockets
      1. Clients and Servers
      2. The DateAtHost Client
      3. A Distributed Game
    6. Much More to Explore
      1. Review Questions
      2. Code Exercises
      3. Advanced Exercises
  15. A. Code Examples and IntelliJ IDEA
    1. Grabbing the Main Code Examples
    2. Importing the Examples
  16. B. Exercise Answers
    1. Chapter 1: A Modern Language
    2. Chapter 2: A First Application
      1. Code Exercises
    3. Chapter 3: Tools of the Trade
      1. Code Exercises
    4. Chapter 4: The Java Language
      1. Code Exercises
      2. Advanced Exercises
    5. Chapter 5: Objects in Java
      1. Code Exercises
      2. Advanced Exercises
    6. Chapter 6: Error Handling
      1. Code Exercises
      2. Advanced Exercises
    7. Chapter 7: Collections and Generics
      1. Code Exercises
      2. Advanced Exercises
    8. Chapter 8: Text and Core Utilities
      1. Code Exercises
    9. Chapter 9: Threads
      1. Code Exercises
    10. Chapter 10: File Input and Output
      1. Code Exercises
      2. Advanced Exercises
    11. Chapter 11: Functional Approaches in Java
      1. Code Exercises
      2. Advanced Exercises
    12. Chapter 12: Desktop Applications
      1. Code Exercises
      2. Advanced Exercises
    13. Chapter 13: Network Programming in Java
      1. Code Exercises
      2. Advanced Exercises
  17. Glossary
  18. Index
  19. About the Authors

Product information

  • Title: Learning Java, 6th Edition
  • Author(s): Marc Loy, Patrick Niemeyer, Daniel Leuck
  • Release date: August 2023
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098145538