® The Java Complete Eighth Edition Reference ™ About the Author Herbert Schildt is a leading authority on the Java, C++, C, and C# languages. His programming books have sold millions of copies worldwide and have been translated into all major foreign languages. He is the author of numerous books on Java, including Java: A Beginner’s Guide, Herb Schildt’s Java Programming Cookbook, Swing: A Beginner’s Guide, and The Art of Java. Among his other bestsellers are C++: The Complete Reference™, C#: The Complete Reference™, and C: The Complete Reference™. Although interested in all facets of computing, his primary focus is computer languages, including compilers, interpreters, and robotic control languages. He also has an active interest in the standardization of languages. 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. About the Technical Editor Dr. Danny Coward has been a contributor to the Java Platforms since 1997. He was a founding member of the Java EE group while at Sun. He has served as a member of the Java Community Process Executive Committee and has been a leading contributor to all editions of the Java Platform—Java SE, Java ME, and Java EE—and established the original JavaFX team. ® Th e Java Complete Eighth Edition Reference ™ Herbert Schildt New York Chicago San Francisco Lisbon London Madrid Mexico City Milan New Delhi San Juan Seoul Singapore Sydney Toronto Copyright © 2011 by The McGraw-Hill Companies. All rights reserved. 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-160631-8 MHID: 0-07-160631-9 The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-160630-1, MHID: 0-07-160630-0. 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 benefi t 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 [email protected]. Trademarks: McGraw-Hill, the McGraw-Hill Publishing logo, Complete Reference™, and related trade dress are trademarks or registered trademarks of The McGraw-Hill Companies and/or its affi liates in the United States and other countries and may not be used without written permission. All other trademarks are the property of their respective owners. The McGraw-Hill Companies is not associated with any product or vendor mentioned in this book. Information has been obtained by Publisher from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, Publisher, or others, Publisher does not guarantee to the accuracy, adequacy, or completeness of any information included in this work and is not responsible for any errors or omissions or the results obtained from the use of such information. Oracle Corporation does not make any representations or warranties as to the accuracy, adequacy, or completeness of any information contained in this Work, and is not responsible for any errors or omissions. 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 EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES 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. GET FREE SUBSCRIPTION YOUR TO ORACLE MAGAZINE Oracle Magazine is essential gear for today’s information technology professionals. Stay informed and increase your productivity with every issue of Oracle Magazine. Inside each free bimonthly issue you’ll get: (cid:116)(cid:1) (cid:1)(cid:54)(cid:81)(cid:14)(cid:85)(cid:80)(cid:14)(cid:69)(cid:66)(cid:85)(cid:70)(cid:1)(cid:74)(cid:79)(cid:71)(cid:80)(cid:83)(cid:78)(cid:66)(cid:85)(cid:74)(cid:80)(cid:79)(cid:1)(cid:80)(cid:79)(cid:1)(cid:48)(cid:83)(cid:66)(cid:68)(cid:77)(cid:70)(cid:1)(cid:37)(cid:66)(cid:85)(cid:66)(cid:67)(cid:66)(cid:84)(cid:70)(cid:13)(cid:1)(cid:48)(cid:83)(cid:66)(cid:68)(cid:77)(cid:70)(cid:1)(cid:34)(cid:81)(cid:81)(cid:77)(cid:74)(cid:68)(cid:66)(cid:85)(cid:74)(cid:80)(cid:79)(cid:1)(cid:52)(cid:70)(cid:83)(cid:87)(cid:70)(cid:83)(cid:13)(cid:1)(cid:1) (cid:56)(cid:70)(cid:67)(cid:1)(cid:69)(cid:70)(cid:87)(cid:70)(cid:77)(cid:80)(cid:81)(cid:78)(cid:70)(cid:79)(cid:85)(cid:13)(cid:1)(cid:70)(cid:79)(cid:85)(cid:70)(cid:83)(cid:81)(cid:83)(cid:74)(cid:84)(cid:70)(cid:1)(cid:72)(cid:83)(cid:74)(cid:69)(cid:1)(cid:68)(cid:80)(cid:78)(cid:81)(cid:86)(cid:85)(cid:74)(cid:79)(cid:72)(cid:13)(cid:1)(cid:69)(cid:66)(cid:85)(cid:66)(cid:67)(cid:66)(cid:84)(cid:70)(cid:1)(cid:85)(cid:70)(cid:68)(cid:73)(cid:79)(cid:80)(cid:77)(cid:80)(cid:72)(cid:90)(cid:13)(cid:1)(cid:1) (cid:66)(cid:79)(cid:69)(cid:1)(cid:67)(cid:86)(cid:84)(cid:74)(cid:79)(cid:70)(cid:84)(cid:84)(cid:1)(cid:85)(cid:83)(cid:70)(cid:79)(cid:69)(cid:84) (cid:116)(cid:1) (cid:53)(cid:73)(cid:74)(cid:83)(cid:69)(cid:14)(cid:81)(cid:66)(cid:83)(cid:85)(cid:90)(cid:1)(cid:79)(cid:70)(cid:88)(cid:84)(cid:1)(cid:66)(cid:79)(cid:69)(cid:1)(cid:66)(cid:79)(cid:79)(cid:80)(cid:86)(cid:79)(cid:68)(cid:70)(cid:78)(cid:70)(cid:79)(cid:85)(cid:84) (cid:116)(cid:1) (cid:1)(cid:53)(cid:70)(cid:68)(cid:73)(cid:79)(cid:74)(cid:68)(cid:66)(cid:77)(cid:1)(cid:66)(cid:83)(cid:85)(cid:74)(cid:68)(cid:77)(cid:70)(cid:84)(cid:1)(cid:80)(cid:79)(cid:1)(cid:48)(cid:83)(cid:66)(cid:68)(cid:77)(cid:70)(cid:1)(cid:66)(cid:79)(cid:69)(cid:1)(cid:81)(cid:66)(cid:83)(cid:85)(cid:79)(cid:70)(cid:83)(cid:1)(cid:81)(cid:83)(cid:80)(cid:69)(cid:86)(cid:68)(cid:85)(cid:84)(cid:13)(cid:1)(cid:85)(cid:70)(cid:68)(cid:73)(cid:79)(cid:80)(cid:77)(cid:80)(cid:72)(cid:74)(cid:70)(cid:84)(cid:13)(cid:1)(cid:1) (cid:66)(cid:79)(cid:69)(cid:1)(cid:80)(cid:81)(cid:70)(cid:83)(cid:66)(cid:85)(cid:74)(cid:79)(cid:72)(cid:1)(cid:70)(cid:79)(cid:87)(cid:74)(cid:83)(cid:80)(cid:79)(cid:78)(cid:70)(cid:79)(cid:85)(cid:84) (cid:116)(cid:1) (cid:37)(cid:70)(cid:87)(cid:70)(cid:77)(cid:80)(cid:81)(cid:78)(cid:70)(cid:79)(cid:85)(cid:1)(cid:66)(cid:79)(cid:69)(cid:1)(cid:66)(cid:69)(cid:78)(cid:74)(cid:79)(cid:74)(cid:84)(cid:85)(cid:83)(cid:66)(cid:85)(cid:74)(cid:80)(cid:79)(cid:1)(cid:85)(cid:74)(cid:81)(cid:84) (cid:116)(cid:1) (cid:51)(cid:70)(cid:66)(cid:77)(cid:14)(cid:88)(cid:80)(cid:83)(cid:77)(cid:69)(cid:1)(cid:68)(cid:86)(cid:84)(cid:85)(cid:80)(cid:78)(cid:70)(cid:83)(cid:1)(cid:84)(cid:85)(cid:80)(cid:83)(cid:74)(cid:70)(cid:84) If there are other Oracle users at your location who would like to Three easy ways to subscribe: receive their own subscription to Oracle Magazine, please photo- copy this form and pass it along. 1 Web (cid:55)(cid:74)(cid:84)(cid:74)(cid:85)(cid:1)(cid:80)(cid:86)(cid:83)(cid:1)(cid:56)(cid:70)(cid:67)(cid:1)(cid:84)(cid:74)(cid:85)(cid:70)(cid:1)(cid:66)(cid:85) oracle.com/oraclemagazine(cid:1) (cid:58)(cid:80)(cid:86)(cid:8)(cid:77)(cid:77)(cid:1)(cid:71)(cid:74)(cid:79)(cid:69)(cid:1)(cid:66)(cid:1)(cid:84)(cid:86)(cid:67)(cid:84)(cid:68)(cid:83)(cid:74)(cid:81)(cid:85)(cid:74)(cid:80)(cid:79)(cid:1)(cid:71)(cid:80)(cid:83)(cid:78)(cid:1)(cid:85)(cid:73)(cid:70)(cid:83)(cid:70)(cid:13)(cid:1)(cid:81)(cid:77)(cid:86)(cid:84)(cid:1)(cid:78)(cid:86)(cid:68)(cid:73)(cid:1)(cid:78)(cid:80)(cid:83)(cid:70) 2 Fax (cid:36)(cid:80)(cid:78)(cid:81)(cid:77)(cid:70)(cid:85)(cid:70)(cid:1)(cid:85)(cid:73)(cid:70)(cid:1)(cid:82)(cid:86)(cid:70)(cid:84)(cid:85)(cid:74)(cid:80)(cid:79)(cid:79)(cid:66)(cid:74)(cid:83)(cid:70)(cid:1)(cid:80)(cid:79)(cid:1)(cid:85)(cid:73)(cid:70)(cid:1)(cid:67)(cid:66)(cid:68)(cid:76)(cid:1)(cid:80)(cid:71)(cid:1)(cid:85)(cid:73)(cid:74)(cid:84)(cid:1)(cid:68)(cid:66)(cid:83)(cid:69)(cid:1)(cid:1) (cid:66)(cid:79)(cid:69)(cid:1)(cid:71)(cid:66)(cid:89)(cid:1)(cid:85)(cid:73)(cid:70)(cid:1)(cid:82)(cid:86)(cid:70)(cid:84)(cid:85)(cid:74)(cid:80)(cid:79)(cid:79)(cid:66)(cid:74)(cid:83)(cid:70)(cid:1)(cid:84)(cid:74)(cid:69)(cid:70)(cid:1)(cid:80)(cid:79)(cid:77)(cid:90)(cid:1)(cid:85)(cid:80)(cid:1)+1.847.763.9638 3 Mail (cid:36)(cid:80)(cid:78)(cid:81)(cid:77)(cid:70)(cid:85)(cid:70)(cid:1)(cid:85)(cid:73)(cid:70)(cid:1)(cid:82)(cid:86)(cid:70)(cid:84)(cid:85)(cid:74)(cid:80)(cid:79)(cid:79)(cid:66)(cid:74)(cid:83)(cid:70)(cid:1)(cid:80)(cid:79)(cid:1)(cid:85)(cid:73)(cid:70)(cid:1)(cid:67)(cid:66)(cid:68)(cid:76)(cid:1)(cid:80)(cid:71)(cid:1)(cid:85)(cid:73)(cid:74)(cid:84)(cid:1)(cid:68)(cid:66)(cid:83)(cid:69)(cid:1)(cid:1) (cid:66)(cid:79)(cid:69)(cid:1)(cid:78)(cid:66)(cid:74)(cid:77)(cid:1)(cid:74)(cid:85)(cid:1)(cid:85)(cid:80) P.O. Box 1263, Skokie, IL 60076-8263 Copyright © 2008, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Want your own FREE subscription? To receive a free subscription to Oracle Magazine, you must fill out the entire card, sign it, and date it (incomplete cards cannot be processed or acknowledged). You can also fax your application to +1.847.763.9638. Or subscribe at our Web site at oracle.com/oraclemagazine Yes, please send me a FREE subscription Oracle Magazine. No. From time to time, Oracle Publishing allows our partners x exclusive access to our e-mail addresses for special promo- tions and announcements. To be included in this program, please check this circle. If you do not wish to be included, you signature (required) date will only receive notices about your subscription via e-mail. name title Oracle Publishing allows sharing of our postal mailing list with selected third parties. If you prefer your mailing address not to company e-mail address be included in this program, please check this circle. ICf uastt oanmye tri mSee ryvoicue waot u+ld1. 8li4k7e. 7t6o3 b.9e6 3re5m oorv esden fdro man eeit-hmear iml taoil inogra lcilset@, phleaallsdea tcao.cnotamct. street/p.o. box If you opt in to the sharing of information, Oracle may also provide you with e-mail related to Oracle products, services, and events. If you want to completely unsubscribe from any e-mail communication from Oracle, please send an e-mail to: city/state/zip or postal code telephone [email protected] with the following in the subject line: REMOVE [your e-mail address]. For complete information on Oracle Publishing’s privacy practices, please visit oracle.com/html/privacy/html country fax Would you like to receive your free subscription in digital format instead of print if it becomes available? Yes No YOU MUST ANSWER ALL 10 QUESTIONS BELOW. 1 WHAT IS THE PRIMARY BUSINESS ACTIVITY 3 WHAT IS YOUR CURRENT PRIMARY OPERATING o 18 Minicomputer o 05 Hibernate o 17 SQL OF YOUR FIRM AT THIS LOCATION? (check PLATFORM (check all that apply) o 19 Intel x86(32) o 06 J++/J# o 18 Visual Basic oooooon e00000 o12354n lCAAAMyh)eupeerptdoomliismacipc aoaaattnciliosdevn eEa SnnMetdear Dvrntiueacffieaen ncmPtsrueeorn viMtnidgaenrufacturing oooooo 000000132546 IDIHLLBBiiiPnnMMg uuiUt xxUAaN l((NII SRXXEIUeXqdSu iEHp)mate)nt Corp UNIX/VAX/VMS SoooooE R22222V41302I CEWICSNnSoyetonmetrwslkm uxsole8ttratik6nr t(iCgic6oo 4nMm )Supelutrtivpeirrcoecsessing oooooo 000111120897 P.JPPJNLSaeHE/vPrTPSlaQ L o 98 Other o 06 Construction/Engineering o 07 Linux (Oracle Enterprise) o 25 Education/Training o 07 GCoonosdusmer Sector/Consumer Packaged oo 0089 LMinaucxin (tootshher) oo 2267 OMnaliinntee Dnaantacbease 10 WSITHEA?T ( OchReAcCk LaEl lP tRhOaDt UapCpTlSy )ARE IN USE AT YOUR o 08 Education o 10 MVS o 28 Support o 09 Financial Services/Insurance o 11 Netware o 29 Technology-Based Training ORACLE DATABASE o 10 Health Care o 12 Network Computing o 30 Other o 01 Oracle Database 11g o 11 High Technology Manufacturing, OEM o 13 SCO UNIX 99 o None of the Above o 02 Oracle Database 10g o 12 Industrial Manufacturing o 14 Sun Solaris/SunOS o 03 Oracle9i Database o 13 Independent Software Vendor o 15 Windows o 04 Oracle Embedded Database o 14 Life Sciences (biotech, pharmaceuticals) o 16 Other UNIX 6 WHAT IS YOUR COMPANY’S SIZE? (Oracle Lite, Times Ten, Berkeley DB) o 15 Natural Resources o 98 Other (check one only) o 05 Other Oracle Database Release o 16 Oil and Gas 99 o None of the Above o 01 More than 25,000 Employees ORACLE FUSION MIDDLEWARE oooooooo 2111222289741032 UPRSPRTTeryeeurtaoilssbtlevfaitelceteieiaicolslme/ r msSascWsi nheom (Idcenhnu tlaoTteonellr crieagS cts(narregaaistrcolitpveo,vo oi/ngercrD,saer tnViasssmA,tt irsoReianbn/nutVi)ttAiaoDtnion, water) 4 DOToooHRO E A000Y F132OUO UTLHAB HLEuapOOVsrpdRiWAlniwIcLeZIaaUsNEtrs AGieT oAT?Hn Ep E(D,pc PlheSivUcePaecREtklCCoi oIaHpFnlmAYls e S,t( hnEREat RE OtTPC Foa,O opCAMlpRsNlMMyY)E, ONeFtD c, . ) 7 DoooooU R00000IN35264G 1F151 Te,00,00Hw1,000E et10 1oNr 1 ttEt 1oohtX, oa051T n0,02 0 10,51002 ,0E 000M0m 00 E O0EpmEm lNmEopTpmylpoHlelopyoSeyleyos,e eeyHeseeOsseWs MUCH ooooooo 00001111207869 OOOOOOOrrrrrrraaaaaaacccccccllllllleeeeeee SDPIAEBdnOoPapetrtAEpnetaLl tar iS ipcHlPtuaryuriit obstiMeecso enaMs nSsaa engMraevagmenerea rngter o 98 Other Business and Services _________ o 04 Database Products DO YOU ANTICIPATE YOUR ORGANIZATION ORACLE DEVELOPMENT TOOLS o 05 Internet or Intranet Products WILL SPEND ON COMPUTER HARDWARE, o 13 Oracle JDeveloper 2 WY(cOhHUeIRcCk HP oROnIFeM ToAHnRElyY F) OJOLLBO FWUNINCGT IBOENS?T DESCRIBES 9oo9 00o76 NMOtoihdneder l eoSwfo tfahtrewe AaPrbreoodvuects SYoOO FU0TR1W LOALeCRsAEs,T tIPhOEaNRn? I$P (1Hc0hE,e0Rc0Ak0L oSn, eA oNnDl yS)ERVICES FOR oooo 11115674 OOOOrrrraaaacccclllleeee DRFDoeeisrpsmcioogrsvntseerrer CooO R00P12O R ECFAixEnTeOacE,n u CcMtFeivO/AeA,N dOMAmwaGinnnEaeiMsgrt,e ErPmaNateTirvnt/entS e(MTrP,A arPeFnrsFaiingdceeimnptae,l n)Cth air, 5 IC(NcHh AYeOSckEU RaA lNlJ OtYhB aO, tFD aTOpH pYElOy F)UO ULLSOE WORIN PGL PARNO TDOU CPTUSR?- ooooo 0000023546 $$$$$1115500,000,0,0000,,0000000,000 0 0t0t o ot0t o o$ $ a $4$9n4999d,9,9 99O9999v,,9999e9r999 oooo 11228910 OOOOrrrraaaacccclllleeee BWAWIp aeBprbleeCiacheanonstutiseoern BEuxiplrdeesrs (VP/Director/ Manager/Controller, SOFTWARE ORACLE APPLICATIONS Purchasing, Administration) o 01 CAD/CAE/CAM o 22 Oracle E-Business Suite o 03 Sales/Marketing Management o 02 Collaboration Software 8 WHAT IS YOUR COMPANY’S YEARLY SALES o 23 PeopleSoft Enterprise (VP/Director/Manager) o 03 Communications REVENUE? (check one only) o 24 JD Edwards EnterpriseOne o 04 Computer Systems/Operations o 04 Database Management o 25 JD Edwards World Management o 05 File Management o 01 $500, 000, 000 and above o 26 Oracle Fusion (CIO/VP/Director/Manager MIS/IS/IT, Ops) o 06 Finance o 02 $100, 000, 000 to $500, 000, 000 o 27 Hyperion IS/IT STAFF o 07 Java o 03 $50, 000, 000 to $100, 000, 000 o 28 Siebel CRM 08014004 oooooooo 0000011910895876 CMSTEOA AODedotBttppaahhcunppAnfheecsllfa/niiarrucc SgitlTaactieyeoiattmsnciinlootg he/SennnTnmu iDDrtpcaseeap iAvvnlo eeiMrdntllm ogoaDppininmmraiegseectenntrmtota//rte/PPonMrrrtoo/aggSnrrtaaaammgfefmmriinngg HooooooooooA R0011111111D1320567489W MSPWMMPSNMAycrreaauaRoooisteilscgjrwEtnteknsierinfcmiafotmrvtlitmraofo eeskMimwsdmlc iyMhn iea aai gnPna nAgaandgrua aEegtnlhmelgoemeirln inePnetngretorcinegssing 9 WYooooooOH U000000A 1U54234T S L$$ACCCEA 1?+5#jaN ,, ( +x0G0c 0h0 U0e0A,c, Gk00 E00aS00l l AtttoohN oooo$a$D5t5 F,0a R11110p, 35640Ap0 0l0My,0 )PSRSE0, ptuyW00rrbt0u0ihOynt0o/sgRnRK aSil sDO O9ooooooo9R A 223339o3C1298830L E ONOOOOOS SirrrrrtoeaaaaahEnbccccceReelllllrV eeeeelo ICfTCEESC eR-dotuEhBcunMpSehcusp nausoOAoitlrnntbitloi oeonDngsvgesyem SOaunni tdDee Omna nDdemand Contents at a Glance Part I The Java Language 1 The History and Evolution of Java 3 2 An Overview of Java 17 3 Data Types, Variables, and Arrays 35 4 Operators 61 5 Control Statements 81 6 Introducing Classes 109 7 A Closer Look at Methods and Classes 129 8 Inheritance 161 9 Packages and Interfaces 187 10 Exception Handling 207 11 Multithreaded Programming 227 12 Enumerations, Autoboxing, and Annotations (Metadata) 259 13 I/O, Applets, and Other Topics 289 14 Generics 325 Part II The Java Library 15 String Handling 371 16 Exploring java.lang 397 17 java.util Part 1: The Collections Framework 453 18 java.util Part 2: More Utility Classes 525 19 Input/Output: Exploring java.io 581 20 Exploring NIO 629 21 Networking 667 22 The Applet Class 687 23 Event Handling 707 24 Introducing the AWT: Working with Windows, Graphics, and Text 735 25 Using AWT Controls, Layout Managers, and Menus 773 26 Images 829 27 The Concurrency Utilities 861 28 Regular Expressions and Other Packages 909 v