Java in a Nutshell, 8th Edition

Book description

This updated edition of the Nutshell guide not only helps experienced Java programmers get the most out of versions through Java 17, it also serves as a learning path for new developers. Chock-full of examples that demonstrate how to take complete advantage of modern Java APIs and development best practices, this thoroughly revised book includes new material on recent enhancements to the Java object model that every developer should know about.

The first section provides a fast-paced, no-fluff introduction to the Java programming language and the core runtime aspects of the Java platform. The second section is a reference to core concepts and APIs that explains how to perform real programming work in the Java environment.

  • Get up to speed on language details through Java 17
  • Learn object-oriented programming using basic Java syntax
  • Explore generics, enumerations, annotations, and lambda expressions
  • Understand techniques used in object-oriented design
  • Examine how concurrency and memory are intertwined
  • Work with Java collections and handle common data formats
  • Delve into Java's latest I/O APIs including asynchronous channels
  • Become familiar with development tools in OpenJDK

Publisher resources

View/Submit Errata

Table of contents

  1. Foreword
  2. Preface
    1. Changes in the Eighth Edition
    2. Contents of This Book
    3. Related Books
    4. Conventions Used in This Book
    5. Using Code Examples
    6. O’Reilly Online Learning
    7. How to Contact Us
    8. Acknowledgments
  3. I. Introducing Java
  4. 1. Introduction to the Java Environment
    1. The Language, the JVM, and the Ecosystem
      1. What Is the Java Language?
      2. What Is the JVM?
      3. What Is the Java Ecosystem?
      4. The Lifecycle of a Java Program
      5. Frequently Asked Questions
    2. Comparing Java to Other Languages
      1. Java Compared to JavaScript
      2. Java Compared to Python
      3. Java Compared to C
      4. Java Compared to C++
    3. Answering Some Criticisms of Java
      1. Overly Verbose
      2. Slow to Change
      3. Performance Problems
      4. Insecure
      5. Too Corporate
    4. A Brief History of Java and the JVM
    5. Summary
  5. 2. Java Syntax from the Ground Up
    1. Java Programs from the Top Down
    2. Lexical Structure
      1. The Unicode Character Set
      2. Case Sensitivity and Whitespace
      3. Comments
      4. Reserved Words
      5. Identifiers
      6. Literals
      7. Punctuation
    3. Primitive Data Types
      1. The boolean Type
      2. The char Type
      3. Integer Types
      4. Floating-Point Types
      5. Primitive Type Conversions
    4. Expressions and Operators
      1. Operator Summary
      2. Arithmetic Operators
      3. String Concatenation Operator
      4. Increment and Decrement Operators
      5. Comparison Operators
      6. Boolean Operators
      7. Bitwise and Shift Operators
      8. Assignment Operators
      9. The Conditional Operator
      10. The instanceof Operator
      11. Special Operators
    5. Statements
      1. Expression Statements
      2. Compound Statements
      3. The Empty Statement
      4. Labeled Statements
      5. Local Variable Declaration Statements
      6. The if/else Statement
      7. The switch Statement
      8. The switch Expression
      9. The while Statement
      10. The do Statement
      11. The for Statement
      12. The foreach Statement
      13. The break Statement
      14. The continue Statement
      15. The return Statement
      16. The synchronized Statement
      17. The throw Statement
      18. The try/catch/finally Statement
      19. The try-with-resources Statement
      20. The assert Statement
    6. Methods
      1. Defining Methods
      2. Method Modifiers
      3. Checked and Unchecked Exceptions
      4. Variable-Length Argument Lists
    7. Introduction to Classes and Objects
      1. Defining a Class
      2. Creating an Object
      3. Using an Object
      4. Object Literals
      5. Lambda Expressions
    8. Arrays
      1. Array Types
      2. Creating and Initializing Arrays
      3. Using Arrays
      4. Multidimensional Arrays
    9. Reference Types
      1. Reference Versus Primitive Types
      2. Manipulating Objects and Reference Copies
      3. Comparing Objects
      4. Boxing and Unboxing Conversions
    10. Packages and the Java Namespace
      1. Package Declaration
      2. Globally Unique Package Names
      3. Importing Types
      4. Importing Static Members
    11. Java Source File Structure
    12. Defining and Running Java Programs
    13. Summary
  6. 3. Object-Oriented Programming in Java
    1. Overview of Classes and Records
      1. Basic OO Definitions
      2. Records
      3. Other Reference Types
      4. Class Definition Syntax
    2. Fields and Methods
      1. Field Declaration Syntax
      2. Class Fields
      3. Class Methods
      4. Instance Fields
      5. Instance Methods
      6. How the this Reference Works
    3. Creating and Initializing Objects
      1. Defining a Constructor
      2. Defining Multiple Constructors
      3. Invoking One Constructor from Another
      4. Field Defaults and Initializers
      5. Record Constructors
    4. Subclasses and Inheritance
      1. Extending a Class
      2. Superclasses, Object, and the Class Hierarchy
      3. Subclass Constructors
      4. Constructor Chaining and the Default Constructor
      5. Hiding Superclass Fields
      6. Overriding Superclass Methods
      7. Sealed Classes
    5. Data Hiding and Encapsulation
      1. Access Control
      2. Data Accessor Methods
    6. Abstract Classes and Methods
      1. Reference Type Conversions
    7. Modifier Summary
    8. Summary
  7. 4. The Java Type System
    1. Interfaces
      1. Defining an Interface
      2. Extending Interfaces
      3. Implementing an Interface
      4. Records and Interfaces
      5. Sealed Interfaces
      6. Default Methods
      7. Marker Interfaces
    2. Java Generics
      1. Introduction to Generics
      2. Generic Types and Type Parameters
      3. Diamond Syntax
      4. Type Erasure
      5. Bounded Type Parameters
      6. Introducing Covariance
      7. Wildcards
      8. Generic Methods
      9. Compile and Runtime Typing
      10. Using and Designing Generic Types
    3. Enums and Annotations
      1. Enums
      2. Annotations
      3. Defining Custom Annotations
      4. Type Annotations
    4. Lambda Expressions
      1. Lambda Expression Conversion
      2. Method References
      3. Functional Programming
      4. Lexical Scoping and Local Variables
    5. Nested Types
      1. Static Member Types
      2. Nonstatic Member Classes
      3. Local Classes
      4. Anonymous Classes
    6. Describing the Java Type System
      1. Nominal Typing
      2. Nondenotable Types and var
    7. Summary
  8. 5. Introduction to Object-Oriented Design in Java
    1. Java Values
    2. Important Common Methods
      1. toString()
      2. equals()
      3. hashCode()
      4. Comparable::compareTo()
      5. clone()
    3. Constants
    4. Working with Fields
    5. Field Inheritance and Accessors
    6. Singleton
    7. Factory Methods
    8. Builders
    9. Interfaces Versus Abstract Classes
    10. Do Default Methods Change Java’s Inheritance Model?
    11. OOD Using Lambdas
      1. Lambdas Versus Nested Classes
      2. Lambdas Versus Method References
    12. OOD Using Sealed Types
    13. OOD Using Records
    14. Instance Methods or Class Methods?
      1. A word about System.out.println()
    15. Composition Versus Inheritance
    16. Exceptions and Exception Handling
    17. Safe Java Programming
  9. 6. Java’s Approach to Memory and Concurrency
    1. Basic Concepts of Java Memory Management
      1. Memory Leaks in Java
      2. Introducing Mark-and-Sweep
    2. How the JVM Optimizes Garbage Collection
      1. Evacuation
      2. Compaction
    3. The HotSpot Heap
      1. G1
      2. ParallelOld
      3. Serial
      4. Shenandoah
      5. ZGC
    4. Finalization
      1. Finalization Details
    5. Java’s Support for Concurrency
      1. Thread Lifecycle
      2. Visibility and Mutability
      3. Exclusion and Protecting State
      4. volatile
      5. Useful Methods of Thread
      6. Deprecated Methods of Thread
    6. Working with Threads
    7. Summary
  10. II. Working with the Java Platform
  11. 7. Programming and Documentation Conventions
    1. Naming and Capitalization Conventions
    2. Practical Naming
    3. Java Documentation Comments
      1. Structure of a Doc Comment
      2. Doc-Comment Tags
      3. Inline Doc-Comment Tags
      4. Cross-References in Doc Comments
      5. Doc Comments for Packages
    4. Doclets
    5. Conventions for Portable Programs
    6. Summary
  12. 8. Working with Java Collections
    1. Introduction to Collections API
      1. The Collection Interface
      2. The Set Interface
      3. The List Interface
      4. The Map Interface
      5. The Queue and BlockingQueue Interfaces
      6. Adding Elements to Queues
      7. Removing Elements from Queues
      8. Utility Methods
      9. Arrays and Helper Methods
    2. Java Streams and Lambda Expressions
      1. Functional Approaches
      2. The Streams API
    3. Summary
  13. 9. Handling Common Data Formats
    1. Text
      1. Special Syntax for Strings
      2. String Immutability
      3. String Formatting
      4. Regular Expressions
    2. Numbers and Math
      1. How Java Represents Integer Types
      2. Java and Floating-Point Numbers
      3. Java’s Standard Library of Mathematical Functions
    3. Date and Time
      1. Introducing the Java 8 Date and Time API
      2. Queries
      3. Adjusters
      4. Timezones
      5. Legacy Date and Time
    4. Summary
  14. 10. File Handling and I/O
    1. Classic Java I/O
      1. Files
      2. I/O Streams
      3. Readers and Writers
      4. try-with-resources Revisited
      5. Problems with Classic I/O
    2. Modern Java I/O
      1. Files
      2. Path
    3. NIO Channels and Buffers
      1. ByteBuffer
      2. Mapped Byte Buffers
    4. Async I/O
      1. Future-Based Style
      2. Callback-Based Style
      3. Watch Services and Directory Searching
    5. Networking
      1. HTTP
      2. TCP
      3. IP
    6. Summary
  15. 11. Classloading, Reflection, and Method Handles
    1. Class Files, Class Objects, and Metadata
      1. Examples of Class Objects
      2. Class Objects and Metadata
    2. Phases of Classloading
      1. Loading
      2. Verification
      3. Preparation and Resolution
      4. Initialization
    3. Secure Programming and Classloading
    4. Applied Classloading
      1. Classloader Hierarchy
    5. Reflection
      1. When to Use Reflection
      2. How to Use Reflection
      3. Dynamic Proxies
    6. Method Handles
      1. MethodType
      2. Method Lookup
      3. Invoking Method Handles
  16. 12. Java Platform Modules
    1. Why Modules?
      1. Modularizing the JDK
    2. Writing Your Own Modules
      1. Basic Modules Syntax
      2. Building a Simple Modular Application
      3. The Module Path
      4. Automatic Modules
      5. Open Modules
      6. Providing Services
      7. Multi-Release JARs
      8. Converting to a Multi-Release JAR
      9. Migrating to Modules
      10. Custom Runtime Images
    3. Issues with Modules
      1. Unsafe and Related Problems
      2. Lack of Versioning
      3. Slow Adoption Rates
    4. Summary
  17. 13. Platform Tools
    1. Command-Line Tools
    2. Introduction to JShell
    3. Introduction to Java Flight Recorder (JFR)
    4. Summary
  18. Appendix. Beyond Java 17
    1. Long-Term JDK Projects
      1. Amber
    2. Java 18
      1. Panama
    3. Java 19
      1. Loom
    4. Future Java
      1. Valhalla
      2. Cloud-Native Java
  19. Index
  20. About the Authors

Product information

  • Title: Java in a Nutshell, 8th Edition
  • Author(s): Benjamin J Evans, Jason Clark, David Flanagan
  • Release date: February 2023
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098131005