Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter Blind Folio FM:ii P:\010Comp\Begin8\189-0\fm.vp Tuesday, March 01, 2005 10:22:50 AM Color profile: Generic CMYK printer profile Composite Default screen TEAM LinG Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter Blind Folio FM:i Java ™ : A Beginner’s Guide, Third Edition P:\010Comp\Begin8\189-0\fm.vp Tuesday, March 01, 2005 10:22:50 AM Color profile: Generic CMYK printer profile Composite Default screen TEAM LinG Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter Blind Folio FM:ii P:\010Comp\Begin8\189-0\fm.vp Tuesday, March 01, 2005 10:22:50 AM Color profile: Generic CMYK printer profile Composite Default screen This page intentionally left blank. TEAM LinG Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter Blind Folio FM:iii Java ™ : A Beginner’s Guide, Third Edition Herbert Schildt McGraw-Hill/Osborne New York Chicago San Francisco Lisbon London Madrid Mexico City Milan New Delhi San Juan Seoul Singapore Sydney Toronto P:\010Comp\Begin8\189-0\fm.vp Tuesday, March 01, 2005 10:22:50 AM Color profile: Generic CMYK printer profile Composite Default screen TEAM LinG Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter Blind Folio FM:iv P:\010Comp\Begin8\189-0\fm.vp Tuesday, March 01, 2005 10:22:50 AM Color profile: Generic CMYK printer profile Composite Default screen Copyright © 2005 by The McGraw-Hill Companies. All rights reserved. Manufactured in the United States of America. Except as permitted under the United States Copyright Act of 976, 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. 0-07-146650-9 The material in this eBook also appears in the print version of this title: 0-07-2231890-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 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. For more information, please contact George Hoare, Special Sales, at george_hoare@mcgraw-hill.com or (212) 904-4069. TERMS OF USE This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) 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. DOI: 10.1036/0071466509 TEAM LinG Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter Blind Folio FM:ii P:\010Comp\Begin8\189-0\fm.vp Tuesday, March 01, 2005 10:22:50 AM Color profile: Generic CMYK printer profile Composite Default screen Want to learn more? We hope you enjoy this McGraw-Hill eBook! If you’d like more information about this book, its author, or related books and websites, please click here. TEAM LinG About the Author Herbert Schildt is the world’s leading programming author. He is an authority on the C, C++, Java, and C# languages, and he is a master Windows programmer. His programming books have sold more than 3 million copies worldwide and have been translated into all major languages. He is the author of numerous bestsellers, including Java: The Complete Reference, C++: The Complete Reference, C: The Complete Reference, and C#: The Complete Reference, and he is the co-author of The Art of Java. 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. Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter Blind Folio FM:v P:\010Comp\Begin8\189-0\fm.vp Tuesday, March 01, 2005 10:22:50 AM Color profile: Generic CMYK printer profile Composite Default screen Copyright © 2005 The McGraw-Hill Companies. Click here for terms of use. TEAM LinG Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / 1 Blind Folio FM:vii Contents at a Glance 1 Java Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2 Introducing Data Types and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 3 Program Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 4 Introducing Classes, Objects, and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 5 More Data Types and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 6 A Closer Look at Methods and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 7 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 8 Packages and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 9 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 10 Using I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 11 Multithreaded Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407 12 Enumerations, Autoboxing, and Static Import . . . . . . . . . . . . . . . . . . . . . . . . . . 447 13 Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481 14 Applets, Events, and Miscellaneous Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525 vii P:\010Comp\Begin8\189-0\fm.vp Tuesday, March 01, 2005 10:22:50 AM Color profile: Generic CMYK printer profile Composite Default screen TEAM LinG Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter Blind Folio FM:viii A Answers to Mastery Checks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557 B Using Java’s Documentation Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613 viii Java: A Beginner’s Guide P:\010Comp\Begin8\189-0\fm.vp Tuesday, March 08, 2005 9:02:59 AM Color profile: Generic CMYK printer profile Composite Default screen TEAM LinG Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / 1 Blind Folio FM:ix ix Contents PREFACE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix 1 Java Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 The Origins of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 How Java Relates to C and C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 How Java Relates to C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Java’s Contribution to the Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Java Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Java’s Magic: The Bytecode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 The Java Buzzwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Obtaining the Java Development Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 A First Simple Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Entering the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Compiling the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 The First Sample Program Line by Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 P:\010Comp\Begin8\189-0\fm.vp Tuesday, March 01, 2005 10:22:50 AM Color profile: Generic CMYK printer profile Composite Default screen For more information about this title, click here TEAM LinG [...]... what sort of operations can be applied to that data To support the principles of object-oriented programming, all OOP languages, including Java, have three traits in common: encapsulation, polymorphism, and inheritance Let’s examine each 9 1 Java Fundamentals Java: A Beginner’s Guide Encapsulation Encapsulation is a programming mechanism that binds together code and the data it manipulates, and that... system.) Java answers these concerns by providing a firewall between a networked application and your computer TEAM LinG 6 Module 1: Java Fundamentals When using a Java- compatible web browser, it is possible to safely download Java applets without fear of viral infection The way that Java achieves this is by confining a Java program to the Java execution environment and not allowing it access to other parts... focus of Java to switch from consumer electronics to Internet programming So, while it was the desire for an architecture-neutral programming language that provided the initial spark, it was the Internet that ultimately led to Java s large-scale success 3 1 Java Fundamentals Java: A Beginner’s Guide How Java Relates to C and C++ Java is directly related to both C and C++ Java inherits its syntax from... preceding upgrades to Java have been, none compares in scale, size, and scope to that of J2SE 5 It has fundamentally reshaped the Java world! TEAM LinG Preface xxi J2SE 5: The Second Java Revolution Java 2 Platform Standard Edition, version 5 (J2SE 5) marks the beginning of the second Java revolution J2SE 5 adds many new features to Java that fundamentally change the character of the language, increasing both... incorporate new features, Java has continually been at the forefront of computer language design One reason for this is the culture of innovation and change that came to surround Java As a result, Java has gone through several upgrades—some relatively small, others more significant The first major update to Java was version 1.1 The features added by Java 1.1 were more substantial than the small increase... the areas of security and portability Prior to Java, cyberspace was effectively closed to half of the entities that now live there As you will see, Java addresses those concerns and, in doing so, has defined a new form of program: the applet Java Fundamentals CRITICAL SKILL 5 Java Applets An applet is a special kind of Java program that is designed to be transmitted over the Internet and automatically... number would have you think For example, Java 1.1 added many new library elements, redefined the way events are handled, and reconfigured many features of the original 1.0 library The next major release of Java was Java 2, where the 2 indicates “second generation.” The creation of Java 2 was a watershed event, marking the beginning of Java s “modern age.” The first release of Java 2 carried the version... of Java are portability and security, other factors played an important role in molding the final form of the language The key considerations were summed up by the Java design team in the following list of buzzwords Simple Java has a concise, cohesive set of features that makes it easy to learn and use Secure Java provides a secure means of creating Internet applications Portable Java programs can... co-exist for a long time to come, so will C# and Java Progress Check 1 Java is useful for the Internet because it can produce _ programs 2 Java is the direct descendent of what languages? 1 portable 2 C and C++ TEAM LinG Java: A Beginner’s Guide 1.2 1 Java s Contribution to the Internet The Internet helped catapult Java to the forefront of programming, and Java, in turn, has had a profound effect... that makes it possible for one object to be a specific instance of a more general case 11 1 Java Fundamentals Java: A Beginner’s Guide Progress Check 1 Name the principles of OOP 2 What is the basic unit of encapsulation in Java? Ask the Expert Q: You state that object-oriented programming is an effective way to manage large programs However, it seems that it might add substantial overhead to relatively . screen TEAM LinG Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter Blind Folio FM:xvi xvi Java: A Beginner’s Guide Metadata . . . . . . profile Composite Default screen TEAM LinG Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter Blind Folio FM:xii xii Java: A Beginner’s Guide The