Contents
1 A Quick Tour 1
1.1 Getting Started 1
1.2 Variables 3
1.3 Comments in Code 6
1.4 Named Constants 7
1.5 Unicode Characters 8
1.6 Flow of Control 9
1.7 Classes and Objects 12
1.7.1 Creating Objects 13
1.7.2 Static or Class Fields 14
1.7.3 The Garbage Collector 15
1.8 Methods and Parameters 15
1.8.1 Invoking a Method 15
Contents
1 A Quick Tour 1
1.1 Getting Started 1
1.2 Variables 3
1.3 Comments in Code 6
1.4 Named Constants 7
1.5 Unicode Characters 8
1.6 Flow of Control 9
1.7 Classes and Objects 12
1.7.1 Creating Objects 13
1.7.2 Static or Class Fields 14
1.7.3 The Garbage Collector 15
1.8 Methods and Parameters 15
1.8.1 Invoking a Method 15
1.8.2 The this Reference 17
1.8.3 Static or Class Methods 17
1.9 Arrays 18
1.10 String Objects 21
1.10.1 String Conversion and Formatting 23
1.11 Extending a Class 24
1.11.1 Invoking Methods of the Superclass 25
1.11.2 The 0bject Class 26
1.11.3 Type Casting 27
1.12 Interfaces 27
1.13 Generic Types 29
1.14 Exceptions 32
1.15 Annotations 35
1.16 Packages 36
1.17 The Java Platform 38
1.18 Other Topics Briefly Noted 39
2 Classes and Objects 41
2.1 A Simple Class 42
2.1.1 Class Members 42
2.1.2 Class Modifiers 43
2.2 Fields 44
2.2.1 Field Initialization 44
2.2.2 Static Fields 45
2.2.3 final Fields 46
2.3 Access Control 47
2.4 Creating Objects 49
2.5 Construction and Initialization 50
2.5.1 Constructors 50
2.5.2 Initialization Blocks 54
2.5.3 Static Initialization 55
2.6 Methods 56
2.6.1 Static Methods 58
2.6.2 Method Invocations 58
2.6.3 Methods with Variable Numbers of Arguments 60
2.6.4 Method Execution and Return 62
2.6.5 Parameter Values 63
2.6.6 Using Methods to Control Access 65
2.7 this 68
2.8 Overloading Methods 69
2.9 Importing Static Member Names 71
2.10 The main Method 73
2.11 Native Methods 74
3 Extending Classes 75
3.1 An Extended, Class 76
3.2 Constructors in Extended Classes 80
3.2.1 Constructor Order Dependencies 81
3.3 Inheriting and Redefining Members 84
3.3.1 Overriding 84
3.3.2 Hiding Fields 86
3.3.3 Accessing Inherited Members 86
3.3.4 Accessibility and Overriding 88
3.3.5 Hiding Static Members 89
3.3.6 The super Keyword 89
3.4 Type Compatibility and Conversion 90
3.4.1 Compatibility 90
3.4.2 Explicit Type Casting 91
3.4.3 Testing for Type 92
3.5 What protected Really Means 93
3.6 Marking Methods and Classes final 96
3.7 Abstract Classes and Methods 97
3.8 The Object Class 99
3.9 Cloning Objects 101
3.9.1 Strategies for Cloning 101
3.9.2 Correct Cloning 101
3.9.3 Shallow Versus Deep Cloning 106
3.10 Extending Classes: How and When 107
3.11 Designing a Class to Be Extended 108
3.11.1 Designing a Class to Be Extended 108
3.12 Single Inheritance versus Multiple Inheritance 114
4 Interfaces 117
4.1 A Simple Interface Example 118
4.2 Interface Declarations 120
4.2.1 Interface Constants 121
4.2.2 Interface Methods 122
4.2.3 Interface Modifiers 122
4.3 Extending Interfaces 122
4.3.1 Inheriting and Hiding Constants 123
4.3.2 Inheriting, Overriding, and Overloading Methods 125
4.4 Working with Interfaces 126
4.4.1 Implementing Interfaces 127
4.4.2 Using an Implementation 129
4.5 Marker Interfaces 130
4.6 When to Use Interfaces 131
5 Nested Classes and Interfaces 133
5.1 Static Nested Types 133
5.1.1 Static Nested Types 134
5.1.2 Nested Interfaces 135
5.2 Inner Classes 136
5.2.1 Accessing Enclosing Objects 138
5.2.2 Extending Inner Classes 139
5.2.3 Inheritance, Scoping, and Hiding 140
5.3 Local Inner Classes 142
5.3.1 Inner Classes in Static Contexts 144
5.4 Anonymous Inner Classes 144
5.5 Inheriting Nested Types 146
5.6 Nesting in Interfaces 148
5.6.1 Modifiable Variables in Interfaces 149
5.7 Implementation of Nested Types 149
6 Enumeration Types 151
6.1 A Simple Enum Example 151
6.2 Enum Declarations 152
6.2.1 Enum Modifiers 154
6.3 Enum Constant Declarations 154
6.3.1 Construction 155
6.3.2 Constant Specific Behavior 156
6.4 java.lang.Enum 159
6.5 To Enum or Not 160
7 Tokens, Values, and Variables 161
7.1 Lexical Elements 161
7.1.1 Character Set 161
7.1.2 Comments 163
7.1.3 Tokens 164
7 1 4 Identifiers164
7.1.5 Keywords 165
7.2 Types and Literals 166
7.1.1 Reference Literals 167
7.2.2 Boolean Literals 167
7 2 3 Character Literals 167
7 2 4 Integer Literals 167
7 2 5 Floating-Point Literals 168
7.2.6 String Literals 168
7.2.7 Class Literals 169
7 3 Variables 169
7.3.1 Field and Local Variable Declarations 170
7.3.2 Parameter Variables 171
7.3.3 final Variables 171
7.4 Array Variables 173
7.4.1 Array Modifiers 174
7.4.2 Arrays of Arrays 174
7 4 3 Array Initialization 175
7.4.4 Arrays and Types 177
7.5 The Meanings of Names 178
8 Primitives as Types 183
8.1 Common Fields and Methods 184
8.1.1 Construction 185
8.1.2 Constants 185
8.1.3 Common Methods 186
8.2 Void 187
8.3 Boolean 187
8.4 Number 188
8.4.1 The Integer Wrappers 188
8.4.2 The Floating-Point Wrapper Classes 191
8.5 Character 192
8.5.1 Working with UTF-16 196
8.6 Boxing Conversions 199
9 Operators and Expressions 201
9.1 Arithmetic Operations 201
9.1.1 Integer Arithmetic 202
9 1 2 Floating-Point Arithmetic 202
9.1.3 Strict and Non-Strict Floating-Point Arithmetic 203
9.2 General Operators 204
9.2.1 Increment and Decrement Operators 205
9.2.2 Relational and Equality Operators 206
9.2.3 Logical Operators 207
9.2.4 instanceof 208
9.2.5 Bit Manipulation Operators 208
9 2 6 The Conditional Operator?: 210
9.2.7 Assignment Operators 212
9.2.8 String Concatenation Operator 214
9.2.9 new 214
9.3 Expressions 214
9.3.1 Order of Evaluation 214
9.3.2 Expression Type 215
9.4 Type Conversions 216
9.4.1 Implicit Type Conversions 216
9.4.2 Explicit-Type Casts 217
9.4.3 String Conversions 220
9.5 Operator Precedence and Associativity 221
9.6 Member Access 223
9.6.1 Finding the Right Method 224
10 Control Flow 229
10.1 Statements and Blocks 229
10.2 if-else 230
10.3 switch 232
10.4 while and do-while 235
10.5 for 236
10.5.1 Basic for Statement 236
10.5.2 Enhanced for Statement 239
10.6 Labels 241
10.7 break 241
10.8 continue 244
10.9 return 245
10.10 What, No goto? 246
11 Generic Types 247
11.1 Generic Type Declarations 250
11.1.1 Bounded Type Parameters 252
11.1.2 Nested Generic Types 253
11 2 Working with Generic Types 256
11.2.1 Subtyping and Wildcards 256
11.3 Generic Methods and Constructors 260
11.3.1 Generic Invocations and Type Inference 262
11.4 Wildcard Capture 264
11.5 Under the Hood: Erasure and Raw Types 267
11.5.1 Erasure at Runtime 267
11.5.2 Overloading and Overriding 271
11.6 Finding the Right Method-Revisited 272
11.7 Class Extension and Generic Types 276
12 Exceptions and Assertions 279
12.1 Creating Exception Types 280
12.2 throw 282
12.2.1 Transfer of Control 283
12.2.2 Asynchronous Exceptions 283
12.3 The throws Clause 283
12.3.1 throws Clauses and Method Overriding 285
12.3.2 throws Clauses and Native Methods 286
12.4 try, catch, and finally 286
12.4.1 finally 288
12.5 Exception Chaining 291
12.6 Stack Traces 294
12.7 When to Use Exceptions 294
12.8 Assertions 296
12.8.1 The assert Statement 297
12.9 When to Use Assertions 297
12.9.1 State Assertions 297
12.9.2 Control Flow Assertions 299
12.10 Turning Assertions On and Off 300
12.10.1 Why Turn Assertions On and Off? 300
12.10.2 Controlling Assertions on the Command Line 300
12.10.3 Complete Removal 302
12.10.4 Making Assertions Required 302
13 Strings and Regular Expressions 305
13.1 Character Sequences 305
13.2 The String Class 306
13.2.1 Basic String Operations 306
13.2.2 String Comparisons 308
13.2.3 String Literals, Equivalence and Interning 311
13.2.4 Making Related Strings 313
13.2.5 String Conversions 316
13.2.6 Strings and char Arrays 317
13.2.7 Strings and byte Arrays 319
13.2.8 Character Set Encoding 320
13.3 Regular Expression Matching 321
13.3.1 Regular Expressions 321
13.3.2 Compiling and Matching with Regular Expressions 323
13.3.3 Replacing 326
13.3.4 Regions 329
13.3.5 Efficiency 329
13.4 The StringBuilder Class 330
13.5 Working with UTF-16 336
14 Threads 337
14.1 Creating Threads 339
14.2 Using Runnable 341
14.3 Synchronization 345
14.3.1 synchronized Methods 346
14 3 2 Static synchronized Methods 348
14.3.3 synchronized Statements 348
14.3.4 Synchronization Designs 352
14.4 wait, notifyAll, and notify 354
14.5 Details of Waiting and Notification 357
14.6 Thread Scheduling 358
14.6.1 Voluntary Rescheduling 360
14.7 Deadlocks 362
14.8 Ending Thread Execution 365
14.8.1 Cancelling a Thread 365
14.8.2 Waiting for a Thread to Complete 367
14.9 Ending Application Execution 369
14.10 The Memory Model: Synchronization and volatile 370
14.10.1 Synchronization Actions 372
14.10.2 Final Fields and Security 373
14.10.3 The Happens-Before Relationship 374
14.11 Thread Management, Security, and ThreadGroup 375
14.12 Threads and Exceptions 379
14.12.1 Don't stop381
14.12.2 Stack Traces 382
14.13 ThreadLocal Variables 382
14.14 Debugging Threads 384
15 Annotations 387
15.1 A Simple Annotation Example 388
15.2 Annotation Types 389
15.3 Annotating Elements 392
15.4 Restricting Annotation Applicability 393
15.5 Retention Policies 395
15.6 Working with Annotations 395
16 Reflection 397
16.1 The Class Class 399
16.1.1 Type Tokens 400
16.1.2 Class Inspection 402
16.1.3 Examining Class Members 408
16,l.4 Naming Classes 411
16.1.5 Obtaining Class Objects by Name 413
16.1.6 Runtime Type Queries 414
16.2 Annotation Queries 414
16.3 The Modifier Class 416
16.4 The Member classes 416
16.5 Access Checking and AccessibleObject 417
16.6 The Field Class 418
16.6.1 Final Fields 420
16.7 The Method Class 420
16.8 Creating New Objects and the Constructor Class 423
16.8.1 Inner Class constructors 425
16.9 Generic Type Inspection 426
16.9.1 Type Variables 426
16.9.2 Parameterized Types 427
16.9.3 Wildcards 428
16.9.4 Generic Arrays 428
16.9.5 String Representation of Type Objects 428
16.10 Arrays 429
16.10.1 Genericity and Dynamic Arrays 430
16.11 Packages 432
16.12 The Proxy Class 432
16.13 Loading Classes 435
16.13.1 The ClassLoader Class 438
16.13.2 Preparing a Class for Use 441
16.13.3 Loading Related Resources 442
16.14 Controlling Assertions at Runtime 444
17 Garbage Collection and Memory 447
17.1 Garbage Collection 447
17.2 A Simple Model 448
17.3 Finalization 449
17.3.1 Resurrecting Objects during finalize 452
17.4 Interacting with the Garbage Collector 452
17.5 Reachability States and Reference Objects 454
17.5.1 The Reference Class 455
17.5.2 Strengths of Reference and Reachability 455
17.5.3 Reference Queues 459
17.5.4 Finalization and Reachability 464
18 Packages 467
18.1 Package Naming 468
18.2 Type Imports 469
18.3 Package Access 471
18.3.1 Accessibility and Overriding Methods 472
18.4 Package Contents 475
18.5 Package Annotations 476
18.6 Package Objects and Specifications 477
19 Documentation Comments 481
19.1 The Anatomy of a Doc Comment 482
19.2 Tags 483
19.2.1 @see 483
19.2.2 {@lin