THE Java™ Programming Language, Fourth Edition
THE Java™ Programming Language, Fourth Edition By Ken Arnold, James Gosling, David Holmes ............................................... Publisher: Addison Wesley Professional Pub Date: August 17, 2005 ISBN: 0-321-34980-6 Pages: 928
Table of Contents | Index
Direct from the creators of the Java™ programming language, the completely revised fourth edition of The Java™ Programming Language is an indispensable resource for novice and advanced programmers alike. Developers around the world have used previous editions to quickly gain a deep understanding of the Java programming language, its design goals, and how to use it most effectively in real-world development. Now, Ken Arnold, James Gosling, and David Holmes have updated this classic to reflect the major enhancements in Java™ 2 Standard Edition 5.0 (J2SE™ 5.0). The authors systematically cover most classes in Java's main packages, java.lang.*, java.util, and java.io, presenting in-depth explanations of why these classes work as they do, with informative examples. Several new chapters and major sections have been added, and every chapter has been updated to reflect today's best practices for building robust, efficient, and maintainable Java software. Key changes in this edition include
●
New chapters on generics, enums, and annotations, the most powerful new language features introduced in J2SE 5.0
●
Changes to classes and methods throughout to reflect the addition of generics
●
Major new sections on assertions and regular expressions
●
Coverage of all the new language features, from autoboxing and variable argument methods to the enhanced for-loop and covariant return types
●
Coverage of key new classes, such as Formatter and Scanner
The Java™ Programming Language, Fourth Edition, is the definitive tutorial introduction to the Java language and essential libraries and an indispensable reference for all programmers, including those with extensive experience. It brings together insights you can only get from the creators of Java: insights that will help you write software of exceptional quality.
file:///D|/1/0321349806/main.html [11.01.2008 03:36:06]
Table of Contents
THE Java™ Programming Language, Fourth Edition By Ken Arnold, James Gosling, David Holmes ............................................... Publisher: Addison Wesley Professional Pub Date: August 17, 2005 ISBN: 0-321-34980-6 Pages: 928
Table of Contents | Index
Copyright The Java™ Series Preface About This Book Examples and Documentation Acknowledgments (Fourth Edition) Acknowledgments (Third Edition) Acknowledgments (Second Edition) Acknowledgments (First Edition) Chapter 1. A Quick Tour Section 1.1. Getting Started Section 1.2. Variables Section 1.3. Comments in Code Section 1.4. Named Constants Section 1.5. Unicode Characters Section 1.6. Flow of Control Section 1.7. Classes and Objects Section 1.8. Methods and Parameters Section 1.9. Arrays Section 1.10. String Objects Section 1.11. Extending a Class Section 1.12. Interfaces Section 1.13. Generic Types Section 1.14. Exceptions Section 1.15. Annotations Section 1.16. Packages Section 1.17. The Java Platform Section 1.18. Other Topics Briefly Noted Chapter 2. Classes and Objects Section 2.1. A Simple Class
file:///D|/1/0321349806/toc.html (1 von 7) [11.01.2008 03:36:15]
Table of Contents
Section 2.2. Fields Section 2.3. Access Control Section 2.4. Creating Objects Section 2.5. Construction and Initialization Section 2.6. Methods Section 2.7. this Section 2.8. Overloading Methods Section 2.9. Importing Static Member Names Section 2.10. The main Method Section 2.11. Native Methods Chapter 3. Extending Classes Section 3.1. An Extended Class Section 3.2. Constructors in Extended Classes Section 3.3. Inheriting and Redefining Members Section 3.4. Type Compatibility and Conversion Section 3.5. What protected Really Means Section 3.6. Marking Methods and Classes final Section 3.7. Abstract Classes and Methods Section 3.8. The Object Class Section 3.9. Cloning Objects Section 3.10. Extending Classes: How and When Section 3.11. Designing a Class to Be Extended Section 3.12. Single Inheritance versus Multiple Inheritance Chapter 4. Interfaces Section 4.1. A Simple Interface Example Section 4.2. Interface Declarations Section 4.3. Extending Interfaces Section 4.4. Working with Interfaces Section 4.5. Marker Interfaces Section 4.6. When to Use Interfaces Chapter 5. Nested Classes and Interfaces Section 5.1. Static Nested Types Section 5.2. Inner Classes Section 5.3. Local Inner Classes Section 5.4. Anonymous Inner Classes Section 5.5. Inheriting Nested Types Section 5.6. Nesting in Interfaces Section 5.7. Implementation of Nested Types Chapter 6. Enumeration Types Section 6.1. A Simple Enum Example Section 6.2. Enum Declarations
file:///D|/1/0321349806/toc.html (2 von 7) [11.01.2008 03:36:15]
Table of Contents
Section 6.3. Enum Constant Declarations Section 6.4. java.lang.Enum Section 6.5. To Enum or Not Chapter 7. Tokens, Values, and Variables Section 7.1. Lexical Elements Section 7.2. Types and Literals Section 7.3. Variables Section 7.4. Array Variables Section 7.5. The Meanings of Names Chapter 8. Primitives as Types Section 8.1. Common Fields and Methods Section 8.2. Void Section 8.3. Boolean Section 8.4. Number Section 8.5. Character Section 8.6. Boxing Conversions Chapter 9. Operators and Expressions Section 9.1. Arithmetic Operations Section 9.2. General Operators Section 9.3. Expressions Section 9.4. Type Conversions Section 9.5. Operator Precedence and Associativity Section 9.6. Member Access Chapter 10. Control Flow Section 10.1. Statements and Blocks Section 10.2. ifelse Section 10.3. switch Section 10.4. while and dowhile Section 10.5. for Section 10.6. Labels Section 10.7. break Section 10.8. continue Section 10.9. return Section 10.10. What, No goto? Chapter 11. Generic Types Section 11.1. Generic Type Declarations Section 11.2. Working with Generic Types Section 11.3. Generic Methods and Constructors Section 11.4. Wildcard Capture Section 11.5. Under the Hood: Erasure and Raw Types Section 11.6. Finding the Right Method Revisited
file:///D|/1/0321349806/toc.html (3 von 7) [11.01.2008 03:36:15]
Table of Contents
Section 11.7. Class Extension and Generic Types Chapter 12. Exceptions and Assertions Section 12.1. Creating Exception Types Section 12.2. throw Section 12.3. The throws Clause Section 12.4. try, catch, and finally Section 12.5. Exception Chaining Section 12.6. Stack Traces Section 12.7. When to Use Exceptions Section 12.8. Assertions Section 12.9. When to Use Assertions Section 12.10. Turning Assertions On and Off Chapter 13. Strings and Regular Expressions Section 13.1. Character Sequences Section 13.2. The String Class Section 13.3. Regular Expression Matching Section 13.4. The StringBuilder Class Section 13.5. Working with UTF-16 Chapter 14. Threads Section 14.1. Creating Threads Section 14.2. Using Runnable Section 14.3. Synchronization Section 14.4. wait, notifyAll, and notify Section 14.5. Details of Waiting and Notification Section 14.6. Thread Scheduling Section 14.7. Deadlocks Section 14.8. Ending Thread Execution Section 14.9. Ending Application Execution Section 14.10. The Memory Model: Synchronization and volatile Section 14.11. Thread Management, Security, and ThreadGroup Section 14.12. Threads and Exceptions Section 14.13. ThreadLocal Variables Section 14.14. Debugging Threads Chapter 15. Annotations Section 15.1. A Simple Annotation Example Section 15.2. Annotation Types Section 15.3. Annotating Elements Section 15.4. Restricting Annotation Applicability Section 15.5. Retention Policies Section 15.6. Working with Annotations Chapter 16. Reflection
file:///D|/1/0321349806/toc.html (4 von 7) [11.01.2008 03:36:15]
Table of Contents
Section 16.1. The Class Class Section 16.2. Annotation Queries Section 16.3. The Modifier Class Section 16.4. The Member classes Section 16.5. Access Checking and AccessibleObject Section 16.6. The Field Class Section 16.7. The Method Class Section 16.8. Creating New Objects and the Constructor Class Section 16.9. Generic Type Inspection Section 16.10. Arrays Section 16.11. Packages Section 16.12. The Proxy Class Section 16.13. Loading Classes Section 16.14. Controlling Assertions at Runtime Chapter 17. Garbage Collection and Memory Section 17.1. Garbage Collection Section 17.2. A Simple Model Section 17.3. Finalization Section 17.4. Interacting with the Garbage Collector Section 17.5. Reachability States and Reference Objects Chapter 18. Packages Section 18.1. Package Naming Section 18.2. Type Imports Section 18.3. Package Access Section 18.4. Package Contents Section 18.5. Package Annotations Section 18.6. Package Objects and Specifications Chapter 19. Documentation Comments Section 19.1. The Anatomy of a Doc Comment Section 19.2. Tags Section 19.3. Inheriting Method Documentation Comments Section 19.4. A Simple Example Section 19.5. External Conventions Section 19.6. Notes on Usage Chapter 20. The I/O Package Section 20.1. Streams Overview Section 20.2. Byte Streams Section 20.3. Character Streams Section 20.4. InputStreamReader and OutputStreamWriter Section 20.5. A Quick Tour of the Stream Classes Section 20.6. The Data Byte Streams
file:///D|/1/0321349806/toc.html (5 von 7) [11.01.2008 03:36:15]
Table of Contents
Section 20.7. Working with Files Section 20.8. Object Serialization Section 20.9. The IOException Classes Section 20.10. A Taste of New I/O Chapter 21. Collections Section 21.1. Collections Section 21.2. Iteration Section 21.3. Ordering with Comparable and Comparator Section 21.4. The Collection Interface Section 21.5. Set and SortedSet Section 21.6. List Section 21.7. Queue Section 21.8. Map and SortedMap Section 21.9. enum Collections Section 21.10. Wrapped Collections and the Collections Class Section 21.11. Synchronized Wrappers and Concurrent Collections Section 21.12. The Arrays Utility Class Section 21.13. Writing Iterator Implementations Section 21.14. Writing Collection Implementations Section 21.15. The Legacy Collection Types Section 21.16. Properties Chapter 22. Miscellaneous Utilities Section 22.1. Formatter Section 22.2. BitSet Section 22.3. Observer/Observable Section 22.4. Random Section 22.5. Scanner Section 22.6. StringTokenizer Section 22.7. Timer and TimerTask Section 22.8. UUID Section 22.9. Math and StrictMath Chapter 23. System Programming Section 23.1. The System Class Section 23.2. Creating Processes Section 23.3. Shutdown Section 23.4. The Rest of Runtime Section 23.5. Security Chapter 24. Internationalization and Localization Section 24.1. Locale Section 24.2. Resource Bundles Section 24.3. Currency
file:///D|/1/0321349806/toc.html (6 von 7) [11.01.2008 03:36:15]
Table of Contents
Section 24.4. Time, Dates, and Calendars Section 24.5. Formatting and Parsing Dates and Times Section 24.6. Internationalization and Localization for Text Chapter 25. Standard Packages Section 25.1. java.awt The Abstract Window Toolkit Section 25.2. java.applet Applets Section 25.3. java.beans Components Section 25.4. java.math Mathematics Section 25.5. java.net The Network Section 25.6. java.rmi Remote Method Invocation Section 25.7. java.security and Related Packages Security Tools Section 25.8. java.sql Relational Database Access Section 25.9. Utility Subpackages Section 25.10. javax.* Standard Extensions Section 25.11. javax.accessibility Accessibility for GUIs Section 25.12. javax.naming Directory and Naming Services Section 25.13. javax.sound Sound Manipulation Section 25.14. javax.swing Swing GUI Components Section 25.15. org.omg.CORBA CORBA APIs Appendix A. Application Evolution Section A.1. Language, Library, and Virtual Machine Versions Section A.2. Dealing with Multiple Dialects Section A.3. Generics: Reification, Erasure, and Raw Types Appendix B. Useful Tables Further Reading Java Platform Topics Object-Oriented Design Multithreaded Programming Generics Regular Expressions General Programming Techniques Related Languages Software Engineering Visual Design & GUI Design Colophon Note to Translators Article Exercises Quotes Index
file:///D|/1/0321349806/toc.html (7 von 7) [11.01.2008 03:36:15]
Copyright
Copyright © 2006 Sun Microsystems, Inc. 4150 Network Circle, Santa Clara, California 95054 U.S.A. All rights reserved. Use, duplication, or disclosure by the United States Government is subject to the restrictions set forth in DFARS 252.227-7013 (c)(1)(ii) and FAR 52.227-19. The release described in this book may be protected by one or more U.S. patents, foreign patents, or pending applications. Sun Microsystems, Inc. (SUN) hereby grants to you a fully-paid, nonexclusive, nontransferable, perpetual, worldwide limited license (without the right to sublicense) under SUN's intellectual property rights that are essential to practice this specification. This license allows and is limited to the creation and distribution of clean room implementations of this specification that: (i) include a complete implementation of the current version of this specification without subsetting or supersetting; (ii) implement all the interfaces and functionality of the standard java.* packages as defined by SUN, without subsetting or supersetting; (iii) do not add any additional packages, classes or methods to the java.* packages; (iv) pass all test suites relating to the most recent published version of this specification that are available from SUN six (6) months prior to any beta release of the clean room implementation or upgrade thereto; (v) do not derive from SUN source code or binary materials; and (vi) do not include any SUN binary materials without an appropriate and separate license from SUN. Sun, Sun Microsystems, Sun Microsystems Computer Corporation, the Sun logo, the Sun Microsystems Computer Corporation logo, Java, JavaSoft, JavaScript JDBC, JDBC Compliant, JavaOS, JavaBeans and HotJava are trademarks or registered trademarks of Sun Microsystems, Inc. UNIX® is a registered trademark in the United States and other countries, exclusively licensed through X/Open Company, Ltd. Apple and Dylan are trademarks of Apple Computer, Inc. All other product names mentioned herein are the trademarks of their respective owners. THIS PUBLICATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THIS PUBLICATION COULD INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS. CHANGES ARE PERIODICALLY ADDED TO THE INFORMATION HEREIN; THESE CHANGES WILL BE INCORPORATED IN NEW EDITIONS OF THE PUBLICATION. SUN MICROSYSTEMS, INC. MAY MAKE IMPROVEMENTS AND/OR CHANGES IN THE PRODUCT(S) AND/OR THE PROGRAM(S) DESCRIBED IN THIS PUBLICATION AT ANY TIME. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
file:///D|/1/0321349806/copyrightpg.html (1 von 2) [11.01.2008 03:36:16]
Copyright
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U. S. Corporate and Government Sales, (800) 382-3419,
[email protected] For sales outside the U. S., please contact: International Sales,
[email protected] Visit us on the Web: www.awprofessional.com Library of Congress Cataloging-in-Publication Data
Arnold, Ken, 1958The Java programming language / Ken Arnold, James Gosling, David Holmes. p. cm. Includes bibliographical references and index. ISBN 0-321-34980-6 (pbk. : alk. paper) 1. Java (Computer program language) I. Gosling, James. II. Holmes, David (David Colin) III. Title. QA76.73.J38A76 2005 005.13'3--dc22 2005017029 All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, write to: Pearson Education, Inc., Rights and Contracts Department, One Lake Street, Upper Saddle River, NJ 07458 Text printed in the United States on recycled paper at Courier in Stoughton, Massachusetts. First printing, August 2005
Dedication This book is dedicated to the Java team From whose hard work and vision A mighty oak has grown To SusanK.A. To Judy and KateJ.A.G. To Lee, Taylor, and CaitlinD.H.
file:///D|/1/0321349806/copyrightpg.html (2 von 2) [11.01.2008 03:36:16]
The Java™ Series
The Java™ Series Ken Arnold, James Gosling, David Holmes The Java™ Programming Language, Third Edition Cynthia Bloch, Annette Wagner MIDP 2.0 Style Guide for the Java™ 2 Platform, Micro Edition Joshua Bloch Effective Java™ Programming Language Guide Mary Campione, Kathy Walrath, Alison Huml The Java™ Tutorial, Third Edition: A Short Course on the Basics Mary Campione, Kathy Walrath, Alison Huml,Tutorial Team The Java™ Tutorial Continued: The Rest of the JDK™ Patrick Chan The Java™ Developers Almanac 1.4, Volume 1 Patrick Chan The Java™ Developers Almanac 1.4, Volume 2 Patrick Chan, Rosanna Lee The Java™ Class Libraries, Second Edition, Volume 2: java.applet, java.awt, java.beans Patrick Chan, Rosanna Lee, Doug Kramer The Java™ Class Libraries, Second Edition, Volume 1: java.io, java.lang, java.math, java.net, java.text, java.util Patrick Chan, Rosanna Lee, Doug Kramer The Java™ Class Libraries, Second Edition, Volume 1: Supplement for the Java™ 2 Platform, Standard Edition, v1.2 Kirk Chen, Li Gong
file:///D|/1/0321349806/pref01.html (1 von 3) [11.01.2008 03:36:16]
The Java™ Series
Programming Open Service Gateways with Java™ Embedded Server Zhiqun Chen Java Card™ Technology for Smart Cards: Architecture and Programmer's Guide Maydene Fisher, Jon Ellis, Jonathan Bruce JDBC™ API Tutorial and Reference, Third Edition Li Gong, Gary Ellison, Mary Dageforde Inside Java™ 2 Platform Security, Second Edition: Architecture, API Design, and Implementation James Gosling, Bill Joy, Guy Steele, Gilad Bracha The Java™ Language Specification, Third Edition Doug Lea Concurrent Programming in Java™, Second Edition: Design Principles and Patterns Rosanna Lee, Scott Seligman JNDI API Tutorial and Reference: Building Directory-Enabled Java™ Applications Sheng Liang The Java™ Native Interface: Programmer's Guide and Specification Tim Lindholm, Frank Yellin The Java™ Virtual Machine Specification, Second Edition Roger Riggs, Antero Taivalsaari, Jim Van Peursem, Jyri Huopaniemi, Mark Patel, Aleksi Uotila Programming Wireless Devices with the Java™ 2 Platform, Micro Edition, Second Edition Sun Microsystems, Inc. Java™ Look and Feel Design Guidelines: Advanced Topics Kathy Walrath, Mary Campione, Alison Huml, Sharon Zakhour The JFC Swing Tutorial, Second Edition: A Guide to Constructing GUIs Seth White, Maydene Fisher, Rick Cattell, Graham Hamilton, Mark Hapner JDBC™ API Tutorial and Reference, Second Edition: Universal Data Access for the Java™ 2 Platform file:///D|/1/0321349806/pref01.html (2 von 3) [11.01.2008 03:36:16]
The Java™ Series
Steve Wilson, Jeff Kesselman Java™ Platform Performance: Strategies and Tactics The Jini™ Technology Series Eric Freeman, Susanne Hupfer, Ken Arnold JavaSpaces™ Principles, Patterns, and Practice The Java™ Series, Enterprise Edition Stephanie Bodoff, Dale Green, Kim Haase, Eric Jendrock The J2EE™ Tutorial, Second Edition Rick Cattell, Jim Inscore, Enterprise Partners J2EE™ Technology in Practice: Building Business Applications with the Java™ 2 Platform, Enterprise Edition Mark Hapner, Rich Burridge, Rahul Sharma, Joseph Fialli, Kim Haase Java™ Message Service API Tutorial and Reference: Messaging for the J2EE™ Platform Inderjeet Singh, Beth Stearns, Mark Johnson, Enterprise Team Designing Enterprise Applications with the Java™ 2 Platform, Enterprise Edition Vlada Matena, Sanjeev Krishnan, Linda DeMichiel, Beth Stearns Applying Enterprise JavaBeans™, Second Edition: Component-Based Development for the J2EE™ Platform Bill Shannon, Mark Hapner, Vlada Matena, James Davidson, Eduardo Pelegri-Llopart, Larry Cable, Enterprise Team Java™ 2 Platform, Enterprise Edition: Platform and Component Specifications Rahul Sharma, Beth Stearns, Tony Ng J2EE™ Connector Architecture and Enterprise Application Integration
file:///D|/1/0321349806/pref01.html (3 von 3) [11.01.2008 03:36:16]
Preface
Preface Beautiful buildings are more than scientific. They are true organisms, spiritually conceived; works of art, using the best technology by inspiration rather than the idiosyncrasies of mere taste or any averaging by the committee mind. Frank Lloyd Wright The Java™ programming language has been warmly received by the world community of software developers and Internet content providers. Users of the Internet and World Wide Web benefit from access to secure, platform-independent applications that can come from anywhere on the Internet. Software developers who create applications in the Java programming language benefit by developing code only once, with no need to "port" their applications to every software and hardware platform. For many, the language was known first as a tool to create applets for the World Wide Web. An applet is a mini-application that runs inside a Web page. An applet can perform tasks and interact with users on their browser pages without using resources from the Web server after being downloaded. Some applets may, of course, talk with the server to do their job, but that's their business. The Java programming language is indeed valuable for distributed network environments like the Web. However, it goes well beyond this domain to provide a powerful general-purpose programming language suitable for building a variety of applications that either do not depend on network features or want them for different reasons. The ability to execute downloaded code on remote hosts in a secure manner is a critical requirement for many organizations. Other groups use it as a general-purpose programming language for projects in which machine independence is less important. Ease of programming and safety features help you quickly produce working code. Some common programming errors never occur because of features like garbage collection and type-safe references. Support for multithreading caters to modern network-based and graphical user interfacebased applications that must attend to multiple tasks simultaneously, and the mechanisms of exception handling ease the task of dealing with error conditions. While the built-in tools are powerful, it is a simple language in which programmers can quickly become proficient. The Java programming language is designed for maximum portability with as few implementation dependencies as possible. An int, for example, is a 32-bit signed two's-complement integer in all implementations, irrespective of the CPU architecture on which the program executes. Defining everything possible about the language and its runtime environment enables users to run compiled code anywhere and share code with anyone who has a Java runtime environment.
file:///D|/1/0321349806/pref02.html [11.01.2008 03:36:17]
About This Book
About This Book This book teaches the Java programming language to people who are familiar with basic programming concepts. It explains the language without being arduously formal or complete. This book is not an introduction to object-oriented programming, although some issues are covered to establish a common terminology. Other books in this series and much online documentation focus on applets, graphical interfaces, Web sites, databases, components, and other specific kinds of programming tasks. For other references, see "Further Reading" on page 755. This fourth edition provides integrated coverage of the Java programming language as provided by the Java™ 2 Platform Standard Edition 5.0 and specified by the Java™ Language Specification, Third Edition. It also covers most of the classes in the main packages (java.lang, java.util, java.io) as implemented in the J2SE™ Development Kit 5.0 (more commonly known as JDK 5.0, or in the older nomenclature JDK 1.5.0). If you have already read the third edition of this book, you will find some major changes, both in the language and the book, since the 1.3 release that the third edition covered. There are new chapters on generics, enums, and annotationsthe major new language features introduced in the 5.0 releaseand major new sections on assertions and regular expressions. Some existing material has been restructured to accommodate other changes and to improve the general flow of the textsuch as introducing the new boxing and unboxing conversions. But every single chapter has been updated in some way, whether it is a new language feature like variable argument methods; the new enhanced for loop construct; a new class such as Formatter for formatting text output; or changes to classes and methods caused by the addition of generics (such as the collections utilities and the reflection classes)change permeates this entire fourth edition. The Java programming language shares many features common to most programming languages in use today. The language should look familiar to C and C++ programmers because it was designed with C and C++ constructs where the languages are similar. That said, this book is neither a comparative analysis nor a "bridge" tutorialno knowledge of C or C++ is assumed. C++ programmers, especially, may be as hindered by what they must unlearn as they are helped by their knowledge. Chapter 1A Quick Tourgives a quick overview of the language. Programmers who are unfamiliar with object-oriented programming notions should read the quick tour, while programmers who are already familiar with object-oriented programming paradigms will find the quick tour a useful introduction to the object-oriented features of the language. The quick tour introduces some of the basic language features on which examples through the rest of the book are built. Chapters 2 through 6 cover the object-oriented core features of the language, namely, class declarations that define components of a program, and objects manufactured according to class definitions. Chapter 2Classes and Objectsdescribes the basis of the language: classes. Chapter 3Extending Classesdescribes how an existing class can be extended, or subclassed, to create a new class with additional data and behavior. Chapter 4Interfacesdescribes how to declare interface types that are abstract descriptions of behavior that provide maximum flexibility for class designers and implementors. Chapter 5Nested Classes and Interfacesdescribes how classes and interfaces can be declared inside other classes and interfaces, and the benefits that provides. Finally, Chapter 6Enumeration Typescovers the definition and use of type-safe enumeration constants. Chapters 7 through 10 cover standard constructs common to most languages. Chapter 7Tokens, Values, and Variablesdescribes the tokens of the language from which statements are constructed, the types defined by the language and their allowed values, and the variables that store data in objects, arrays, or locally within methods. Chapter 8Primitives as Typesexplores the relationship between the primitive file:///D|/1/0321349806/fm01lev1sec1.html (1 von 3) [11.01.2008 03:36:17]
About This Book
types and objects of their corresponding wrapper classes, and how boxing and unboxing can transparently convert between them. Chapter 9Operators and Expressionsdescribes the basic operators of the language, how these operators are used to build expressions, and how expressions are evaluated. Chapter 10Control Flowdescribes how control statements direct the order of statement execution. Chapter 11Generic Typesdescribes generic types: how they are written and used, their power, and their limitations. Chapter 12Exceptions and Assertionsdescribes the language's powerful error-handling capabilities, and the use of assertions to validate the expected behavior of code. Chapter 13Strings and Regular Expressionsdescribes the built-in language and runtime support for String objects, the underlying character set support, and the powerful utilities for regular expression matching. Chapter 14Threadsexplains the language's view of multithreading. Many applications, such as graphical interfacebased software, must attend to multiple tasks simultaneously. These tasks must cooperate to behave correctly, and threads meet the needs of cooperative multitasking. Chapter 15Annotationsdescribes the annotation types used to document some of the extra-linguistic properties of classes and method. Chapter 16Reflectiondescribes the runtime type introspection mechanism and how you can construct and manipulate objects of unknown type dynamically at runtime. Chapter 17Garbage Collection and Memorytalks about garbage collection, finalization, and lowerstrength reference objects. Chapter 18Packagesdescribes how you can group collections of classes and interfaces into separate packages. Chapter 19Documentation Commentsshows how to write reference documentation in comments. Chapters 20 through 24 cover the main packages. Chapter 20The I/O Packagedescribes the input/output system, which is based on streams. Chapter 21Collectionscovers the collection or container classes such as sets and lists. Chapter 22Miscellaneous Utilitiescovers the rest of the utility classes such as bit sets, formatted output, text scanning, and random number generation. Chapter 23System Programmingleads you through the system classes that provide access to features of the underlying platform. Chapter 24Internationalization and Localizationcovers some of the tools used to create programs that can run in many linguistic and cultural environments. Chapter 25Standard Packagesbriefly explores the packages that are part of the standard platform, giving overviews of those packages not covered in more detail in this book. Appendix AApplication Evolutionlooks at some of the issues involved in dealing with the evolution of applications and the Java platform, and the impact this has on some of the new language features. Appendix BUseful Tableshas tables of information that you may find useful for quick reference. Finally, Further Reading lists works that may be interesting for further reading on complete details,
file:///D|/1/0321349806/fm01lev1sec1.html (2 von 3) [11.01.2008 03:36:17]
About This Book
object orientation, programming with threads, software design, and other topics.
file:///D|/1/0321349806/fm01lev1sec1.html (3 von 3) [11.01.2008 03:36:17]
Examples and Documentation
Examples and Documentation All the code examples in the text have been compiled and run on the latest version of the language available at the time the book was written, which was the JDK 1.5.0_02 product version. Only supported features are covereddeprecated types, methods, and fields are ignored except when unavoidable or when knowledge of the past is necessary to understand the present. We have also covered issues beyond writing programs that simply compile. Part of learning a language is to learn to use it well. For this reason, we have tried to show principles of good programming style and design. In a few places we refer to online documentation. Development environments provide a way to automatically generate documentation (usually HTML documents) for a compiled class from its documentation comments. This documentation is normally viewed with a Web browser.
file:///D|/1/0321349806/fm01lev1sec2.html [11.01.2008 03:36:18]
Acknowledgments (Fourth Edition)
Acknowledgments (Fourth Edition) Once again we are indebted to a group of people who took time out from their hectic schedules (and even their own books!) to give us the benefit of their experiences in making this the best edition yet. Of course, some of those people were also responsible for the huge volume of work required for us to catch up with all the new language and library features, but we won't hold that against them. Much. On the reviewing side our thanks go first to Yoshiki Shibata. Yoshiki produced the Japanese translation of the third edition and in the process single-handedly discovered at least 85% of the errata. To get the fourth edition as errata free as possible we knew we needed Yoshiki onboard as a reviewer very early on. Yoshiki became our front-line reviewer and nothing went out for general review without his nod of approval. Not only did he provide valuable corrections and feedback on the new material, he also pointed out several areas where there had been language changes that we hadn't been aware of! Brian Goetz also rates special thanks not only for reviewing the entire edition, but also for re-reviewing various sections that took several iterations to get right. The turnaround times for these re-reviews were very tight and we much appreciate Brian's time at that critical stage. The remainder of the core reviewing team consisted of Beth Stearns and Kevin Davis, who were able to stay for the long haul, while Bob Withers, Herb Jellinek, Ken Louden, Rich Wardwell, and Kevin Hammond provided valuable feedback on the earlier portions of the text. Thank you all very much. We'd also like to thank Tim Peierls for supplying the chess example used in the enum chapter; and Keith Lea and Martin Buchholz for valuable reviews of the generics materialthanks! It will come as no surprise to most that writing the generics chapter (and the related part of the appendix) was the hardest part of this edition. Armed initially only with a draft of the third edition of the Java Language Specification, it was never going to be easy to fathom the depths of the new type system, so we brought in the big guns: Neal Gafter, Josh Bloch, Gilad Bracha, and Peter von der Ahé were all instrumental in educating us about generics, and for that we thank them. Peter's in-depth review of the generics chapter was especially valuable and warrants an additional thank you. Of course, the teachers cannot be blamed for the shortcomings of the student, so any errors or omissions in the description of generics are the sole responsibility of the authors. (We'd like to thank Phil Wadler for catching two such omissions in the generics coverage just prior to publication.) We'd also like to thank the following technical experts for their assistance in specific areas: Doug Lea for reviewing the Java memory model material and providing assistance with concurrent collections; Brian Goetz for checking the overview of the concurrent collections (in addition to everything else); Mike "MadBot" McCloskey for the review of, and assistance with, the use of the Scanner class and the associated regular expression usage; and Jeremy Manson for trying to explain the unfathomable relationship between finalization and the memory model. Rosemary Simpson seems to always manage to be around when we need her. It's starting to get difficult to find a creative way to say how much we appreciate her indexing, but I'm sure we'll have one by the fifth edition. On the production side, the path to the fourth edition was a somewhat changeable one. Mike Hendrickson held the reins when the fourth edition was conceived, then handed the reins to Ann Sellers. Ann, with assistance from Ebony Haight, saw the fourth edition through its early gestation, then handed the reins to Greg Doench. Greg, with assistance from Noreen Regina, Stephane Nakib, Tyrrell Albaugh, Elizabeth Ryan, and Mary Lou Nohr saw us through to the ultimate delivery. Thank you all, and to all those behind the scenes.
file:///D|/1/0321349806/fm01lev1sec3.html (1 von 2) [11.01.2008 03:36:18]
Acknowledgments (Fourth Edition)
And thanks as well to Kathy Kerby, Ben Littauer, and Ariana Littauer for being a valuable source of borrowed computrons. It's always nice to have neighbors from whom you can borrow a cup of cycles. Last, but by no means least, thank you to our families for putting up with the trials and tribulations of authorship once again.
file:///D|/1/0321349806/fm01lev1sec3.html (2 von 2) [11.01.2008 03:36:18]
Acknowledgments (Third Edition)
Acknowledgments (Third Edition) The third edition required yet more reviews and work, and the helper list is equally critical. Lisa Friendly continued her attempts to keep the project in line; someday we will cooperate better. The set of reviewers included new faces and old friends, all helpful: Joshua Bloch, Joseph Bowbeer, Gilad Bracha, Keith Edwards, Joshua Engel, Rich Gillam, Peter Haggar, Cay Horstmann, Alexander Kuzmin, Doug Lea, Keith Lea, Tim Lindholm, David Mendenhall, Andrew M. Morgan, Ray Ortigas, Brian Preston, Mark Schuldenfrei, Peter Sparago, Guy Steele, Antoine Trux, and our Russian compatriots Leonid Arbouzov, Valery Shakurov, Viatcheslav Rybalov, Eugene Latkin, Dmitri Khukhro, Konstantin Anisimov, Alexei Kaigorodov, Oleg Oleinik, and Maxim Sokolnikov. Several people let us bend their ears to figure out how to approach things better: Peter Jones, Robert W. Scheifler, Susan Snyder, Guy Steele, Jimmy Torres, and Ann Wollrath. Helen Leary made the logistics work smoothly, as always. Material support is always provided by the Addison-Wesley team: Julie DiNicola, Mike Hendrickson, and Tracy Russ. And since the last edition, Peet's Coffee and Tea has opened up on the East Coast, so the eastern part of this writing team can enjoy it regularly. The world continues to improve apace.
file:///D|/1/0321349806/fm01lev1sec4.html [11.01.2008 03:36:18]
Acknowledgments (Second Edition)
Acknowledgments (Second Edition) The cast of characters for this second edition is much like the first. Series Editor Lisa Friendly continued to be doggedly supportive and attentive. The set of reviewers was smaller, overlapping, and certainly as helpful and thorough. Overall reviews by Steve Byrne, Tom Cargill, Mary Dageforde, Tim Lindholm, and Rob Murray were critical to clarity. Brian Beck, Peter Jones, Doug Lea, Bryan O'Sullivan, Sue Palmer, Rosanna Lee, Lori Park, Mark Reinhold, Roger Riggs, Ann Wollrath, and Ken Zadek contributed focused reviews of important parts. Guy Steele's support was ongoing and warm. Rosemary Simpson's extensive and intensive efforts to make a useful index are deeply appreciated. Carla Carlson and Helen Leary gave logistic support that kept all the wheels on the tracks instead of in the ditch. Gerry Wiener provided the Tibetan word on page 550, and we also had help on this from Craig Preston and Takao Miyatani. All who submitted errata and suggestions from the first edition were helpful. For some inexplicable reason we left the friendly folks of Addison-Wesley off the original acknowledgmentsluckily, most of them were present again for this edition. A merged list for both editions includes Kate Duffy, Rosa Gonzales, Mike Hendrickson, Marina Lang, Shannon Patti, Marty Rabinowitz, Sarah Weaver, and Pamela Yee. Others did much that we are blissfully unaware of, but for which we are nonetheless abidingly grateful. The revision was additionally aided by Josh Bloch, Joe Fialli, Jimmy Torres, Benjamin Renaud, Mark Reinhold, Jen Volpe, and Ann Wollrath. And Peet's Coffee and Tea continued its supporting role as purveyor to the caffeine-consuming connoisseur.
file:///D|/1/0321349806/fm01lev1sec5.html [11.01.2008 03:36:19]
Acknowledgments (First Edition)
Acknowledgments (First Edition) No technical book-writing endeavor is an island unto itself, and ours was more like a continent. Many people contributed technical help, excellent reviews, useful information, and book-writing advice. Contributing editor Henry McGilton of Trilithon Software played the role of "chief editorial firefighter" to help make this book possible. Series editor Lisa Friendly contributed dogged perseverance and support. A veritable multitude of reviewers took time out of their otherwise busy lives to read, edit, advise, revise, and delete material, all in the name of making this a better book. Kevin Coyle performed one of the most detailed editorial reviews at all levels. Karen Bennet, Mike Burati, Patricia Giencke, Steve Gilliard, Bill Joy, Rosanna Lee, Jon Madison, Brian O'Neill, Sue Palmer, Stephen Perelgut, R. Anders Schneiderman, Susan Sim, Bob Sproull, Guy Steele, Arthur van Hoff, Jim Waldo, Greg Wilson, and Ann Wollrath provided in-depth review. Geoff Arnold, Tom Cargill, Chris Darke, Pat Finnegan, Mick Jordan, Doug Lea, Randall Murray, Roger Riggs, Jimmy Torres, Arthur van Hoff, and Frank Yellin contributed useful comments and technical information at critical junctures. Alka Deshpande, Sharon Flank, Nassim Fotouhi, Betsy Halstead, Kee Hinckley, Dr. K. Kalyanasundaram, Patrick Martin, Paul Romagna, Susan Snyder, and Nicole Yankelovich collaborated to make possible the five words of non-ISO-Latin-1 text on pages 164 and 550. Jim Arnold provided research help on the proper spelling, usage, and etymology of "smoog" and "moorge." Ed Mooney helped with the document preparation. Herb and Joy Kaiser were our Croatian language consultants. Cookie Callahan, Robert E. Pierce, and Rita Tavilla provided the support necessary to keep this project going at many moments when it would otherwise have stalled with a sputtering whimper. Thanks to Kim Polese for supplying us the capsule summary of why the Java programming language is important to computer users as well as programmers. Support and advice were provided at critical moments by Susan Jones, Bob Sproull, Jim Waldo, and Ann Wollrath. And we thank our families, who, besides their loving support, would at times drag us out to play when we should have been working, for which we are deeply grateful. And thanks to the folks at Peet's Coffee and Tea, who kept us buzzed on the best Java on the planet. Any errors or shortcomings that remain in this bookdespite the combined efforts of these myriadsare completely the responsibility of the authors. Results! Why, man, I have gotten a lot of results. I know several thousand things that won't work. Thomas Edison Calvin and Hobbes quote (page 39) reprinted by permission of Universal Press Syndicate. "Mail Myself to You," (page 183) words and music by Woody Guthrie. trO 1962 (renewed), 1963 (renewed) by Ludlow Music, Inc. New York, NY. Used by permission.
file:///D|/1/0321349806/fm01lev1sec6.html [11.01.2008 03:36:19]
Chapter 1. A Quick Tour
Chapter 1. A Quick Tour See Europe! Ten Countries in Seventeen Days! Sign in a travel agent's window This chapter is a whirlwind tour of the Java™ programming language that gets you started writing code quickly. We briefly cover the main points of the language, without slowing you down with full-blown detail. Subsequent chapters contain detailed discussions of specific features.
file:///D|/1/0321349806/ch01.html [11.01.2008 03:36:19]
Section 1.1. Getting Started
1.1. Getting Started In the Java programming language, programs are built from classes. From a class definition, you can create any number of objects that are known as instances of that class. Think of a class as a factory with blueprints and instructions to build gadgetsobjects are the gadgets the factory makes. A class contains members, the primary kinds being fields and methods. Fields are data variables belonging either to the class itself or to objects of the class; they make up the state of the object or class. Methods are collections of statements that operate on the fields to manipulate the state. Statements define the behavior of the classes: they can assign values to fields and other variables, evaluate arithmetic expressions, invoke methods, and control the flow of execution. Long tradition holds that the first sample program for any language should print "Hello, world": class HelloWorld { public static void main(String[] args) { System.out.println("Hello, world"); } }
Use your favorite text editor to type this program source code into a file. Then run the compiler to compile the source of this program into bytecodes, the "machine language" for the Java virtual machine (more on this later in this chapter). Details of editing and compiling source vary from system to systemconsult your system manuals for specific information. On the system we use most oftenthe Java™ 2 Platform, Standard Edition (J2SE™) Development Kit (JDK) provided free of charge by Sun Microsystemsyou put the source for HelloWorld into a file named HelloWorld.java. To compile it you type the command javac HelloWorld.java
To run the program you type the command java HelloWorld
This executes the main method of HelloWorld. When you run the program, it displays Hello, world
Now you have a small program that does something, but what does it mean? The program declares a class called HelloWorld with a single member: a method called main. Class members appear between curly braces { and } following the class name. The main method is a special method: the main method of a class, if declared exactly as shown, is executed when you run the class as an application. When run, a main method can create objects, evaluate expressions, invoke other methods, and do anything else needed to define an application's file:///D|/1/0321349806/ch01lev1sec1.html (1 von 2) [11.01.2008 03:36:20]
Section 1.1. Getting Started
behavior. The main method is declared publicso that anyone can invoke it (in this case the Java virtual machine) and static, meaning that the method belongs to the class and is not associated with a particular instance of the class. Preceding the method name is the return type of the method. The main method is declared void because it doesn't return a value and so has no return type. Following the method name is the parameter list for the methoda sequence of zero or more pairs of types and names, separated by commas and enclosed in parentheses ( and ). The main method's only parameter is an array of String objects, referred to by the name args. Arrays of objects are denoted by the square brackets [] that follow the type name. In this case args will contain the program's arguments from the command line with which it was invoked. Arrays and strings are covered later in this chapter. The meaning of args for the main method is described in Chapter 2 on page 73. The name of a method together with its parameter list constitute the signature of the method. The signature and any modifiers (such as public and static), the return type, and exception throws list (covered later in this chapter) form the method header. A method declaration consists of the method header followed by the method bodya block of statements appearing between curly braces. In this example, the body of main contains a single statement that invokes the println methodthe semicolon ends the statement. A method is invoked by supplying an object reference (in this case System.outthe out field of the System class) and a method name (println) separated by a dot (.). HelloWorld uses the out object's println method to print a newline-terminated string on the standard output stream. The string printed is the string literal "Hello,world" , which is passed as an argument to println. A string literal is a sequence of characters contained within double-quotes " and ".
Exercise 1.1: Enter, compile, and run HelloWorld on your system. Exercise 1.2: Try changing parts of HelloWorld and see what errors you might get.
file:///D|/1/0321349806/ch01lev1sec1.html (2 von 2) [11.01.2008 03:36:20]
Section 1.2. Variables
1.2. Variables The next example prints a part of the Fibonacci sequence, an infinite sequence whose first few terms are 1 1 2 3 5 8 13 21 34
The Fibonacci sequence starts with the terms 1 and 1, and each successive term is the sum of the previous two terms. A Fibonacci printing program is simple, and it demonstrates how to declare variables, write a simple loop, and perform basic arithmetic. Here is the Fibonacci program: class Fibonacci { /** Print out the Fibonacci sequence for values < 50 */ public static void main(String[] args) { int lo = 1; int hi = 1; System.out.println(lo); while (hi < 50) { System.out.println(hi); hi = lo + hi; // new hi lo = hi - lo; /* new lo is (sum - old lo) that is, the old hi */ } } }
This example declares a Fibonacci class that, like HelloWorld, has a main method. The first two lines of main are statements declaring two local variables: lo and hi. In this program hi is the current term in the series and lo is the previous term. Local variables are declared within a block of code, such as a method body, in contrast to fields that are declared as members of a class. Every variable must have a type that precedes its name when the variable is declared. The variables lo and hi are of type int, 32bit signed integers with values in the range 231 through 2311. The Java programming language has built-in "primitive" data types to support integer, floating-point, boolean, and character values. These primitive types hold numeric data that is understood directly, as opposed to object types defined by programmers. The type of every variable must be defined explicitly. The primitive data types are: boolean
either TRue or false
char
16-bit Unicode UTF-16 character (unsigned)
file:///D|/1/0321349806/ch01lev1sec2.html (1 von 3) [11.01.2008 03:36:20]
Section 1.2. Variables
byte
8-bit integer (signed)
short
16-bit integer (signed)
int
32-bit integer (signed)
long
64-bit integer (signed)
float
32-bit floating-point (IEEE 754)
double
64-bit floating-point (IEEE 754)
For each primitive type there is also a corresponding object type, generally termed a "wrapper" class. For example, the class Integer is the wrapper class for int. In most contexts, the language automatically converts between primitive types and objects of the wrapper class if one type is used where the other is expected. In the Fibonacci program, we declared hi and lo with initial values of 1. The initial values are set by initialization expressions, using the = operator, when the variables are declared. The = operator (also called the assignment operator), sets the variable named on the left-hand side to the value of the expression on the right-hand side. Local variables are undefined prior to initialization. You don't have to initialize them at the point at which you declare them, but if you try to use local variables before assigning a value, the compiler will refuse to compile your program until you fix the problem. As both lo and hi are of the same type, we could have used a short-hand form for declaring them. We can declare more than one variable of a given type by separating the variable names (with their initialization expressions) by commas. We could replace the first two lines of main with the single equivalent line int lo = 1, hi = 1;
or the much more readable int lo = 1, hi = 1;
Notice that the presence of line breaks makes no difference to the meaning of the statementline breaks, spaces, tabs, and other whitespace are purely for the programmer's convenience. The while statement in the example demonstrates one way of looping. The expression inside the while is evaluatedif the expression is true, the loop's body is executed and the expression is tested again. The while is repeated until the expression becomes false. If it never becomes false, the loop will run forever unless something intervenes to break out of the loop, such as a break statement or an exception. The body of the while consists of a single statement. That could be a simple statement (such as a method invocation), another control-flow statement, or a blockzero or more individual statements enclosed in curly braces. The expression that while tests is a boolean expression that has the value true or false. Boolean file:///D|/1/0321349806/ch01lev1sec2.html (2 von 3) [11.01.2008 03:36:20]
Section 1.2. Variables
expressions can be formed with the comparison operators (=) to compare the relative magnitudes of two values or with the == operator or != operator to test for equality or inequality, respectively. The boolean expression hi< 50 in the example tests whether the current high value of the sequence is less than 50. If the high value is less than 50, its value is printed and the next value is calculated. If the high value equals or exceeds 50, control passes to the first line of code following the body of the while loop. That is the end of the main method in this example, so the program is finished. To calculate the next value in the sequence we perform some simple arithmetic and again use the = operator to assign the value of the arithmetic expression on the right to the variable on the left. As you would expect, the + operator calculates the sum of its operands, and the - operator calculates the difference. The language defines a number of arithmetic operators for the primitive integer and floatingpoint types including addition (+), subtraction (-), multiplication (*), and division (/), as well as some other operators we talk about later. Notice that the println method accepts an integer argument in the Fibonacci example, whereas it accepted a string argument in the HelloWorld example. The println method is one of many methods that are overloaded so that they can accept arguments of different types. The runtime system decides which method to actually invoke based on the number and types of arguments you pass to it. This is a very powerful tool. Exercise 1.3: Add a title to the printed list of the Fibonacci program. Exercise 1.4: Write a program that generates a different sequence, such as a table of squares.
file:///D|/1/0321349806/ch01lev1sec2.html (3 von 3) [11.01.2008 03:36:20]
Section 1.3. Comments in Code
1.3. Comments in Code The English text scattered through the code is in comments. There are three styles of comments, all illustrated in the Fibonacci example. Comments enable you to write descriptive text alongside your code, annotating it for programmers who may read your code in the future. That programmer may well be you months or years later. You save yourself effort by commenting your own code. Also, you often find bugs when you write comments, because explaining what the code is supposed to do forces you to think about it. Text that occurs between /* and */ is ignored by the compiler. This style of comment can be used on part of a line, a whole line, or more commonly (as in the example) to define a multiline comment. For single line and part line comments you can use // which tells the compiler to ignore everything after it on that line. The third kind of comment appears at the very top, between /** and */. A comment starting with two asterisks is a documentation comment ("doc comment" for short). Documentation comments are intended to describe declarations that follow them. The comment in the previous example is for the main method. These comments can be extracted by a tool that uses them to generate reference documentation for your classes. By convention, lines within a documentation comment or a /*...*/ comment have a leading asterisk (which is ignored by documentation tools), which gives a visual clue to readers of the extent of the comment.
file:///D|/1/0321349806/ch01lev1sec3.html [11.01.2008 03:36:21]
Section 1.4. Named Constants
1.4. Named Constants Constants are values like 12, 17.9, and "StringsLike This". Constants, or literals as they are also known, are the way you specify values that are not computed and recomputed but remain, well, constant for the life of a program. The Fibonacci example printed all Fibonacci numbers with a value less than 50. The constant 50 was used within the expression of the while loop and within the documentation comment describing main. Suppose that you now want to modify the example to print the Fibonacci numbers with values less than 100. You have to go through the source code and locate and modify all occurrences of the constant 50. Though this is trivial in our example, in general it is a tedious and error-prone process. Further, if people reading the code see an expression like hi< 50 they may have no idea what the constant 50 actually represents. Such "magic numbers" hinder program understandability and maintainability. A named constant is a constant value that is referred to by a name. For example, we may choose the name MAX to refer to the constant 50 in the Fibonacci example. You define named constants by declaring fields of the appropriate type, initialized to the appropriate value. That itself does not define a constant, but a field whose value could be changed by an assignment statement. To make the value a constant we declare the field as final. A final field or variable is one that once initialized can never have its value changedit is immutable. Further, because we don't want the named constant field to be associated with instances of the class, we also declare it as static. We would rewrite the Fibonacci example as follows: class Fibonacci2 { static final int MAX = 50; /** Print the Fibonacci sequence for values < MAX */ public static void main(String[] args) { int lo = 1; int hi = 1; System.out.println(lo); while (hi < MAX) { System.out.println(hi); hi = lo + hi; lo = hi - lo; } } }
Modifying the maximum value now requires only one change, in one part of the program, and it is clearer what the loop condition is actually testing. You can group related constants within a class. For example, a card game might use these constants: class Suit { final static final static final static final static }
int int int int
CLUBS DIAMONDS HEARTS SPADES
= = = =
1; 2; 3; 4;
file:///D|/1/0321349806/ch01lev1sec4.html (1 von 2) [11.01.2008 03:36:21]
Section 1.4. Named Constants
To refer to a static member of a class we use the name of the class followed by dot and the name of the member. With the above declaration, suits in a program would be accessed as Suit.HEARTS, Suit. SPADES, and so on, thus grouping all the suit names within the single name Suit. Notice that the order of the modifiers final and static makes no differencethough you should use a consistent order. We have already accessed static fields in all of the preceding examples, as you may have realizedout is a static field of class System. Groups of named constants, like the suits, can often be better represented as the members of an enumeration type, or enum for short. An enum is a special class with predefined instances for each of the named constants the enum represents. For example, we can rewrite our suit example using an enum: enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }
Each enum constant is a static field that refers to the object for that valuesuch as Suit.HEARTS. By representing each named constant as an object rather than just an integer value, you improve the typesafety and so the robustness, of your program. Enums are covered in detail in Chapter 6. Exercise 1.5: Change the HelloWorld application to use a named string constant as the string to print. (A string constant can be initialized with a string literal.) Exercise 1.6: Change your program from Exercise 1.3 to use a named string constant for the title.
file:///D|/1/0321349806/ch01lev1sec4.html (2 von 2) [11.01.2008 03:36:21]
Section 1.5. Unicode Characters
1.5. Unicode Characters Suppose we were defining a class that dealt with circles and we wanted a named constant that represented the value π. In most programming languages we would name the constant "pi" because in most languages identifiers (the technical term for names) are limited to the letters and digits available in the ASCII character set. In the Java programming language, however, we can do this: class Circle { static final double π = 3.14159265358979323846; // ... }
The Java programming language moves you toward the world of internationalized software: you write [1] code in Unicode, an international character set standard. Unicode basic characters are 16 bits, and together with the supplemental characters (21 bits) provide a character range large enough to write the major languages used in the world. That is why we can use π for the name of the constant in the example. π is a valid letter from the Greek section of Unicode and is therefore valid in source. Most existing code is typed in ASCII, a 7-bit character standard, or ISO Latin-1, an 8-bit character standard commonly called Latin-1. But these characters are translated into Unicode before processing, so the character set is always Unicode. [1]
Basic Multilingual Plane, or BMP, in Unicode terminology.
file:///D|/1/0321349806/ch01lev1sec5.html [11.01.2008 03:36:21]
Section 1.6. Flow of Control
1.6. Flow of Control "Flow of control" is the term for deciding which statements in a program are executed and in what order. The while loop in the Fibonacci program is one control flow statement, as are blocks, which define a sequential execution of the statements they group. Other control flow statements include for, ifelse, switch, and dowhile. We change the Fibonacci sequence program by numbering the elements of the sequence and marking even numbers with an asterisk: class ImprovedFibonacci { static final int MAX_INDEX = 9; /** * Print out the first few Fibonacci numbers, * marking evens with a '*' */ public static void main(String[] args) { int lo = 1; int hi = 1; String mark; System.out.println("1: " + lo); for (int i = 2; i