Java: The Complete Reference, Thirteenth Edition, 13th Edition

Java: The Complete Reference, Thirteenth Edition, 13th Edition

Read it now on the O’Reilly learning platform with a 10-day free trial.

O’Reilly members get unlimited access to books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.

Book description

The definitive guide to Java programming—thoroughly revised for Java SE 21

Fully updated for the current version of Java, Java SE 21, this comprehensive guide shows, step by step, how to design, write, debug, run, and administer high-performance Java programs. Inside, bestselling author Herbert Schildt and programming expert Dr. Danny Coward cover the entire Java language, including its syntax, keywords, and libraries. The book lays out cutting-edge development techniques and best practices.

Java: The Complete Reference, Thirteenth Edition features clear explanations, detailed code samples, and real-world examples that demonstrate how Java can be put to work in the real world. JavaBeans, servlets, applets, Swing, lambda expressions, multithreading, and the default interface method are thoroughly discussed. You will get full details on all of the latest features and changes in JDK 18 through long-term support (LTS) release JDK 21.

Show and hide more Table of contents Product information

Table of contents

  1. Cover
  2. Title Page
  3. Copyright Page
  4. Contents at a Glance
  5. Contents
  6. Foreword
  7. Preface
  8. Part I The Java Language
    1. Chapter 1 The History and Evolution of Java
      1. Java’s Lineage
        1. The Birth of Modern Programming: C
        2. C++: The Next Step
        3. The Stage Is Set for Java
        1. The C# Connection
        2. Longevity
        1. Java Applets
        2. Security
        3. Portability
        1. Simple
        2. Object-Oriented
        3. Robust
        4. Multithreaded
        5. Architecture-Neutral
        6. Interpreted and High Performance
        7. Distributed
        8. Dynamic
        1. Object-Oriented Programming
          1. Two Paradigms
          2. Abstraction
          3. The Three OOP Principles
          1. Entering the Program
          2. Compiling the Program
          3. A Closer Look at the First Sample Program
          1. The if Statement
          2. The for Loop
          1. Whitespace
          2. Identifiers
          3. Literals
          4. Comments
          5. Separators
          6. The Java Keywords
          1. Java Is a Strongly Typed Language
          2. The Primitive Types
          3. Integers
            1. byte
            2. short
            3. int
            4. long
            1. float
            2. double
            1. Integer Literals
            2. Floating-Point Literals
            3. Boolean Literals
            4. Character Literals
            5. String Literals
            1. Declaring a Variable
            2. Dynamic Initialization
            3. The Scope and Lifetime of Variables
            1. Java’s Automatic Conversions
            2. Casting Incompatible Types
            1. The Type Promotion Rules
            1. One-Dimensional Arrays
            2. Multidimensional Arrays
            3. Alternative Array Declaration Syntax
            1. Some var Restrictions
            1. Arithmetic Operators
              1. The Basic Arithmetic Operators
              2. The Modulus Operator
              3. Arithmetic Compound Assignment Operators
              4. Increment and Decrement
              1. The Bitwise Logical Operators
              2. The Left Shift
              3. The Right Shift
              4. The Unsigned Right Shift
              5. Bitwise Operator Compound Assignments
              1. Short-Circuit Logical Operators
              1. Java’s Selection Statements
                1. if
                2. The Traditional switch
                1. while
                2. do-while
                3. for
                4. The For-Each Version of the for Loop
                5. Local Variable Type Inference in a for Loop
                6. Nested Loops
                1. Using break
                2. Using continue
                3. return
                1. Class Fundamentals
                  1. The General Form of a Class
                  2. A Simple Class
                  1. A Closer Look at new
                  1. Adding a Method to the Box Class
                  2. Returning a Value
                  3. Adding a Method That Takes Parameters
                  1. Parameterized Constructors
                  1. Instance Variable Hiding
                  1. Overloading Methods
                    1. Overloading Constructors
                    1. Overloading Vararg Methods
                    2. Varargs and Ambiguity
                    1. Inheritance Basics
                      1. Member Access and Inheritance
                      2. A More Practical Example
                      3. A Superclass Variable Can Reference a Subclass Object
                      1. Using super to Call Superclass Constructors
                      2. A Second Use for super
                      1. Why Overridden Methods?
                      2. Applying Method Overriding
                      1. Using final to Prevent Overriding
                      2. Using final to Prevent Inheritance
                      1. Packages
                        1. Defining a Package
                        2. Finding Packages and CLASSPATH
                        3. A Short Package Example
                        1. An Access Example
                        1. Defining an Interface
                        2. Implementing Interfaces
                        3. Nested Interfaces
                        4. Applying Interfaces
                        5. Variables in Interfaces
                        6. Interfaces Can Be Extended
                        1. Default Method Fundamentals
                        2. A More Practical Example
                        3. Multiple Inheritance Issues
                        1. Exception-Handling Fundamentals
                        2. Exception Types
                        3. Uncaught Exceptions
                        4. Using try and catch
                          1. Displaying a Description of an Exception
                          1. The Java Thread Model
                            1. Thread Priorities
                            2. Synchronization
                            3. Messaging
                            4. The Thread Class and the Runnable Interface
                            1. Implementing Runnable
                            2. Extending Thread
                            3. Choosing an Approach
                            1. Using Synchronized Methods
                            2. The synchronized Statement
                            1. Deadlock
                            1. Enumerations
                              1. Enumeration Fundamentals
                              2. The values( ) and valueOf( ) Methods
                              3. Java Enumerations Are Class Types
                              4. Enumerations Inherit Enum
                              5. Another Enumeration Example
                              1. Character
                              2. Boolean
                              3. The Numeric Type Wrappers
                              1. Autoboxing and Methods
                              2. Autoboxing/Unboxing Occurs in Expressions
                              3. Autoboxing/Unboxing Boolean and Character Values
                              4. Autoboxing/Unboxing Helps Prevent Errors
                              5. A Word of Warning
                              1. Annotation Basics
                              2. Specifying a Retention Policy
                              3. Obtaining Annotations at Run Time by Use of Reflection
                              4. The AnnotatedElement Interface
                              5. Using Default Values
                              6. Marker Annotations
                              7. Single-Member Annotations
                              8. The Built-In Annotations
                              1. I/O Basics
                                1. Streams
                                2. Byte Streams and Character Streams
                                3. The Predefined Streams
                                1. Reading Characters
                                2. Reading Strings
                                1. Assertion Enabling and Disabling Options
                                1. What Are Generics?
                                2. A Simple Generics Example
                                  1. Generics Work Only with Reference Types
                                  2. Generic Types Differ Based on Their Type Arguments
                                  3. How Generics Improve Type Safety
                                  1. Bounded Wildcards
                                  1. Generic Constructors
                                  1. Using a Generic Superclass
                                  2. A Generic Subclass
                                  3. Run-Time Type Comparisons Within a Generic Hierarchy
                                  4. Casting
                                  5. Overriding Methods in a Generic Class
                                  1. Bridge Methods
                                  1. Type Parameters Can’t Be Instantiated
                                  2. Restrictions on Static Members
                                  3. Generic Array Restrictions
                                  4. Generic Exception Restriction
                                  1. Introducing Lambda Expressions
                                    1. Lambda Expression Fundamentals
                                    2. Functional Interfaces
                                    3. Some Lambda Expression Examples
                                    1. Method References to static Methods
                                    2. Method References to Instance Methods
                                    3. Method References with Generics
                                    1. Module Basics
                                      1. A Simple Module Example
                                      2. Compile and Run the First Module Example
                                      3. A Closer Look at requires and exports
                                      1. Service and Service Provider Basics
                                      2. The Service-Based Keywords
                                      3. A Module-Based Service Example
                                      1. Open Modules
                                      2. The opens Statement
                                      3. requires static
                                      1. Linking Files in an Exploded Directory
                                      2. Linking Modular JAR Files
                                      3. JMOD Files
                                      1. Records
                                        1. Record Basics
                                        2. Create Record Constructors
                                        3. Another Record Constructor Example
                                        4. Create Record Getter Methods
                                        1. Pattern Variables in a Logical AND Expression
                                        2. Pattern Matching in Other Statements
                                        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
                                        5. Another switch Expression Example
                                        6. Pattern Matching in switch
                                        1. Text Block Fundamentals
                                        2. Understanding Leading Whitespace
                                        3. Use Double Quotes in a Text Block
                                        4. Escape Sequences in Text Blocks
                                        5. Pattern Matching with Records
                                        1. Sealed Classes
                                        2. Sealed Interfaces
                                        1. Chapter 18 String Handling
                                          1. The String Constructors
                                          2. String Length
                                          3. Special String Operations
                                            1. String Literals
                                            2. String Concatenation
                                            3. String Concatenation with Other Data Types
                                            4. String Conversion and toString( )
                                            1. charAt( )
                                            2. getChars( )
                                            3. getBytes( )
                                            4. toCharArray( )
                                            1. equals( ) and equalsIgnoreCase( )
                                            2. regionMatches( )
                                            3. startsWith( ) and endsWith( )
                                            4. equals( ) Versus ==
                                            5. compareTo( )
                                            1. substring( )
                                            2. concat( )
                                            3. replace( )
                                            4. trim( ) and strip( )
                                            1. StringBuffer Constructors
                                            2. length( ) and capacity( )
                                            3. ensureCapacity( )
                                            4. setLength( )
                                            5. charAt( ) and setCharAt( )
                                            6. getChars( )
                                            7. append( )
                                            8. insert( )
                                            9. reverse( )
                                            10. delete( ) and deleteCharAt( )
                                            11. replace( )
                                            12. substring( )
                                            13. Additional StringBuffer Methods
                                            1. Primitive Type Wrappers
                                              1. Number
                                              2. Double and Float
                                              3. Understanding isInfinite( ) and isNaN( )
                                              4. Byte, Short, Integer, and Long
                                              5. Character
                                              6. Additions to Character for Unicode Code Point Support
                                              7. Boolean
                                              1. Executing Other Programs
                                              1. Using currentTimeMillis( ) to Time Program Execution
                                              2. Using arraycopy( )
                                              3. Environment Properties
                                              1. Trigonometric Functions
                                              2. Exponential Functions
                                              3. Rounding Functions
                                              4. Miscellaneous Math Methods
                                              1. The Runnable Interface
                                              2. Thread
                                              3. ThreadGroup
                                              1. java.lang.annotation
                                              2. java.lang.constant
                                              3. java.lang.instrument
                                              4. java.lang.invoke
                                              5. java.lang.management
                                              6. java.lang.module
                                              7. java.lang.ref
                                              8. java.lang.reflect
                                              1. Collections Overview
                                              2. The Collection Interfaces
                                                1. The Collection Interface
                                                2. The SequencedCollection Interface
                                                3. The List Interface
                                                4. The Set Interface
                                                5. The SequencedSet Interface
                                                6. The SortedSet Interface
                                                7. The NavigableSet Interface
                                                8. The Queue Interface
                                                9. The Deque Interface
                                                1. The ArrayList Class
                                                2. The LinkedList Class
                                                3. The HashSet Class
                                                4. The LinkedHashSet Class
                                                5. The TreeSet Class
                                                6. The PriorityQueue Class
                                                7. The ArrayDeque Class
                                                8. The EnumSet Class
                                                1. Using an Iterator
                                                2. The For-Each Alternative to Iterators
                                                1. The Map Interfaces
                                                2. The Map Classes
                                                1. Using a Comparator
                                                1. The Enumeration Interface
                                                2. Vector
                                                3. Stack
                                                4. Dictionary
                                                5. Hashtable
                                                6. Properties
                                                7. Using store( ) and load( )
                                                1. StringTokenizer
                                                2. BitSet
                                                3. Optional, OptionalDouble, OptionalInt, and OptionalLong
                                                4. Date
                                                5. Calendar
                                                6. GregorianCalendar
                                                7. TimeZone
                                                8. SimpleTimeZone
                                                9. Locale
                                                10. Random
                                                11. Timer and TimerTask
                                                12. Currency
                                                13. Formatter
                                                  1. The Formatter Constructors
                                                  2. The Formatter Methods
                                                  3. Formatting Basics
                                                  4. Formatting Strings and Characters
                                                  5. Formatting Numbers
                                                  6. Formatting Time and Date
                                                  7. The %n and %% Specifiers
                                                  8. Specifying a Minimum Field Width
                                                  9. Specifying Precision
                                                  10. Using the Format Flags
                                                  11. Justifying Output
                                                  12. The Space, +, 0, and ( Flags
                                                  13. The Comma Flag
                                                  14. The # Flag
                                                  15. The Uppercase Option
                                                  16. Using an Argument Index
                                                  17. Closing a Formatter
                                                  18. The Java printf( ) Connection
                                                  1. The Scanner Constructors
                                                  2. Scanning Basics
                                                  3. Some Scanner Examples
                                                  4. Setting Delimiters
                                                  5. Other Scanner Features
                                                  1. java.util.concurrent, java.util.concurrent.atomic, and java.util.concurrent.locks
                                                  2. java.util.function
                                                  3. java.util.jar
                                                  4. java.util.logging
                                                  5. java.util.prefs
                                                  6. java.util.random
                                                  7. java.util.regex
                                                  8. java.util.spi
                                                  9. java.util.stream
                                                  10. java.util.zip
                                                  1. The I/O Classes and Interfaces
                                                  2. File
                                                    1. Directories
                                                    2. Using FilenameFilter
                                                    3. The listFiles( ) Alternative
                                                    4. Creating Directories
                                                    1. InputStream
                                                    2. OutputStream
                                                    3. FileInputStream
                                                    4. FileOutputStream
                                                    5. ByteArrayInputStream
                                                    6. ByteArrayOutputStream
                                                    7. Filtered Byte Streams
                                                    8. Buffered Byte Streams
                                                    9. SequenceInputStream
                                                    10. PrintStream
                                                    11. DataOutputStream and DataInputStream
                                                    12. RandomAccessFile
                                                    1. Reader
                                                    2. Writer
                                                    3. FileReader
                                                    4. FileWriter
                                                    5. CharArrayReader
                                                    6. CharArrayWriter
                                                    7. BufferedReader
                                                    8. BufferedWriter
                                                    9. PushbackReader
                                                    10. PrintWriter
                                                    1. Serializable
                                                    2. Externalizable
                                                    3. ObjectOutput
                                                    4. ObjectOutputStream
                                                    5. ObjectInput
                                                    6. ObjectInputStream
                                                    7. A Serialization Example
                                                    1. The NIO Classes
                                                    2. NIO Fundamentals
                                                      1. Buffers
                                                      2. Channels
                                                      3. Charsets and Selectors
                                                      1. The Path Interface
                                                      2. The Files Class
                                                      3. The Paths Class
                                                      4. The File Attribute Interfaces
                                                      5. The FileSystem, FileSystems, and FileStore Classes
                                                      1. Use NIO for Channel-Based I/O
                                                      2. Use NIO for Stream-Based I/O
                                                      3. Use NIO for Path and File System Operations
                                                      1. Networking Basics
                                                      2. The java.net Networking Classes and Interfaces
                                                      3. InetAddress
                                                        1. Factory Methods
                                                        2. Instance Methods
                                                        1. DatagramSocket
                                                        2. DatagramPacket
                                                        3. A Datagram Example
                                                        1. Three Key Elements
                                                        2. A Simple HTTP Client Example
                                                        3. Things to Explore in java.net.http
                                                        1. Two Event Handling Mechanisms
                                                        2. The Delegation Event Model
                                                          1. Events
                                                          2. Event Sources
                                                          3. Event Listeners
                                                          1. The ActionEvent Class
                                                          2. The AdjustmentEvent Class
                                                          3. The ComponentEvent Class
                                                          4. The ContainerEvent Class
                                                          5. The FocusEvent Class
                                                          6. The InputEvent Class
                                                          7. The ItemEvent Class
                                                          1. The MouseEvent Class
                                                          2. The MouseWheelEvent Class
                                                          3. The TextEvent Class
                                                          4. The WindowEvent Class
                                                          1. The ActionListener Interface
                                                          2. The AdjustmentListener Interface
                                                          3. The ComponentListener Interface
                                                          4. The ContainerListener Interface
                                                          5. The FocusListener Interface
                                                          6. The ItemListener Interface
                                                          7. The KeyListener Interface
                                                          8. The MouseListener Interface
                                                          9. The MouseMotionListener Interface
                                                          10. The MouseWheelListener Interface
                                                          11. The TextListener Interface
                                                          12. The WindowFocusListener Interface
                                                          13. The WindowListener Interface
                                                          1. Some Key AWT GUI Concepts
                                                          2. Handling Mouse Events
                                                          3. Handling Keyboard Events
                                                          1. Anonymous Inner Classes
                                                          1. AWT Classes
                                                          2. Window Fundamentals
                                                            1. Component
                                                            2. Container
                                                            3. Panel
                                                            4. Window
                                                            5. Frame
                                                            6. Canvas
                                                            1. Setting the Window’s Dimensions
                                                            2. Hiding and Showing a Window
                                                            3. Setting a Window’s Title
                                                            4. Closing a Frame Window
                                                            5. The paint( ) Method
                                                            6. Displaying a String
                                                            7. Setting the Foreground and Background Colors
                                                            8. Requesting Repainting
                                                            9. Creating a Frame-Based Application
                                                            1. Drawing Lines
                                                            2. Drawing Rectangles
                                                            3. Drawing Ellipses and Circles
                                                            4. Drawing Arcs
                                                            5. Drawing Polygons
                                                            6. Demonstrating the Drawing Methods
                                                            7. Sizing Graphics
                                                            1. Color Methods
                                                            2. Setting the Current Graphics Color
                                                            3. A Color Demonstration Program
                                                            1. Determining the Available Fonts
                                                            2. Creating and Selecting a Font
                                                            3. Obtaining Font Information
                                                            1. AWT Control Fundamentals
                                                              1. Adding and Removing Controls
                                                              2. Responding to Controls
                                                              3. The HeadlessException
                                                              1. Handling Buttons
                                                              1. Handling Check Boxes
                                                              1. Handling Choice Lists
                                                              1. Handling Lists
                                                              1. Handling Scroll Bars
                                                              1. Handling a TextField
                                                              1. FlowLayout
                                                              2. BorderLayout
                                                              3. Using Insets
                                                              4. GridLayout
                                                              5. CardLayout
                                                              6. GridBagLayout
                                                              1. File Formats
                                                              2. Image Fundamentals: Creating, Loading, and Displaying
                                                                1. Creating an Image Object
                                                                2. Loading an Image
                                                                3. Displaying an Image
                                                                1. MemoryImageSource
                                                                1. PixelGrabber
                                                                1. CropImageFilter
                                                                2. RGBImageFilter
                                                                1. The Concurrent API Packages
                                                                  1. java.util.concurrent
                                                                  2. java.util.concurrent.atomic
                                                                  3. java.util.concurrent.locks
                                                                  1. Semaphore
                                                                  2. CountDownLatch
                                                                  3. CyclicBarrier
                                                                  4. Exchanger
                                                                  5. Phaser
                                                                  1. A Simple Executor Example
                                                                  2. Using Callable and Future
                                                                  1. The Main Fork/Join Classes
                                                                  2. The Divide-and-Conquer Strategy
                                                                  3. A Simple First Fork/Join Example
                                                                  4. Understanding the Impact of the Level of Parallelism
                                                                  5. An Example that Uses RecursiveTask
                                                                  6. Executing a Task Asynchronously
                                                                  7. Cancelling a Task
                                                                  8. Determining a Task’s Completion Status
                                                                  9. Restarting a Task
                                                                  10. Things to Explore
                                                                  11. Some Fork/Join Tips
                                                                  1. Stream Basics
                                                                    1. Stream Interfaces
                                                                    2. How to Obtain a Stream
                                                                    3. A Simple Stream Example
                                                                    1. Use an Iterator with a Stream
                                                                    2. Use Spliterator
                                                                    1. Regular Expression Processing
                                                                      1. Pattern
                                                                      2. Matcher
                                                                      3. Regular Expression Syntax
                                                                      4. Demonstrating Pattern Matching
                                                                      5. Two Pattern-Matching Options
                                                                      6. Exploring Regular Expressions
                                                                      1. A Simple Client/Server Application Using RMI
                                                                      1. DateFormat Class
                                                                      2. SimpleDateFormat Class
                                                                      1. Time and Date Fundamentals
                                                                      2. Formatting Date and Time
                                                                      3. Parsing Date and Time Strings
                                                                      4. Other Things to Explore in java.time
                                                                      1. Chapter 32 Introducing Swing
                                                                        1. The Origins of Swing
                                                                        2. Swing Is Built on the AWT
                                                                        3. Two Key Swing Features
                                                                          1. Swing Components Are Lightweight
                                                                          2. Swing Supports a Pluggable Look and Feel
                                                                          1. Components
                                                                          2. Containers
                                                                          3. The Top-Level Container Panes
                                                                          1. Painting Fundamentals
                                                                          2. Compute the Paintable Area
                                                                          3. A Paint Example
                                                                          1. JLabel and ImageIcon
                                                                          2. JTextField
                                                                          3. The Swing Buttons
                                                                            1. JButton
                                                                            2. JToggleButton
                                                                            3. Check Boxes
                                                                            4. Radio Buttons
                                                                            1. Menu Basics
                                                                            2. An Overview of JMenuBar, JMenu, and JMenuItem
                                                                              1. JMenuBar
                                                                              2. JMenu
                                                                              3. JMenuItem
                                                                              1. Chapter 35 Java Beans
                                                                                1. What Is a Java Bean?
                                                                                2. Advantages of Beans
                                                                                3. Introspection
                                                                                  1. Design Patterns for Properties
                                                                                  2. Design Patterns for Events
                                                                                  3. Methods and Design Patterns
                                                                                  4. Using the BeanInfo Interface
                                                                                  1. Introspector
                                                                                  2. PropertyDescriptor
                                                                                  3. EventSetDescriptor
                                                                                  4. MethodDescriptor
                                                                                  1. Background
                                                                                  2. The Life Cycle of a Servlet
                                                                                  3. Servlet Development Options
                                                                                  4. Using Tomcat
                                                                                  5. A Simple Servlet
                                                                                    1. Create and Compile the Servlet Source Code
                                                                                    2. Start Tomcat
                                                                                    3. Start a Web Browser and Request the Servlet
                                                                                    1. The Servlet Interface
                                                                                    2. The ServletConfig Interface
                                                                                    3. The ServletContext Interface
                                                                                    4. The ServletRequest Interface
                                                                                    5. The ServletResponse Interface
                                                                                    6. The GenericServlet Class
                                                                                    7. The ServletInputStream Class
                                                                                    8. The ServletOutputStream Class
                                                                                    9. The Servlet Exception Classes
                                                                                    1. The HttpServletRequest Interface
                                                                                    2. The HttpServletResponse Interface
                                                                                    3. The HttpSession Interface
                                                                                    4. The Cookie Class
                                                                                    5. The HttpServlet Class
                                                                                    1. Handling HTTP GET Requests
                                                                                    2. Handling HTTP POST Requests
                                                                                    1. Appendix A Using Java’s Documentation Comments
                                                                                      1. The javadoc Tags
                                                                                        1. @author
                                                                                        2. @deprecated
                                                                                        3. @end
                                                                                        4. @exception
                                                                                        5. @hidden
                                                                                        6. @param
                                                                                        7. @provides
                                                                                        8. @return
                                                                                        9. @see
                                                                                        10. @serial
                                                                                        11. @serialData
                                                                                        12. @serialField
                                                                                        13. @since
                                                                                        14. @start
                                                                                        15. @throws
                                                                                        16. @uses
                                                                                        17. @version
                                                                                        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
                                                                                        Show and hide more

                                                                                        Product information

                                                                                        • Title: Java: The Complete Reference, Thirteenth Edition, 13th Edition
                                                                                        • Author(s): Herbert Schildt, Dr. Danny Coward
                                                                                        • Release date: January 2024
                                                                                        • Publisher(s): McGraw-Hill
                                                                                        • ISBN: 9781265062705