Java™: A Beginner’s Guide, Ninth Edition
By Herbert Schildt
Contents:
Introduction .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
1 Java Fundamentals ………………………………………………………………………………. 1
The History and Philosophy of Java …………………………………………………………………….. 3
The Origins of Java …………………………………………………………………………………… 3
Java’s Lineage: C and C++ ………………………………………………………………………… 4
How Java Impacted the Internet …………………………………………………………………. 4
Java’s Magic: The Bytecode ………………………………………………………………………. 6
Moving Beyond Applets ……………………………………………………………………………. 8
A Faster Release Schedule …………………………………………………………………………. 8
The Java Buzzwords …………………………………………………………………………………. 9
Object-Oriented Programming ……………………………………………………………………………. 10
Encapsulation …………………………………………………………………………………………… 11
Polymorphism ………………………………………………………………………………………….. 11
Inheritance ………………………………………………………………………………………………. 12
The Java Development Kit …………………………………………………………………………………. 12
A First Simple Program ……………………………………………………………………………………… 13
Entering the Program ………………………………………………………………………………… 14
Compiling the Program ……………………………………………………………………………… 14
The First Sample Program Line by Line ……………………………………………………… 15
Handling Syntax Errors ……………………………………………………………………………………… 17
A Second Simple Program …………………………………………………………………………………. 18
Another Data Type ……………………………………………………………………………………………. 20
Try This 1-1: Converting Gallons to Liters …………………………………………………………… 21
Two Control Statements …………………………………………………………………………………….. 22
The if Statement ……………………………………………………………………………………….. 23
The for Loop ……………………………………………………………………………………………. 24
Create Blocks of Code ……………………………………………………………………………………….. 26
Semicolons and Positioning ……………………………………………………………………………….. 27
Indentation Practices …………………………………………………………………………………………. 28
Try This 1-2: Improving the Gallons-to-Liters Converter ……………………………………….. 28
The Java Keywords ……………………………………………………………………………………………. 29
Identifiers in Java ………………………………………………………………………………………………. 30
The Java Class Libraries …………………………………………………………………………………….. 31
Chapter 1 Self Test ……………………………………………………………………………………………. 31
2 Introducing Data Types and Operators ………………………………………………… 33
Why Data Types Are Important …………………………………………………………………………… 34
Java’s Primitive Types ……………………………………………………………………………………….. 34
Integers …………………………………………………………………………………………………… 35
Floating-Point Types …………………………………………………………………………………. 37
Characters ……………………………………………………………………………………………….. 37
The Boolean Type ……………………………………………………………………………………………… 39
Try This 2-1: How Far Away Is the Lightning? ……………………………………………………… 40
Literals …………………………………………………………………………………………………………….. 41
Hexadecimal, Octal, and Binary Literals ……………………………………………………… 42
Character Escape Sequences ………………………………………………………………………. 42
String Literals ………………………………………………………………………………………….. 43
A Closer Look at Variables …………………………………………………………………………………. 44
Initializing a Variable ………………………………………………………………………………… 44
Dynamic Initialization ………………………………………………………………………………. 45
The Scope and Lifetime of Variables …………………………………………………………………… 45
Operators …………………………………………………………………………………………………………. 48
Arithmetic Operators …………………………………………………………………………………………. 48
Increment and Decrement ………………………………………………………………………….. 49
Relational and Logical Operators ………………………………………………………………………… 50
Short-Circuit Logical Operators ………………………………………………………………………….. 52
The Assignment Operator …………………………………………………………………………………… 53
Shorthand Assignments ……………………………………………………………………………………… 53
Type Conversion in Assignments ………………………………………………………………………… 55
Casting Incompatible Types ……………………………………………………………………………….. 56
Operator Precedence …………………………………………………………………………………………. 58
Try This 2-2: Display a Truth Table for the Logical Operators ………………………………… 59
Expressions ………………………………………………………………………………………………………. 60
Type Conversion in Expressions …………………………………………………………………. 60
Spacing and Parentheses ……………………………………………………………………………. 62
Chapter 2 Self Test ……………………………………………………………………………………………. 62
3 Program Control Statements ……………………………………………………………….. 65
Input Characters from the Keyboard ……………………………………………………………………. 66
The if Statement ……………………………………………………………………………………………….. 67
Nested ifs …………………………………………………………………………………………………………. 69
The if-else-if Ladder ………………………………………………………………………………………….. 70
The Traditional switch Statement ………………………………………………………………………… 71
Nested switch Statements …………………………………………………………………………………… 75
Try This 3-1: Start Building a Java Help System …………………………………………………… 75
The for Loop …………………………………………………………………………………………………….. 77
Some Variations on the for Loop …………………………………………………………………………. 79
Missing Pieces ………………………………………………………………………………………………….. 80
The Infinite Loop ……………………………………………………………………………………… 81
Loops with No Body …………………………………………………………………………………………. 81
Declaring Loop Control Variables Inside the for Loop …………………………………………… 82
The Enhanced for Loop ……………………………………………………………………………………… 83
The while Loop ………………………………………………………………………………………………… 83
The do-while Loop ……………………………………………………………………………………………. 85
Try This 3-2: Improve the Java Help System ………………………………………………………… 87
Use break to Exit a Loop ……………………………………………………………………………………. 90
Use break as a Form of goto ……………………………………………………………………………….. 91
Use continue …………………………………………………………………………………………………….. 96
Try This 3-3: Finish the Java Help System …………………………………………………………… 97
Nested Loops ……………………………………………………………………………………………………. 101
Chapter 3 Self Test ……………………………………………………………………………………………. 102
4 Introducing Classes, Objects, and Methods …………………………………………… 105
Class Fundamentals …………………………………………………………………………………………… 106
The General Form of a Class ……………………………………………………………………… 107
Defining a Class ……………………………………………………………………………………….. 108
How Objects Are Created …………………………………………………………………………………… 110
Reference Variables and Assignment …………………………………………………………………… 111
Methods …………………………………………………………………………………………………………… 112
Adding a Method to the Vehicle Class …………………………………………………………. 112
Returning from a Method …………………………………………………………………………………… 114
Returning a Value ……………………………………………………………………………………………… 115
Using Parameters ………………………………………………………………………………………………. 117
Adding a Parameterized Method to Vehicle …………………………………………………. 119
Try This 4-1: Creating a Help Class …………………………………………………………………….. 121
Constructors …………………………………………………………………………………………………….. 126
Parameterized Constructors ………………………………………………………………………………… 128
Adding a Constructor to the Vehicle Class ……………………………………………………………. 128
The new Operator Revisited ……………………………………………………………………………….. 130
Garbage Collection ……………………………………………………………………………………………. 130
The this Keyword ……………………………………………………………………………………………… 131
Chapter 4 Self Test ……………………………………………………………………………………………. 133
5 More Data Types and Operators ………………………………………………………….. 135
Arrays ……………………………………………………………………………………………………………… 136
One-Dimensional Arrays …………………………………………………………………………… 137
Try This 5-1: Sorting an Array ……………………………………………………………………………. 140
Multidimensional Arrays ……………………………………………………………………………………. 142
Two-Dimensional Arrays …………………………………………………………………………… 142
Irregular Arrays ………………………………………………………………………………………… 143
Arrays of Three or More Dimensions ………………………………………………………….. 144
Initializing Multidimensional Arrays …………………………………………………………… 144
Alternative Array Declaration Syntax ………………………………………………………………….. 145
Assigning Array References ……………………………………………………………………………….. 146
Using the length Member …………………………………………………………………………………… 147
Try This 5-2: A Queue Class ………………………………………………………………………………. 149
The For-Each Style for Loop ………………………………………………………………………………. 153
Iterating Over Multidimensional Arrays ………………………………………………………. 156
Applying the Enhanced for ………………………………………………………………………… 157
Strings ……………………………………………………………………………………………………………… 158
Constructing Strings …………………………………………………………………………………. 159
Operating on Strings …………………………………………………………………………………. 160
Arrays of Strings ………………………………………………………………………………………. 162
Strings Are Immutable ………………………………………………………………………………. 162
Using a String to Control a switch Statement ……………………………………………….. 163
Using Command-Line Arguments ……………………………………………………………………….. 166
Using Type Inference with Local Variables …………………………………………………………… 167
Local Variable Type Inference with Reference Types ……………………………………. 169
Using Local Variable Type Inference in a for Loop ……………………………………….. 171
Some var Restrictions ……………………………………………………………………………….. 171
The Bitwise Operators ……………………………………………………………………………………….. 172
The Bitwise AND, OR, XOR, and NOT Operators ……………………………………….. 173
The Shift Operators …………………………………………………………………………………… 177
Bitwise Shorthand Assignments …………………………………………………………………. 179
Try This 5-3: A ShowBits Class ………………………………………………………………………….. 180
The ? Operator ………………………………………………………………………………………………….. 182
Chapter 5 Self Test ……………………………………………………………………………………………. 184
6 A Closer Look at Methods and Classes …………………………………………………. 187
Controlling Access to Class Members …………………………………………………………………. 188
Java’s Access Modifiers …………………………………………………………………………….. 189
Try This 6-1: Improving the Queue Class …………………………………………………………….. 193
Pass Objects to Methods …………………………………………………………………………………….. 194
How Arguments Are Passed ……………………………………………………………………….. 196
Returning Objects ……………………………………………………………………………………………… 198
Method Overloading ………………………………………………………………………………………….. 200
Overloading Constructors …………………………………………………………………………………… 205
Try This 6-2: Overloading the Queue Constructor …………………………………………………. 207
Recursion …………………………………………………………………………………………………………. 210
Understanding static ………………………………………………………………………………………….. 212
Static Blocks ……………………………………………………………………………………………. 215
Try This 6-3: The Quicksort ……………………………………………………………………………….. 216
Introducing Nested and Inner Classes ………………………………………………………………….. 219
Varargs: Variable-Length Arguments …………………………………………………………………… 222
Varargs Basics ………………………………………………………………………………………….. 223
Overloading Varargs Methods ……………………………………………………………………. 226
Varargs and Ambiguity ……………………………………………………………………………… 227
Chapter 6 Self Test ……………………………………………………………………………………………. 228
7 Inheritance ………………………………………………………………………………………….. 231
Inheritance Basics …………………………………………………………………………………………….. 232
Member Access and Inheritance …………………………………………………………………………. 235
Constructors and Inheritance ………………………………………………………………………………. 238
Using super to Call Superclass Constructors ………………………………………………………… 240
Using super to Access Superclass Members …………………………………………………………. 244
Try This 7-1: Extending the Vehicle Class ……………………………………………………………. 245
Creating a Multilevel Hierarchy ………………………………………………………………………….. 248
When Are Constructors Executed? ………………………………………………………………………. 250
Superclass References and Subclass Objects ………………………………………………………… 252
Method Overriding ……………………………………………………………………………………………. 256
Overridden Methods Support Polymorphism ………………………………………………………… 259
Why Overridden Methods? ………………………………………………………………………………… 261
Applying Method Overriding to TwoDShape ……………………………………………….. 261
Using Abstract Classes ………………………………………………………………………………………. 265
Using final ……………………………………………………………………………………………………….. 269
final Prevents Overriding …………………………………………………………………………… 269
final Prevents Inheritance ………………………………………………………………………….. 269
Using final with Data Members ………………………………………………………………….. 270
The Object Class ………………………………………………………………………………………………. 271
Chapter 7 Self Test ……………………………………………………………………………………………. 272
8 Packages and Interfaces ………………………………………………………………………. 275
Packages ………………………………………………………………………………………………………….. 276
Defining a Package …………………………………………………………………………………… 277
Finding Packages and CLASSPATH …………………………………………………………… 278
A Short Package Example …………………………………………………………………………. 278
Packages and Member Access …………………………………………………………………………….. 280
A Package Access Example ……………………………………………………………………….. 281
Understanding Protected Members ……………………………………………………………………… 282
Importing Packages …………………………………………………………………………………………… 284
Java’s Class Library Is Contained in Packages ……………………………………………………… 286
Interfaces …………………………………………………………………………………………………………. 286
Implementing Interfaces …………………………………………………………………………………….. 287
Using Interface References ………………………………………………………………………………… 291
Try This 8-1: Creating a Queue Interface ……………………………………………………………… 293
Variables in Interfaces ……………………………………………………………………………………….. 298
Interfaces Can Be Extended ……………………………………………………………………………….. 299
Default Interface Methods ………………………………………………………………………………….. 300
Default Method Fundamentals …………………………………………………………………… 301
A More Practical Example of a Default Method …………………………………………… 303
Multiple Inheritance Issues ………………………………………………………………………… 304
Use static Methods in an Interface ………………………………………………………………………. 305
Private Interface Methods …………………………………………………………………………………… 306
Final Thoughts on Packages and Interfaces ………………………………………………………….. 307
Chapter 8 Self Test ……………………………………………………………………………………………. 307
9 Exception Handling ……………………………………………………………………………… 309
The Exception Hierarchy …………………………………………………………………………………… 311
Exception Handling Fundamentals ……………………………………………………………………… 311
Using try and catch …………………………………………………………………………………… 312
A Simple Exception Example …………………………………………………………………….. 312
The Consequences of an Uncaught Exception ………………………………………………………. 314
Exceptions Enable You to Handle Errors Gracefully ……………………………………… 316
Using Multiple catch Statements …………………………………………………………………………. 317
Catching Subclass Exceptions …………………………………………………………………………….. 318
Try Blocks Can Be Nested …………………………………………………………………………………. 319
Throwing an Exception ……………………………………………………………………………………… 320
Rethrowing an Exception …………………………………………………………………………… 321
A Closer Look at Throwable ………………………………………………………………………………. 322
Using finally …………………………………………………………………………………………………….. 324
Using throws …………………………………………………………………………………………………….. 326
Three Additional Exception Features …………………………………………………………………… 327
Java’s Built-in Exceptions ………………………………………………………………………………….. 329
Creating Exception Subclasses ……………………………………………………………………………. 331
Try This 9-1: Adding Exceptions to the Queue Class …………………………………………….. 333
Chapter 9 Self Test ……………………………………………………………………………………………. 337
10 Using I/O …………………………………………………………………………………………….. 339
Java’s I/O Is Built upon Streams …………………………………………………………………………. 341
Byte Streams and Character Streams …………………………………………………………………… 341
The Byte Stream Classes ……………………………………………………………………………………. 341
The Character Stream Classes …………………………………………………………………………….. 342
The Predefined Streams ……………………………………………………………………………………… 343
Using the Byte Streams ……………………………………………………………………………………… 344
Reading Console Input ……………………………………………………………………………… 345
Writing Console Output …………………………………………………………………………….. 346
Reading and Writing Files Using Byte Streams …………………………………………………….. 347
Inputting from a File …………………………………………………………………………………. 347
Writing to a File ……………………………………………………………………………………….. 351
Automatically Closing a File ………………………………………………………………………………. 353
Reading and Writing Binary Data ……………………………………………………………………….. 356
Try This 10-1: A File Comparison Utility …………………………………………………………….. 359
Random-Access Files ………………………………………………………………………………………… 360
Using Java’s Character-Based Streams ………………………………………………………………… 362
Console Input Using Character Streams ………………………………………………………. 364
Console Output Using Character Streams ……………………………………………………. 368
File I/O Using Character Streams ……………………………………………………………………….. 369
Using a FileWriter ……………………………………………………………………………………. 369
Using a FileReader …………………………………………………………………………………… 370
Using Java’s Type Wrappers to Convert Numeric Strings ………………………………………. 372
Try This 10-2: Creating a Disk-Based Help System ………………………………………………. 374
Chapter 10 Self Test ………………………………………………………………………………………….. 381
11 Multithreaded Programming ……………………………………………………………….. 383
Multithreading Fundamentals …………………………………………………………………………….. 384
The Thread Class and Runnable Interface ……………………………………………………………. 385
Creating a Thread ……………………………………………………………………………………………… 386
One Improvement and Two Simple Variations ……………………………………………… 389
Try This 11-1: Extending Thread ………………………………………………………………………… 393
Creating Multiple Threads ………………………………………………………………………………….. 396
Determining When a Thread Ends ………………………………………………………………………. 399
Thread Priorities ……………………………………………………………………………………………….. 402
Synchronization ………………………………………………………………………………………………… 406
Using Synchronized Methods …………………………………………………………………………….. 406
The synchronized Statement ………………………………………………………………………………. 409
Thread Communication Using notify( ), wait( ), and notifyAll( ) ……………………………. 412
An Example That Uses wait( ) and notify( ) …………………………………………………. 413
Suspending, Resuming, and Stopping Threads ……………………………………………………… 418
Try This 11-2: Using the Main Thread …………………………………………………………………. 422
Chapter 11 Self Test ………………………………………………………………………………………….. 424
12 Enumerations, Autoboxing, Annotations, and More ………………………………. 425
Enumerations ……………………………………………………………………………………………………. 426
Enumeration Fundamentals ……………………………………………………………………….. 427
Java Enumerations Are Class Types …………………………………………………………………….. 429
The values( ) and valueOf( ) Methods ………………………………………………………………….. 429
Constructors, Methods, Instance Variables, and Enumerations ……………………………….. 431
Two Important Restrictions ……………………………………………………………………….. 433
Enumerations Inherit Enum ……………………………………………………………………………….. 433
Try This 12-1: A Computer-Controlled Traffic Light …………………………………………….. 435
Autoboxing ………………………………………………………………………………………………………. 440
Type Wrappers ………………………………………………………………………………………………….. 440
Autoboxing Fundamentals …………………………………………………………………………………. 442
Autoboxing and Methods …………………………………………………………………………………… 443
Autoboxing/Unboxing Occurs in Expressions ………………………………………………………. 445
A Word of Warning …………………………………………………………………………………… 446
Static Import …………………………………………………………………………………………………….. 447
Annotations (Metadata) ……………………………………………………………………………………… 450
Introducing instanceof ……………………………………………………………………………………….. 453
Chapter 12 Self Test ………………………………………………………………………………………….. 455
13 Generics ………………………………………………………………………………………………. 457
Generics Fundamentals ……………………………………………………………………………………… 458
A Simple Generics Example ………………………………………………………………………………. 459
Generics Work Only with Reference Types ………………………………………………….. 463
Generic Types Differ Based on Their Type Arguments ………………………………….. 463
A Generic Class with Two Type Parameters …………………………………………………. 464
The General Form of a Generic Class …………………………………………………………. 465
Bounded Types …………………………………………………………………………………………………. 466
Using Wildcard Arguments ………………………………………………………………………………… 469
Bounded Wildcards …………………………………………………………………………………………… 472
Generic Methods ………………………………………………………………………………………………. 475
Generic Constructors …………………………………………………………………………………………. 477
Generic Interfaces ……………………………………………………………………………………………… 478
Try This 13-1: Create a Generic Queue ……………………………………………………………….. 480
Raw Types and Legacy Code ……………………………………………………………………………… 485
Type Inference with the Diamond Operator ………………………………………………………….. 488
Local Variable Type Inference and Generics …………………………………………………………. 489
Erasure …………………………………………………………………………………………………………….. 489
Ambiguity Errors ………………………………………………………………………………………………. 490
Some Generic Restrictions …………………………………………………………………………………. 491
Type Parameters Can’t Be Instantiated ………………………………………………………… 491
Restrictions on Static Members ………………………………………………………………….. 491
Generic Array Restrictions ………………………………………………………………………… 492
Generic Exception Restriction ……………………………………………………………………. 493
Continuing Your Study of Generics ……………………………………………………………………… 493
Chapter 13 Self Test ………………………………………………………………………………………….. 493
14 Lambda Expressions and Method References ………………………………………… 495
Introducing Lambda Expressions ………………………………………………………………………… 496
Lambda Expression Fundamentals ……………………………………………………………… 497
Functional Interfaces ………………………………………………………………………………… 498
Lambda Expressions in Action …………………………………………………………………… 500
Block Lambda Expressions ………………………………………………………………………………… 505
Generic Functional Interfaces …………………………………………………………………………….. 506
Try This 14-1: Pass a Lambda Expression as an Argument …………………………………….. 508
Lambda Expressions and Variable Capture …………………………………………………………… 513
Throw an Exception from Within a Lambda Expression ………………………………………… 514
Method References ……………………………………………………………………………………………. 516
Method References to static Methods ………………………………………………………….. 516
Method References to Instance Methods ……………………………………………………… 518
Constructor References ……………………………………………………………………………………… 522
Predefined Functional Interfaces …………………………………………………………………………. 525
Chapter 14 Self Test ………………………………………………………………………………………….. 527
15 Modules ………………………………………………………………………………………………. 529
Module Basics ………………………………………………………………………………………………….. 531
A Simple Module Example ……………………………………………………………………….. 532
Compile and Run the First Module Example ……………………………………………….. 536
A Closer Look at requires and exports ……………………………………………………….. 537
java.base and the Platform Modules …………………………………………………………………….. 538
Legacy Code and the Unnamed Module ………………………………………………………………. 540
Exporting to a Specific Module …………………………………………………………………………… 541
Using requires transitive …………………………………………………………………………………….. 542
Try This 15-1: Experiment with requires transitive ……………………………………………….. 543
Use Services …………………………………………………………………………………………………….. 546
Service and Service Provider Basics …………………………………………………………… 547
The Service-Based Keywords …………………………………………………………………….. 548
A Module-Based Service Example ……………………………………………………………… 548
Additional Module Features ……………………………………………………………………………….. 555
Open Modules ………………………………………………………………………………………….. 555
The opens Statement …………………………………………………………………………………. 556
requires static …………………………………………………………………………………………… 556
Continuing Your Study of Modules ……………………………………………………………………… 556
Chapter 15 Self Test ………………………………………………………………………………………….. 557
16 Switch Expressions, Records, and Other Recently Added Features ……….. 559
Enhancements to switch …………………………………………………………………………………….. 561
Use a List of case Constants ………………………………………………………………………. 563
Introducing the switch Expression and the yield Statement ……………………………. 563
Introducing the Arrow in a case Statement …………………………………………………… 565
A Closer Look at the Arrow case ………………………………………………………………… 567
Try This 16-1: Use a switch Expression to Obtain a City’s Time Zone …………………….. 571
Records ……………………………………………………………………………………………………………. 573
Record Basics ………………………………………………………………………………………….. 574
Create Record Constructors ……………………………………………………………………….. 576
A Closer Look at Record Getter Methods ……………………………………………………. 581
Pattern Matching with instanceof ………………………………………………………………………… 581
Sealed Classes and Interfaces ……………………………………………………………………………… 583
Sealed Classes ………………………………………………………………………………………….. 583
Sealed Interfaces ………………………………………………………………………………………. 586
Future Directions ………………………………………………………………………………………………. 587
Chapter 16 Self Test ………………………………………………………………………………………….. 588
17 Introducing Swing ……………………………………………………………………………….. 591
The Origins and Design Philosophy of Swing ………………………………………………………. 593
Components and Containers ……………………………………………………………………………….. 595
Components …………………………………………………………………………………………….. 595
Containers ……………………………………………………………………………………………….. 596
The Top-Level Container Panes ………………………………………………………………….. 596
Layout Managers ………………………………………………………………………………………………. 597
A First Simple Swing Program …………………………………………………………………………… 597
The First Swing Example Line by Line ……………………………………………………….. 599
Swing Event Handling ……………………………………………………………………………………….. 602
Events …………………………………………………………………………………………………….. 603
Event Sources ………………………………………………………………………………………….. 603
Event Listeners ………………………………………………………………………………………… 603
Event Classes and Listener Interfaces …………………………………………………………. 604
Use JButton ……………………………………………………………………………………………………… 604
Work with JTextField ………………………………………………………………………………………… 608
Create a JCheckBox ………………………………………………………………………………………….. 611
Work with JList ………………………………………………………………………………………………… 615
Try This 17-1: A Swing-Based File Comparison Utility …………………………………………. 619
Use Anonymous Inner Classes or Lambda Expressions to Handle Events ………………… 624
Chapter 17 Self Test ……………………………………………………………………………………… 626
A Answers to Self Tests ……………………………………………………………………………. 627
Chapter 1: Java Fundamentals …………………………………………………………………………….. 628
Chapter 2: Introducing Data Types and Operators …………………………………………………. 630
Chapter 3: Program Control Statements ……………………………………………………………….. 631
Chapter 4: Introducing Classes, Objects, and Methods ………………………………………….. 634
Chapter 5: More Data Types and Operators ………………………………………………………….. 635
Chapter 6: A Closer Look at Methods and Classes ………………………………………………… 640
Chapter 7: Inheritance ……………………………………………………………………………………….. 645
Chapter 8: Packages and Interfaces ……………………………………………………………………… 647
Chapter 9: Exception Handling …………………………………………………………………………… 649
Chapter 10: Using I/O ……………………………………………………………………………………….. 652
Chapter 11: Multithreaded Programming …………………………………………………………….. 656
Chapter 12: Enumerations, Autoboxing, Annotations, and More …………………………….. 658
Chapter 13: Generics …………………………………………………………………………………………. 662
Chapter 14: Lambda Expressions and Method References ……………………………………… 666
Chapter 15: Modules …………………………………………………………………………………………. 670
Chapter 16: Switch Expressions, Records, and Other Recently Added Features ……….. 671
Chapter 17: Introducing Swing …………………………………………………………………………… 675
B Using Java’s Documentation Comments ……………………………………………….. 683
The javadoc Tags ………………………………………………………………………………………………. 684
@author ………………………………………………………………………………………………….. 685
{@code} …………………………………………………………………………………………………. 685
@deprecated ……………………………………………………………………………………………. 685
{@docRoot} ……………………………………………………………………………………………. 685
@exception ……………………………………………………………………………………………… 686
@hidden ………………………………………………………………………………………………….. 686
{@index} ………………………………………………………………………………………………… 686
{@inheritDoc} …………………………………………………………………………………………. 686
{@link} …………………………………………………………………………………………………… 686
{@linkplain} ……………………………………………………………………………………………. 687
{@literal} ……………………………………………………………………………………………….. 687
@param …………………………………………………………………………………………………… 687
@provides ……………………………………………………………………………………………….. 687
@return …………………………………………………………………………………………………… 687
@see ……………………………………………………………………………………………………….. 687
@since ……………………………………………………………………………………………………. 688
{@summary} …………………………………………………………………………………………… 688
@throws ………………………………………………………………………………………………….. 688
@uses ……………………………………………………………………………………………………… 688
{@value} ………………………………………………………………………………………………… 688
@version …………………………………………………………………………………………………. 689
The General Form of a Documentation Comment …………………………………………………. 689
What javadoc Outputs ……………………………………………………………………………………….. 689
An Example That Uses Documentation Comments ……………………………………………….. 689
C Compile and Run Simple Single-File Programs in One Step ………………….. 691
D Introducing JShell ……………………………………………………………………………….. 693
JShell Basics …………………………………………………………………………………………………….. 694
List, Edit, and Rerun Code …………………………………………………………………………………. 696
Add a Method …………………………………………………………………………………………………… 697
Create a Class …………………………………………………………………………………………………… 698
Use an Interface ………………………………………………………………………………………………… 699
Evaluate Expressions and Use Built-in Variables …………………………………………………… 700
Importing Packages …………………………………………………………………………………………… 701
Exceptions ……………………………………………………………………………………………………….. 702
Some More JShell Commands ……………………………………………………………………………. 702
Exploring JShell Further ……………………………………………………………………………………. 703
E More Java Keywords ……………………………………………………………………………. 705
The transient and volatile Modifiers …………………………………………………………………….. 706
strictfp ……………………………………………………………………………………………………………… 706
assert ……………………………………………………………………………………………………………….. 707
Native Methods ………………………………………………………………………………………………… 708
Another Form of this …………………………………………………………………………………………. 708
Index .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711