Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Java the complete reference

710 views

Published on

the whole book java the complete reference

Published in: Education
  • Be the first to comment

Java the complete reference

  1. 1. Java™ : The Complete Reference, Seventh Edition
  2. 2. About the Author Herbert Schildt is a leading authority on the Java, C, C++, and C# languages, and is a master Windows programmer. His programming books have sold more than 3.5 million copies worldwide and have been translated into all major foreign languages. He is the author of the best-selling The Art of Java, Java: A Beginner’s Guide, and Swing: A Beginner’s Guide. Among his other bestsellers are C++: The Complete Reference, C++: A Beginner’s Guide, C#: The Complete Reference, and C#: A Beginner’s Guide. Schildt holds both graduate and undergraduate degrees from the University of Illinois. He can be reached at his consulting office at (217) 586-4683. His Web site is www.HerbSchildt.com.
  3. 3. Java™ : The Complete Reference, Seventh Edition Herbert Schildt New York Chicago San Francisco Lisbon London Madrid Mexico City Milan New Delhi San Juan Seoul Singapore Sydney Toronto
  4. 4. Copyright © 2007 by The McGraw-Hill Companies. All rights reserved. Except as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher. ISBN: 978-0-07-163177-8 MHID: 0-07-163177-1 The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-226385-5, MHID: 0-07-226385-7. All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where such designations appear in this book, they have been printed with initial caps. McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training programs. To contact a representative please e-mail us at bulksales@mcgraw-hill.com. Information has been obtained by McGraw-Hill from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, McGraw-Hill, or others, McGraw-Hill does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from the use of such information. TERMS OF USE This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGrawHill”) and its licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent. You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these terms. THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLYDISCLAIMANYWARRANTY, EXPRESS OR IMPLIED, INCLUDING BUTNOTLIMITEDTO IMPLIEDWARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise.
  5. 5. Contents at a Glance Part I The Java Language 1 The History and Evolution of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2 An Overview of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 3 Data Types, Variables, and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 4 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 5 Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 6 Introducing Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 7 A Closer Look at Methods and Classes . . . . . . . . . . . . . . . . . . . . . . . . 125 8 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 9 Packages and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 10 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 11 Multithreaded Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 12 Enumerations, Autoboxing, and Annotations (Metadata) . . . . . . . . . 255 13 I/O, Applets, and Other Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 14 Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 Part II The Java Library 15 String Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 16 Exploring java.lang . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385 17 java.util Part 1: The Collections Framework . . . . . . . . . . . . . . . . . . . . 437 18 java.util Part 2: More Utility Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 503 19 Input/Output: Exploring java.io . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555 20 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599 21 The Applet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617 22 Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637 23 Introducing the AWT: Working with Windows, Graphics, and Text . . 663 24 Using AWT Controls, Layout Managers, and Menus . . . . . . . . . . . . 701 25 Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755 26 The Concurrency Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787 27 NIO, Regular Expressions, and Other Packages . . . . . . . . . . . . . . . . . 813 v
  6. 6. Part III Software Development Using Java 28 Java Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847 29 Introducing Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 859 30 Exploring Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 879 31 Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 907 Part IV Applying Java 32 Financial Applets and Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931 33 Creating a Download Manager in Java . . . . . . . . . . . . . . . . . . . . . . . . . 965 A Using Java’s Documentation Comments . . . . . . . . . . . . . . . . . . . . . . . 991 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 997 vi J a v a : T h e C o m p l e t e R e f e r e n c e
  7. 7. Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix Part I The Java Language 1 The History and Evolution of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Java’s Lineage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The Birth of Modern Programming: C . . . . . . . . . . . . . . . . . . . 4 C++: The Next Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 The Stage Is Set for Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 The Creation of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 The C# Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 How Java Changed the Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Java Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Java’s Magic: The Bytecode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Servlets: Java on the Server Side . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 The Java Buzzwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Simple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Object-Oriented . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Robust . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Multithreaded . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Architecture-Neutral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Interpreted and High Performance . . . . . . . . . . . . . . . . . . . . . . 12 Distributed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Dynamic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 The Evolution of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Java SE 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 A Culture of Innovation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2 An Overview of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Two Paradigms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 The Three OOP Principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 A First Simple Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Entering the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Compiling the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 A Closer Look at the First Sample Program . . . . . . . . . . . . . . . 22 vii
  8. 8. A Second Short Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Two Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Using Blocks of Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Lexical Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Separators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 The Java Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 The Java Class Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3 Data Types, Variables, and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Java Is a Strongly Typed Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 The Primitive Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 byte . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 short . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 long . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Floating-Point Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 float . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 double . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 A Closer Look at Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Integer Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Floating-Point Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Boolean Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Character Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Declaring a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Dynamic Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 The Scope and Lifetime of Variables . . . . . . . . . . . . . . . . . . . . . 42 Type Conversion and Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Java’s Automatic Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Casting Incompatible Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Automatic Type Promotion in Expressions . . . . . . . . . . . . . . . . . . . . . 47 The Type Promotion Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Alternative Array Declaration Syntax . . . . . . . . . . . . . . . . . . . . 55 viii J a v a : T h e C o m p l e t e R e f e r e n c e
  9. 9. A Few Words About Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 A Note to C/C++ Programmers About Pointers . . . . . . . . . . . . . . . . . 56 4 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 The Basic Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . 58 The Modulus Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Arithmetic Compound Assignment Operators . . . . . . . . . . . . 59 Increment and Decrement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 The Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 The Bitwise Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . 63 The Left Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 The Right Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 The Unsigned Right Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Bitwise Operator Compound Assignments . . . . . . . . . . . . . . . 69 Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Boolean Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Short-Circuit Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . 72 The Assignment Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 The ? Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Using Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 5 Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Java’s Selection Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .