A hands-on guide to the Java programming language, The Java(TM) Tutorial, Fourth Edition is perfect for any developer looking for a proven path to proficiency with Java SE. This popular tutorial "from the Source" has been completely revised and updated to cover Version 6 of the Java Platform, Standard Edition. Written by members of the Java Software team at Sun Microsystems, this book uses a tested, interactive approach and features real-world problems that help you learn the Java platform by example. New to this edition are chapters on generics, collections, Java Web Start, the platform environment, and regular expressions. Key sections, including the Threads, I/O, Object-Oriented Programming Concepts, and Language Basics chapters have been completely rewritten to reflect reader feedback and to cover new features added to the Java SE 6 platform. A new appendix contains information on how to prepare for the Java Programming Language Certification exam. As with the previous editions, you will find clear explanations of the fundamentals of objects, classes, and data structures, as well as detailed coverage of exceptions, I/O, and threads. All of the popular features that made this book a classic have been retained, including convenient summaries at the end of each section and Questions and Exercises segments to help you practice what you learn. The accompanying CD-ROM is filled with valuable resources including the latest Java SE software (the JRE, JDK, Java API spec, and the guide documentation), the code samples from this book, and solutions to the questions and exercises. The Java(TM) Series is supported, endorsed, and authored by the creators of the Java technology at Sun Microsystems, Inc. It is the official place to go for complete, expert, and definitive information on Java technology. The books in this series provide the inside information you need to build effective, robust, and portable applications and applets. The Series is an indispensable resource for anyone targeting the Java(TM) platform. Cover......Page 1 Contents......Page 8 Foreword......Page 20 Preface......Page 22 About the Java Technology......Page 28 What Can Java Technology Do?......Page 31 How Will Java Technology Change My Life?......Page 32 "Hello World!" for the NetBeans IDE......Page 33 "Hello World!" for Microsoft Windows......Page 41 "Hello World!" for Solaris OS and Linux......Page 46 Source Code Comments......Page 51 The main Method......Page 52 Compiler Problems......Page 54 Runtime Problems......Page 56 Questions and Exercises: Getting Started......Page 57 What Is an Object?......Page 60 What Is a Class?......Page 62 What Is Inheritance?......Page 64 What Is an Interface?......Page 65 What Is a Package?......Page 66 Questions and Exercises: Object-Oriented Programming Concepts......Page 67 Variables......Page 70 Naming......Page 71 Primitive Data Types......Page 72 Arrays......Page 76 Summary of Variables......Page 80 Questions and Exercises: Variables......Page 81 Operators......Page 82 Assignment, Arithmetic, and Unary Operators......Page 83 Equality, Relational, and Conditional Operators......Page 86 Bitwise and Bit Shift Operators......Page 89 Summary of Operators......Page 90 Questions and Exercises: Operators......Page 91 Expressions......Page 93 Statements......Page 94 Questions and Exercises: Expressions, Statements, and Blocks......Page 95 The if-then and if-then-else Statements......Page 96 The switch Statement......Page 98 The while and do-while Statements......Page 101 The for Statement......Page 102 Branching Statements......Page 104 Questions and Exercises: Control Flow Statements......Page 109 Classes......Page 112 Declaring Classes......Page 114 Declaring Member Variables......Page 115 Defining Methods......Page 117 Providing Constructors for Your Classes......Page 119 Passing Information to a Method or a Constructor......Page 120 Objects......Page 124 Creating Objects......Page 126 Using Objects......Page 130 Returning a Value from a Method......Page 133 Using the this Keyword......Page 136 Controlling Access to Members of a Class......Page 137 Understanding Instance and Class Members......Page 139 Initializing Fields......Page 144 Summary of Creating and Using Classes and Objects......Page 145 Questions and Exercises: Classes......Page 146 Questions and Exercises: Objects......Page 148 Why Use Nested Classes?......Page 149 Inner Classes......Page 150 Inner Class Example......Page 151 Summary of Nested Classes......Page 154 Enum Types......Page 155 Annotations......Page 159 Documentation......Page 160 Annotations Used by the Compiler......Page 161 Questions and Exercises: Annotations......Page 163 Interfaces......Page 166 Interfaces in Java......Page 167 Interfaces and Multiple Inheritance......Page 168 Defining an Interface......Page 169 Implementing an Interface......Page 170 Using an Interface as a Type......Page 172 Summary of Interfaces......Page 173 Inheritance......Page 174 An Example of Inheritance......Page 175 What You Can Do in a Subclass......Page 177 Casting Objects......Page 178 Overriding and Hiding Methods......Page 179 Using the Keyword super......Page 181 Object as a Superclass......Page 183 Writing Final Classes and Methods......Page 187 Abstract Methods and Classes......Page 188 Questions and Exercises: Inheritance......Page 191 Introduction......Page 194 A Simple Box Class......Page 195 Generic Types......Page 196 Type Parameter Conventions......Page 198 Generic Methods and Constructors......Page 199 Bounded Type Parameters......Page 200 Subtyping......Page 202 Wildcards......Page 204 Type Erasure......Page 205 Summary of Generics......Page 206 Questions and Exercises: Generics......Page 207 Creating and Using Packages......Page 210 Creating a Package......Page 211 Naming a Package......Page 213 Using Package Members......Page 214 Managing Source and Class Files......Page 218 Summary of Creating and Using Packages......Page 220 Questions and Exercises: Creating and Using Packages......Page 221 The Numbers Classes......Page 222 Formatting Numeric Print Output......Page 225 Beyond Basic Arithmetic......Page 230 Summary of Numbers......Page 235 Questions and Exercises: Numbers......Page 236 Characters......Page 237 Strings......Page 239 String Length......Page 240 Concatenating Strings......Page 241 Creating Format Strings......Page 242 Converting Between Numbers and Strings......Page 243 Manipulating Characters in a String......Page 245 Comparing Strings and Portions of Strings......Page 251 The StringBuilder Class......Page 253 Summary of Characters and Strings......Page 257 Questions and Exercises: Characters and Strings......Page 258 What Is an Exception?......Page 260 The Three Kinds of Exceptions......Page 262 Catching and Handling Exceptions......Page 263 The try Block......Page 265 The catch Blocks......Page 266 The finally Block......Page 267 Putting It All Together......Page 268 Specifying the Exceptions Thrown by a Method......Page 272 How to Throw Exceptions......Page 273 Throwable Class and Its Subclasses......Page 274 Exception Class......Page 275 Chained Exceptions......Page 276 Creating Exception Classes......Page 277 Unchecked Exceptions—The Controversy......Page 279 Advantage 1: Separating Error-Handling Code from "Regular" Code......Page 280 Advantage 2: Propagating Errors Up the Call Stack......Page 282 Advantage 3: Grouping and Differentiating Error Types......Page 284 Summary......Page 285 Questions and Exercises: Exceptions......Page 286 I/O Streams......Page 288 Byte Streams......Page 290 Character Streams......Page 292 Buffered Streams......Page 296 Scanning and Formatting......Page 297 I/O from the Command Line......Page 303 Data Streams......Page 306 Object Streams......Page 309 File Objects......Page 313 Random Access Files......Page 316 The New I/O Packages......Page 318 Questions and Exercises: Basic I/O......Page 319 Introduction to Collections......Page 320 Benefits of the Java Collections Framework......Page 321 Interfaces......Page 322 The Collection Interface......Page 325 The Set Interface......Page 328 The List Interface......Page 333 The Queue Interface......Page 343 The Map Interface......Page 346 Object Ordering......Page 355 The SortedSet Interface......Page 362 The SortedMap Interface......Page 365 Questions and Exercises: Interfaces......Page 367 Implementations......Page 369 Set Implementations......Page 371 List Implementations......Page 373 Map Implementations......Page 374 Queue Implementations......Page 376 Wrapper Implementations......Page 377 Convenience Implementations......Page 379 Summary of Implementations......Page 381 Algorithms......Page 382 Sorting......Page 383 Shuffling......Page 385 Searching......Page 386 Reasons to Write an Implementation......Page 387 How to Write a Custom Implementation......Page 388 Compatibility......Page 391 API Design......Page 393 Processes and Threads......Page 396 Threads......Page 397 Defining and Starting a Thread......Page 398 Pausing Execution with sleep......Page 399 Interrupts......Page 400 The SimpleThreads Example......Page 402 Thread Interference......Page 404 Memory Consistency Errors......Page 406 Synchronized Methods......Page 407 Intrinsic Locks and Synchronization......Page 408 Atomic Access......Page 410 Deadlock......Page 411 Starvation and Livelock......Page 412 Guarded Blocks......Page 413 A Synchronized Class Example......Page 418 A Strategy for Defining Immutable Objects......Page 420 Lock Objects......Page 422 Executors......Page 424 Concurrent Collections......Page 427 Atomic Variables......Page 428 For Further Reading......Page 429 Questions and Exercises: Concurrency......Page 430 What Are Regular Expressions?......Page 432 Test Harness......Page 433 String Literals......Page 434 Metacharacters......Page 435 Character Classes......Page 436 Simple Classes......Page 437 Predefined Character Classes......Page 441 Quantifiers......Page 443 Zero-Length Matches......Page 444 Capturing Groups and Character Classes with Quantifiers......Page 447 Differences among Greedy, Reluctant, and Possessive Quantifiers......Page 448 Numbering......Page 449 Backreferences......Page 450 Boundary Matchers......Page 451 Methods of the Pattern Class......Page 452 Creating a Pattern with Flags......Page 453 Embedded Flag Expressions......Page 454 Using the split(String) Method......Page 455 Pattern Method Equivalents in java.lang.String......Page 457 Study Methods......Page 458 Using the start and end Methods......Page 459 Using replaceFirst(String) and replaceAll(String)......Page 461 Matcher Method Equivalents in java.lang.String......Page 463 Methods of the PatternSyntaxException Class......Page 464 Summary......Page 466 Questions and Exercises: Regular Expressions......Page 467 Properties......Page 470 Command-Line Arguments......Page 475 Environment Variables......Page 476 Other Configuration Utilities......Page 478 System Properties......Page 479 The Security Manager......Page 482 PATH and CLASSPATH......Page 484 Update the PATH Variable (Solaris and Linux)......Page 485 Questions and Exercises: The Platform Environment......Page 487 What Is Swing?......Page 490 A Swing Demo......Page 493 A Visual Guide to Swing Components......Page 497 Pluggable Look and Feel......Page 503 Internationalization and Localization......Page 507 Accessibility......Page 509 Integrating with the Desktop......Page 510 System Tray Icon Support......Page 511 Questions: Graphical User Interfaces......Page 512 Chapter 16 Packaging Programs in JAR Files......Page 514 Using JAR Files: The Basics......Page 515 Creating a JAR File......Page 517 Viewing the Contents of a JAR File......Page 520 Extracting the Contents of a JAR File......Page 522 Updating a JAR File......Page 523 Running JAR-Packaged Software......Page 525 Understanding the Default Manifest......Page 527 Modifying a Manifest File......Page 528 Setting an Application's Entry Point......Page 529 Adding Classes to the JAR File's Classpath......Page 530 Setting Package Version Information......Page 531 Sealing Packages Within a JAR File......Page 533 Signing and Verifying JAR Files......Page 534 Understanding Signing and Verification......Page 535 Signing JAR Files......Page 538 Verifying Signed JAR Files......Page 540 An Example—The JarRunner Application......Page 541 The JarClassLoader Class......Page 542 The JarRunner Class......Page 545 Questions: JAR Files......Page 547 Chapter 17 Java Web Start......Page 548 Running Java Web Start Applications......Page 549 Running an Application from the Java Cache Viewer......Page 550 Deploying Java Web Start Applications......Page 551 Creating the JNLP File......Page 552 Placing the Application on the Web Server......Page 560 Packaging the Application in JAR Files......Page 561 Untrusted Applications......Page 562 The JNLP API......Page 563 Security and JNLP Files......Page 565 Common Java Web Start Problems......Page 566 Questions and Exercises: Java Web Start......Page 567 Chapter 18 Applets......Page 570 Getting Started with Applets......Page 572 Extending Applet or JApplet......Page 573 The Life Cycle of an Applet......Page 574 Methods for Milestones......Page 577 Methods for Drawing and Event Handling......Page 579 Methods for Adding UI Components......Page 581 What Applets Can and Can't Do......Page 585 Taking Advantage of the Applet API......Page 586 Finding and Loading Data Files......Page 587 Displaying Short Status Strings......Page 588 Displaying Documents in the Browser......Page 589 Sending Messages to Other Applets......Page 591 Playing Sounds......Page 595 Defining and Using Applet Parameters......Page 597 Using the APPLET Tag......Page 601 Security Restrictions......Page 605 Creating a User Interface......Page 608 Getting System Properties......Page 610 Threads in Applets......Page 612 Working with a Server-Side Application......Page 616 Before You Ship That Applet......Page 620 Deploying Applets......Page 621 Using the applet Tag......Page 622 Using the object Tag......Page 623 Deploying Applets in a Mixed-Browser Environment......Page 625 Solving Common Applet Problems......Page 627 Questions and Exercises: Java Applets......Page 629 Appendix A: Java Language Keywords......Page 630 Appendix B: Preparation for Java Programming Language Certification......Page 632 Index......Page 644 A......Page 645 C......Page 647 E......Page 650 F......Page 651 G......Page 652 I......Page 653 J......Page 654 L......Page 655 M......Page 656 N......Page 657 P......Page 658 R......Page 659 S......Page 660 U......Page 663 Z......Page 664 Written by members of the Java Software team at Sun Microsystems, this book is a hands-on guide to the Java programming language aimed at any developer looking for a proven path to proficiency with Java SE. It uses a tested, interactive approach and features real-world problems that help you learn the Java platform by example. 本书除了讲解面向对象的编程概念, 语言基础, 类和对象, 接口和继承, 包, 数字和字符串, 异常, 基本I/O, 集合, 并发, Swing, 把程序打包到JAR文件中, applet之外, 还介绍了Java Web Start, 平台环境和正则表达式, 同时涵盖了Java 5.0和6.0引入的新特性, 比如泛型和并发, 注解