Learning Java, 5th Edition

Book description

If you're new to Java—or new to programming—this best-selling book will guide you through the language features and APIs of Java 11. With fun, compelling, and realistic examples, authors Marc Loy, Patrick Niemeyer, and Daniel Leuck introduce you to Java fundamentals—including its class libraries, programming techniques, and idioms—with an eye toward building real applications.

You'll learn powerful new ways to manage resources and exceptions in your applications—along with core language features included in recent Java versions.

  • Develop with Java, using the compiler, interpreter, and other tools
  • Explore Java's built-in thread facilities and concurrency package
  • Learn text processing and the powerful regular expressions API
  • Write advanced networked or web-based applications and services

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 11, 12, 13, 14)
    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
      3. Security Managers
    6. Application and User-Level Security
    7. A Java Road Map
      1. The Past: Java 1.0–Java 11
      2. The Present: Java 14
      3. The Future
      4. Availability
  3. 2. A First Application
    1. Java Tools and Environment
      1. Installing the JDK
      2. Installing OpenJDK on Linux
      3. Installing OpenJDK on macOS
      4. Installing OpenJDK on Windows
      5. Configuring IntelliJ IDEA and Creating a Project
      6. Running the Project
      7. Grabbing the Learning Java 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. Package 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
  4. 3. Tools of the Trade
    1. JDK Environment
    2. The Java VM
    3. Running Java Applications
      1. System Properties
    4. The Classpath
      1. javap
      2. Modules
    5. The Java Compiler
    6. Trying Java
    7. JAR Files
      1. File Compression
      2. The jar Utility
      3. The pack200 Utility
    8. Building Up
  5. 4. The Java Language
    1. Text Encoding
    2. Comments
      1. Javadoc Comments
    3. Variables and Constants
    4. Types
      1. Primitive Types
      2. Reference Types
      3. Inferring Types
      4. Passing References
      5. 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!
  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. Interfaces
      3. Inner Classes
      4. Anonymous Inner Classes
    7. Organizing Content and Planning for Failure
  7. 6. Error Handling and Logging
    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. The Logging API
      1. Overview
      2. Logging Levels
      3. A Simple Example
      4. Logging Setup Properties
      5. The Logger
      6. Performance
    4. Real-World Exceptions
  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. Conclusion
  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. Parsing Errors
      6. Timestamps
    6. Other Useful Utilities
  10. 9. Threads
    1. Introducing Threads
      1. The Thread Class and the Runnable Interface
      2. Controlling Threads
      3. Death of a Thread
    2. Synchronization
      1. Serializing Access to Methods
      2. Accessing Class and Instance Variables from Multiple Threads
    3. Scheduling and Priority
      1. Thread State
      2. Time-Slicing
      3. Priorities
      4. Yielding
    4. Thread Performance
      1. The Cost of Synchronization
      2. Thread Resource Consumption
    5. Concurrency Utilities
  11. 10. 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. Modals and Pop Ups
      1. Message Dialogs
      2. Confirmation Dialogs
      3. Input Dialogs
    5. Threading Considerations
      1. SwingUtilities and Component Updates
      2. Timers
    6. Next Steps
      1. Menus
      2. Preferences
      3. Custom Components and Java2D
      4. JavaFX
    7. User Interface and User Experience
  12. 11. Networking and I/O
    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 NIO 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
    4. Network Programming
    5. Sockets
      1. Clients and Servers
      2. The DateAtHost Client
      3. A Distributed Game
    6. More to Explore
  13. 12. Programming for the Web
    1. Uniform Resource Locators
    2. The URL Class
      1. Stream Data
      2. Getting the Content as an Object
      3. Managing Connections
      4. Handlers in Practice
      5. Useful Handler Frameworks
    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. Java Web Applications
      1. The Servlet Life Cycle
      2. Servlets
      3. The HelloClient Servlet
      4. The Servlet Response
      5. Servlet Parameters
      6. The ShowParameters Servlet
      7. User Session Management
      8. The ShowSession Servlet
    5. Servlet Containers
      1. Configuration with web.xml and Annotations
      2. URL Pattern Mappings
      3. Deploying HelloClient
    6. The World Wide Web Is, Well, Wide
  14. 13. Expanding Java
    1. Java Releases
      1. JCP and JSRs
    2. Lambda Expressions
      1. Retrofitting Your Code
    3. Expanding Java Beyond the Core
    4. Final Wrap-Up and Next Steps
  15. A. Code Examples and IntelliJ IDEA
    1. Grabbing the Main Code Examples
    2. Installing IntelliJ IDEA
      1. Installing on Linux
      2. Installing on a macOS
      3. Installing on Windows
    3. Importing the Examples
    4. Running the Examples
    5. Grabbing the Web Code Examples
    6. Working with Servlets
  16. Glossary
  17. Index

Product information

  • Title: Learning Java, 5th Edition
  • Author(s): Marc Loy, Patrick Niemeyer, Daniel Leuck
  • Release date: March 2020
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492056270