Java程序设计语言(英文版)(第4版)

目 录内容简介
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 Dont 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 {@link} and {@1inkplain} 484
19.2.3 @param 485
19.2.4 @return 485
19.2.5 @throws and @exception 485
19.2.6 @deprecated 486
19.2.7 @author 486
19.2.8 @version 487
19.2.9 @since 487
19.2.10 {@literal} and {@code} 487
19.2.11 {@value} 487
19.2.12 {@docRoot} 488
19.2.13 {@inheritDoc} 488
19.3 Inheriting Method Documentation Comments 489
19.3.1 Inheriting @throws Comments 490
19.4 A Simple Example 491
19.5 External Conventions 496
19.5.1 Package and Overview Documentation 496
19.5.2 The doc-files Directory 497
19.6 Notes on Usage 497
20 The I/O Package 499
20.1 Streams Overview 500
20.2 Byte Streams 501
20.2.1 InputStream 503
20.2.2 Outputstream 505
20.3 Character Streams 507
20.3.l Reader 508
20.3.2 Writer 510
20.3.3 Character Streams and the Standard Streams 511
20.4 InputStreamReader and OutputStreamWriter 512
20.5 A Quick Tour of the Stream Classes 514
20.5.1 Synchronization and Concurrency 515
20.5.2 Filter Streams 516
20.5.3 Buffered Streams 518
20.5.4 Piped Streams 520
20.5.5 ByteArray Byte Streams 521
20.5.6 CharArray Character Streams 522
20.5.7 String Character Streams 523
20.5.8 Print Streams 525
20.5.9 LineNumberReader 527
20.5.10 SequenceInputStream 528
20.5.11 Pushback Streams 529
20.5.12 StreamTokenizer 532
20.6 The Data Byte Streams 537
20.6.1 DataInput and DataOutput 537
20.6.2 The Data Stream Classes 539
20.7 Working with Files 540
20.7.1 Fi1e Streams and Fi1eDescriptor 540
20.7.2 RandomAccessFile 541
20.7.3 The File Class 543
20.7.4 FilenameFilter and FileFi1ter 548
20.8 Object Serialization 549
20.8.1 The Object Byte Streams 549
20.8.2 Making Your Classes Serializable 551
20.8.3 Serialization and Deserialization Order 552
20 8 4 Customized Serialization 554
20.8.5 Object Versioning. 555
20 8 6 Serialized Fields 559
20.8.7 The Externalizable Interface 561
20.8.8 Documentation Comment Tags 562
20.9 The IOException Classes 563
20.10 A Taste of New I/O 565
21 Collections 567
21.1 Collections 567
21.1.1 Exception Conventions 571
21.2 Iteration 571
21.3 Ordering with Comparable and Comparator 574
21.4 The Collection Interface 575
21.5 Set and SortedSet 577
21.5.1 HashSet 579
21.5.2 LinkedHashSet 580
21.5.3 TreeSet 580
21.6 List 580
21.6.1 ArrayList 582
21.6.2 LinkedList 583
21.6.3 RandomAccess Lists 584
21.7 Queue 585
21.7.1 PriorityQueue 586
21.8 Map and SortedMap 587
21.8.1 HashNap 590
21.8.2 LinkedHashMap 591
21.8.3 IdentityHashNap 592
21.8.4 WeakHashNap 592
21.8.5 TreeMap 593
21.9 enum Collections 594
21.9.1 EnumSet 594
21.9.2 EnumNap 596
21.10 Wrapped Collections and the Collections Class 597
21.10.1 The Collections Utilities 597
21.10.2 The Unmodifiable Wrappers 601
21.10.3 The Checked Wrappers 601
21.11 Synchronized Wrappers and Concurrent Collections 602
21.11.1 The Synchronized Wrappers 602
21.11.2 The Concurrent Collections 604
21.12 The Arrays Utility Class 607
21.13 Writing Iterator Implementations 609
21.14 Writing Collection Implementations 611
21.15 The Legacy Collection Types 616
21.15.1 Enumeration 617
21.15.2 Vector 617
21.15.3 Stack 619
21.15.4 Dictionary 619
21.15.5 Hashtable 619
21.16 Properties 620
22 Miscellaneous Utilities 623
22.1 Formatter 624
22.1.1 Format Specifiers 626
22.1.2 Integer Conversions 627
22.1.3 Floating-Point Conversions 627
22.1.4 Character Conversions 629
22.1.5 General Conversions 629
22.1.6 Custom Formatting 630
22.1.7 Format Exceptlons 630
22.1.8 The Formatter Class 631
22.2 BitSet 632
22.3 Observer/Observable 635
22.4 Random 639
22.5 Scanner 641
22.5.1 Stream of Values 641
22.5.2 Scanning Lines 644
22.5.3 Using Scanner 647
22.5.4 Localization 651
22.6 StringTokenizer 651
22.7 Timer and TimerTask 653
22.8 UUID 656
22.9 Math and StrictMath 657
23 System Programming 661
23.1 The System Class 662
23.1.1 Standard I/O Streams 662
23.1.2 System Properties 663
23.1.3 Utility Methods 665
23.2 Creating Processes 666
23.2.1 Process 667
23.2.2 Process Environments 669
23.2.3 ProcessBuilder 670
23.2.4 Portability 672
23.3 Shutdown 672
23.3.1 Shutdown Hooks 672
23.3.2 The Shutdown Sequence 674
23.3.3 Shutdown Strategies 674
23.4 The Rest of Runtime 675
23.4.1 Loading Native Code 676
23.4.2 Debugging 676
23.5 Security 677
23.5.1 The SecurityManager Class 678
23.5.2 Permissions 679
23 5 3 Security Policies 680
23.5.4 Access Controllers and Privileged Execution 681
24 Internationalization and Localization 685
24.1 Locale 686
24.2 Resource Bundles 688
24.2.1 ListResourceBundle 691
24.2.2 PropertyResourceBundle 692
24.2.3 Subclassing ResourceBundle 693
24.3 Currency 694
24.4 Time, Dates, and Calendars 695
24.4.1 Calendars 695
24 4 2 Time Zones 700
24.4,3 Gregori anCal endar and Si mpl eTi meZone 701
24.5 Formatting and Parsing Dates and Times 703
24.5.1 Using Formatter with Dates an Times 706
24.6 Internationalization and Localization for Text 708
24.6.1 Collation 708
24.6.2 Formatting and Parsing 710
24 6 3 Text Boundaries 712
25 Standard Packages 715
25.1 java.awt-The Abstract Window Toolkit 717
25.2 java.applet-Applets 720
25.3 java.beans-Components 721
25.4 java.math-Mathematics 722
25.5 java.net-The Network 724
25.6 java.rmi-Remote Method Invocation 727
25.7 java.security and Related Packages-Security Tools 732
25.8 java.sql-Relational Database Access 732
25.9 Utility Subpackages 733
25.9.1 Concurrency Utilities-Java.util.concurrent 733
25.9.2 Archive Files-java.util.jar 735
25.9.3 ZIP Files-java.uti1.zip 736
25.10 javax.* -Standard Extensions 737
25.11 javax.accessibility-Accessibility for GUIs 737
25.12 javax.naming-Directory and Naming Services 738
25.13 javax.sound-Sound Manipulation 739
25.14 javax.swing-Swing GUI Components 740
25.15 org.omg.CORBA-CORBA APIs 740
A Application Evolution 741
A.1 Language, Library, and Virtual Machine Versions 741
A 2 Dealing with Multiple Dialects 743
A.3 Generics: Reification, Erasure, and Raw Types 744
A.3.1 Raw Types, “Unchecked” Warnings, and Bridge Methods 745
A.3.2 API Issues 747
B Useful Tables 749
Further Reading 755
Index 761
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 Dont 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 {@link} and {@1inkplain} 484
19.2.3 @param 485
19.2.4 @return 485
19.2.5 @throws and @exception 485
19.2.6 @deprecated 486
19.2.7 @author 486
19.2.8 @version 487
19.2.9 @since 487
19.2.10 {@literal} and {@code} 487
19.2.11 {@value} 487
19.2.12 {@docRoot} 488
19.2.13 {@inheritDoc} 488
19.3 Inheriting Method Documentation Comments 489
19.3.1 Inheriting @throws Comments 490
19.4 A Simple Example 491
19.5 External Conventions 496
19.5.1 Package and Overview Documentation 496
19.5.2 The doc-files Directory 497
19.6 Notes on Usage 497
20 The I/O Package 499
20.1 Streams Overview 500
20.2 Byte Streams 501
20.2.1 InputStream 503
20.2.2 Outputstream 505
20.3 Character Streams 507
20.3.l Reader 508
20.3.2 Writer 510
20.3.3 Character Streams and the Standard Streams 511
20.4 InputStreamReader and OutputStreamWriter 512
20.5 A Quick Tour of the Stream Classes 514
20.5.1 Synchronization and Concurrency 515
20.5.2 Filter Streams 516
20.5.3 Buffered Streams 518
20.5.4 Piped Streams 520
20.5.5 ByteArray Byte Streams 521
20.5.6 CharArray Character Streams 522
20.5.7 String Character Streams 523
20.5.8 Print Streams 525
20.5.9 LineNumberReader 527
20.5.10 SequenceInputStream 528
20.5.11 Pushback Streams 529
20.5.12 StreamTokenizer 532
20.6 The Data Byte Streams 537
20.6.1 DataInput and DataOutput 537
20.6.2 The Data Stream Classes 539
20.7 Working with Files 540
20.7.1 Fi1e Streams and Fi1eDescriptor 540
20.7.2 RandomAccessFile 541
20.7.3 The File Class 543
20.7.4 FilenameFilter and FileFi1ter 548
20.8 Object Serialization 549
20.8.1 The Object Byte Streams 549
20.8.2 Making Your Classes Serializable 551
20.8.3 Serialization and Deserialization Order 552
20 8 4 Customized Serialization 554
20.8.5 Object Versioning. 555
20 8 6 Serialized Fields 559
20.8.7 The Externalizable Interface 561
20.8.8 Documentation Comment Tags 562
20.9 The IOException Classes 563
20.10 A Taste of New I/O 565
21 Collections 567
21.1 Collections 567
21.1.1 Exception Conventions 571
21.2 Iteration 571
21.3 Ordering with Comparable and Comparator 574
21.4 The Collection Interface 575
21.5 Set and SortedSet 577
21.5.1 HashSet 579
21.5.2 LinkedHashSet 580
21.5.3 TreeSet 580
21.6 List 580
21.6.1 ArrayList 582
21.6.2 LinkedList 583
21.6.3 RandomAccess Lists 584
21.7 Queue 585
21.7.1 PriorityQueue 586
21.8 Map and SortedMap 587
21.8.1 HashNap 590
21.8.2 LinkedHashMap 591
21.8.3 IdentityHashNap 592
21.8.4 WeakHashNap 592
21.8.5 TreeMap 593
21.9 enum Collections 594
21.9.1 EnumSet 594
21.9.2 EnumNap 596
21.10 Wrapped Collections and the Collections Class 597
21.10.1 The Collections Utilities 597
21.10.2 The Unmodifiable Wrappers 601
21.10.3 The Checked Wrappers 601
21.11 Synchronized Wrappers and Concurrent Collections 602
21.11.1 The Synchronized Wrappers 602
21.11.2 The Concurrent Collections 604
21.12 The Arrays Utility Class 607
21.13 Writing Iterator Implementations 609
21.14 Writing Collection Implementations 611
21.15 The Legacy Collection Types 616
21.15.1 Enumeration 617
21.15.2 Vector 617
21.15.3 Stack 619
21.15.4 Dictionary 619
21.15.5 Hashtable 619
21.16 Properties 620
22 Miscellaneous Utilities 623
22.1 Formatter 624
22.1.1 Format Specifiers 626
22.1.2 Integer Conversions 627
22.1.3 Floating-Point Conversions 627
22.1.4 Character Conversions 629
22.1.5 General Conversions 629
22.1.6 Custom Formatting 630
22.1.7 Format Exceptlons 630
22.1.8 The Formatter Class 631
22.2 BitSet 632
22.3 Observer/Observable 635
22.4 Random 639
22.5 Scanner 641
22.5.1 Stream of Values 641
22.5.2 Scanning Lines 644
22.5.3 Using Scanner 647
22.5.4 Localization 651
22.6 StringTokenizer 651
22.7 Timer and TimerTask 653
22.8 UUID 656
22.9 Math and StrictMath 657
23 System Programming 661
23.1 The System Class 662
23.1.1 Standard I/O Streams 662
23.1.2 System Properties 663
23.1.3 Utility Methods 665
23.2 Creating Processes 666
23.2.1 Process 667
23.2.2 Process Environments 669
23.2.3 ProcessBuilder 670
23.2.4 Portability 672
23.3 Shutdown 672
23.3.1 Shutdown Hooks 672
23.3.2 The Shutdown Sequence 674
23.3.3 Shutdown Strategies 674
23.4 The Rest of Runtime 675
23.4.1 Loading Native Code 676
23.4.2 Debugging 676
23.5 Security 677
23.5.1 The SecurityManager Class 678
23.5.2 Permissions 679
23 5 3 Security Policies 680
23.5.4 Access Controllers and Privileged Execution 681
24 Internationalization and Localization 685
24.1 Locale 686
24.2 Resource Bundles 688
24.2.1 ListResourceBundle 691
24.2.2 PropertyResourceBundle 692
24.2.3 Subclassing ResourceBundle 693
24.3 Currency 694
24.4 Time, Dates, and Calendars 695
24.4.1 Calendars 695
24 4 2 Time Zones 700
24.4,3 Gregori anCal endar and Si mpl eTi meZone 701
24.5 Formatting and Parsing Dates and Times 703
24.5.1 Using Formatter with Dates an Times 706
24.6 Internationalization and Localization for Text 708
24.6.1 Collation 708
24.6.2 Formatting and Parsing 710
24 6 3 Text Boundaries 712
25 Standard Packages 715
25.1 java.awt-The Abstract Window Toolkit 717
25.2 java.applet-Applets 720
25.3 java.beans-Components 721
25.4 java.math-Mathematics 722
25.5 java.net-The Network 724
25.6 java.rmi-Remote Method Invocation 727
25.7 java.security and Related Packages-Security Tools 732
25.8 java.sql-Relational Database Access 732
25.9 Utility Subpackages 733
25.9.1 Concurrency Utilities-Java.util.concurrent 733
25.9.2 Archive Files-java.util.jar 735
25.9.3 ZIP Files-java.uti1.zip 736
25.10 javax.* -Standard Extensions 737
25.11 javax.accessibility-Accessibility for GUIs 737
25.12 javax.naming-Directory and Naming Services 738
25.13 javax.sound-Sound Manipulation 739
25.14 javax.swing-Swing GUI Components 740
25.15 org.omg.CORBA-CORBA APIs 740
A Application Evolution 741
A.1 Language, Library, and Virtual Machine Versions 741
A 2 Dealing with Multiple Dialects 743
A.3 Generics: Reification, Erasure, and Raw Types 744
A.3.1 Raw Types, “Unchecked” Warnings, and Bridge Methods 745
A.3.2 API Issues 747
B Useful Tables 749
Further Reading 755
Index 761
目 录内容简介
本书主要讲授Java程序设计语言,系统地介绍Java的主要包(即java.lang.*、java.util和java.io)中的大多数类,并以内容丰富的示例对这些类如何工作进行了深入剖析。作者对这部经典著作进行更新,使其反映了Java 2标准版5.0(J2SE 5.0)中主要的增强。本版中增加了几章新内容,专门讨论泛型、枚举以及注解(这些是5.0版引入的主要的新特性),并且加了几小节论述断言和正则表达式。本书经过更新后还结合了当今构建健壮、高效和可维护的Java软件的最佳实践。
本书对所有程序员(包括那些经验丰富的程序员在内)都是不可或缺的参考书。
本书对所有程序员(包括那些经验丰富的程序员在内)都是不可或缺的参考书。
比价列表
公众号、微信群

微信公众号

实时获取购书优惠