Second Edition - Java Community Process

Transcription

diffs.book Page i Thursday, March 2, 2000 3:58 PMFTThe Java Language SpecificationDRASecond Edition

diffs.book Page ii Thursday, March 2, 2000 3:58 PMThe Java SeriesLisa Friendly, Series EditorBill Joy, Technical AdvisorFTThe Java Programming LanguageKen Arnold and James GoslingISBN 0-201-63455-4The Java Language Specification, Second EditionJames Gosling, Bill Joy, Guy Steele and Gilad BrachaISBN 0-201-31008-2RAThe Java Virtual Machine Specification, Second EditionTim Lindholm and Frank YellinISBN 0-201-63452-XThe Java Application Programming Interface,Volume 1: Core PackagesJames Gosling, Frank Yellin, and the Java TeamISBN 0-201-43294-3The Java Application Programming Interface,Volume 2: Window Toolkit and AppletsJames Gosling, Frank Yellin, and the Java TeamISBN 0-201-63459-7DThe Java Tutorial: Object-Oriented Programming for the InternetMary Campione and Kathy WalrathISBN 0-201-63454-6The Java Class Libraries: An Annotated ReferencePatrick Chan and Rosanna LeeISBN 0-201-63458-9The Java FAQ: Frequently Asked QuestionsJonni KanervaISBN 0-201-63456-2

diffs.book Page iii Thursday, March 2, 2000 3:58 PMFTThe Java Language SpecificationRASecond EditionDJames GoslingBill JoyGuy SteeleGilad BrachaADDISON-WESLEYAn imprint of Addison Wesley Longman, Inc.

diffs.book Page iv Thursday, March 2, 2000 3:58 PMivTHE JAVA LANGUAGE SPECIFICATIONReading, Massachusetts Harlow, England Menlo Park, CaliforniaBerkeley, California Don Mills, Ontario SydneyBonn Amsterdam Tokyo Mexico City DRAFT

diffs.book Page v Thursday, March 2, 2000 3:58 PMCopyright 1996-2000 Sun Microsystems, Inc.901 San Antonio Road, Mountain View, California 94303 U.S.A.All rights reserved.Duke logo designed by Joe Palrang.Sun Microsystems, Inc has intellectual property rights relating to implementations of thetechnology described in this publication. In particular, and without limitation, these intellectual property rights may include one or more U.S. patents, foreign patents, or pendingapplications. Sun, Sun Microsystems, Sun Microsystems Computer Corporation, the Sunlogo, the Sun Microsystems Computer Corporation logo, Solaris, Java, JavaSoft, JavaScript, HotJava, JDK, and all Java-based trademarks or logos are trademarks or registeredtrademarks of Sun Microsystems, Inc. in the United States and other countries. UNIX isa registered trademark in the United States and other countries, exclusively licensedthrough 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.U.S. GOVERNMENT USE: This specification relates to commercial items, processes orsoftware. Accordingly, use by the United States Government is subject to these terms andconditions, consistent with FAR 12.211 and 12.212.THIS IS A DRAFT SPECIFICATION FOR COMMUNITY REVIEW AND MAYCHANGE ACCORDINGLY. YOUR COMMENTS ARE ENCOURAGED, BUT MUSTBE PROVIDED WITHOUT RESTRICTION.THIS PUBLICATION IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANYKIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THEIMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULARPURPOSE, 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 NEWEDITIONS OF THE PUBLICATION. SUN MICROSYSTEMS, INC. MAY MAKEIMPROVEMENTS AND/OR CHANGES IN ANY TECHNOLOGY, PRODUCT, ORPROGRAM DESCRIBED IN THIS PUBLICATION AT ANY TIME.Credits and permissions for quoted material appear in a separate section on page 519.

diffs.book Page vi Thursday, March 2, 2000 3:58 PMvi/

diffs.book Page ix Thursday, March 2, 2000 3:58 PMTable of ContentsFTTable of Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ixSeries Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiPreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii1 Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Example Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Relationship to Predefined Classes and Interfaces . . . . . . . . . . . . . . . . . . . .References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .RA1.11.21.31.455662 Grammars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92.12.22.32.4Context-Free Grammars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9The Lexical Grammar. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9The Syntactic Grammar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Grammar Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Lexical Structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Unicode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Lexical Translations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Unicode Escapes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Line Terminators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Input Elements and Tokens. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .White Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.10.1 Integer Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.10.2 Floating-Point Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.10.3 Boolean Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.10.4 Character Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.10.5 String Literals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.10.6 Escape Sequences for Character and String Literals . . . . . . . . . 223232627282931ix

diffs.book Page x Thursday, March 2, 2000 3:58 PMTABLE OF CONTENTS3.113.123.10.7 The Null Literal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Separators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 Types, Values, and Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354.3RA4.44.5The Kinds of Types and Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36Primitive Types and Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364.2.1Integral Types and Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374.2.2Integer Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374.2.3Floating-Point Types, Formats, and Values. . . . . . . . . . . . . . . . . . 394.2.4Floating-Point Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414.2.5The boolean Type and boolean Values . . . . . . . . . . . . . . 44Reference Types and Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454.3.1Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454.3.2The Class Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484.3.3The Class String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494.3.4When Reference Types Are the Same . . . . . . . . . . . . . . . . . . . . . . 49Where Types Are Used . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514.5.1Variables of Primitive Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514.5.2Variables of Reference Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514.5.3Kinds of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524.5.4final Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534.5.5Initial Values of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544.5.6Types, Classes and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55FT4.14.25 Conversions and Promotions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59D5.15.25.35.45.55.6Kinds of Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625.1.1Identity Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625.1.2Widening Primitive Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . 625.1.3Narrowing Primitive Conversions . . . . . . . . . . . . . . . . . . . . . . . . . 635.1.4Widening Reference Conversions . . . . . . . . . . . . . . . . . . . . . . . . . 665.1.5Narrowing Reference Conversions . . . . . . . . . . . . . . . . . . . . . . . . 675.1.6String Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685.1.7Forbidden Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685.1.8Value Set Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69Assignment Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70Method Invocation Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75String Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76Casting Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76Numeric Promotions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825.6.1Unary Numeric Promotion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825.6.2Binary Numeric Promotion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836 Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 876.1xDeclarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

diffs.book Page xi Thursday, March 2, 2000 3:58 PMTABLE OF CONTENTS6.26.36.4RAFT6.5Names and Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89Scope of a Simple Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 916.3.1Hiding Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94Members and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 976.4.1The Members of a Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 986.4.2The Members of a Class Type . . . . . . . . . . . . . . . . . . . . . . . . . . . 986.4.3The Members of an Interface Type . . . . . . . . . . . . . . . . . . . . . . 1006.4.4The Members of an Array Type . . . . . . . . . . . . . . . . . . . . . . . . . 100Determining the Meaning of a Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1016.5.1Syntactic Classification of a Name According to Context. . . . . 1026.5.2Reclassification of Contextually Ambiguous Names. . . . . . . . . 1046.5.3Meaning of Package Names. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1066.5.3.1Simple Package Names . . . . . . . . . . . . . . . . . . . . . . 1066.5.3.2Qualified Package Names. . . . . . . . . . . . . . . . . . . . . 1066.5.4Meaning of PackageOrTypeNames . . . . . . . . . . . . . . . . . . . . . . 1066.5.4.1Simple PackageOrTypeNames . . . . . . . . . . . . . . . . . 1066.5.4.2Qualified PackageOrTypeNames . . . . . . . . . . . . . . . 1066.5.5Meaning of Type Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1076.5.5.1Simple Type Names . . . . . . . . . . . . . . . . . . . . . . . . . 1076.5.5.2Qualified Type Names . . . . . . . . . . . . . . . . . . . . . . . 1086.5.6Meaning of Expression Names . . . . . . . . . . . . . . . . . . . . . . . . . 1086.5.6.1Simple Expression Names . . . . . . . . . . . . . . . . . . . . 1086.5.6.2Qualified Expression Names . . . . . . . . . . . . . . . . . . 1106.5.7Meaning of Method Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1116.5.7.1Simple Method Names . . . . . . . . . . . . . . . . . . . . . . . 1116.5.7.2Qualified Method Names . . . . . . . . . . . . . . . . . . . . . 112Qualified Names and Access Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1126.6.1Determining Accessibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1126.6.2Details on protected Access . . . . . . . . . . . . . . . . . . . . . . . . . 1136.6.2.1Qualified Access to a protected Member . . . . . . . 1136.6.2.2Qualified Access to a protected Constructor. . . . . 1136.6.3An Example of Access Control . . . . . . . . . . . . . . . . . . . . . . . . . 1146.6.4Example: Access to public and Non-public Classes . . . . . . 1156.6.5Example: Default-Access Fields, Methods, and Constructors. . 1166.6.6Example: public Fields, Methods, and Constructors . . . . . . . 1176.6.7Example: protected Fields, Methods, and Constructors . . . . 1176.6.8Example: private Fields, Methods, and Constructors . . . . . . 118Fully Qualified Names and Canonical Names . . . . . . . . . . . . . . . . . . . . . 119Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1216.8.1Package Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1216.8.2Class and Interface Type Names . . . . . . . . . . . . . . . . . . . . . . . . 1226.8.3Method Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1226.8.4Field Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1236.8.5Constant Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1246.8.6Local Variable and Parameter Names . . . . . . . . . . . . . . . . . . . . 124D6.66.76.87 Packages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1277.1Package Members. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127xi

diffs.book Page xii Thursday, March 2, 2000 3:58 PMTABLE OF CONTENTS7.37.47.57.67.7Host Support for Packages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1287.2.1Storing Packages in a File System . . . . . . . . . . . . . . . . . . . . . . . 1297.2.2Storing Packages in a Database. . . . . . . . . . . . . . . . . . . . . . . . . . 131Compilation Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131Package Declarations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1327.4.1Named Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1327.4.2Unnamed Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1327.4.3Accessibility of a Package. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1337.4.4Scope of a Package Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133Import Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1337.5.1Single-Type-Import Declaration . . . . . . . . . . . . . . . . . . . . . . . . . 1347.5.2Type-Import-on-Demand Declaration . . . . . . . . . . . . . . . . . . . . . 1357.5.3Automatic Imports. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1367.5.4A Strange Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136Top level Type Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137Unique Package Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140FT7.28 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143Class Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1448.1.1Class Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1458.1.1.1abstract Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . 1458.1.1.2final Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1478.1.1.3strictfp Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 1488.1.2Inner Classes and Enclosing Instances . . . . . . . . . . . . . . . . . . . . 1488.1.3Superclasses and Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1508.1.4Superinterfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1528.1.5Class Body and Member Declarations . . . . . . . . . . . . . . . . . . . . 155Class Members. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1568.2.1Examples of Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1578.2.1.1Example: Inheritance with Default Access . . . . . . . . 1578.2.1.2Inheritance with public and protected . . . . . . . . . 1588.2.1.3Inheritance with private . . . . . . . . . . . . . . . . . . . . . 1598.2.1.4Accessing Members of Inaccessible Classes. . . . . . . 159Field Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1618.3.1Field Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1638.3.1.1static Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163final Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1648.3.1.28.3.1.3transient Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . 1648.3.1.4volatile Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1658.3.2Initialization of Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1668.3.2.1Initializers for Class Variables . . . . . . . . . . . . . . . . . . 1678.3.2.2Initializers for Instance Variables . . . . . . . . . . . . . . . 1688.3.3Examples of Field Declarations . . . . . . . . . . . . . . . . . . . . . . . . . 1688.3.3.1Example: Hiding of Class Variables . . . . . . . . . . . . . 1698.3.3.2Example: Hiding of Instance Variables . . . . . . . . . . . 1698.3.3.3Example: Multiply Inherited Fields. . . . . . . . . . . . . . 1718.3.3.4Example: Re-inheritance of Fields . . . . . . . . . . . . . . 172Method Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173RA8.18.2D8.38.4xii

diffs.book Page xiii Thursday, March 2, 2000 3:58 PMTABLE OF CONTENTSFormal Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Method Signature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Method Modifiers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8.4.3.1abstract Methods . . . . . . . . . . . . . . . . . . . . . . . . .8.4.3.2static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . .8.4.3.3final Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . .8.4.3.4native Methods . . . . . . . . . . . . . . . . . . . . . . . . . . .8.4.3.5strictfp Methods . . . . . . . . . . . . . . . . . . . . . . . . .8.4.3.6synchronized Methods . . . . . . . . . . . . . . . . . . . . .8.4.4Method Throws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8.4.5Method Body . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8.4.6Inheritance, Overriding, and Hiding . . . . . . . . . . . . . . . . . . . . .8.4.6.1Overriding (by Instance Methods) . . . . . . . . . . . . . .8.4.6.2Hiding (by Class Methods). . . . . . . . . . . . . . . . . . . .8.4.6.3Requirements in Overriding and Hiding . . . . . . . . .8.4.6.4Inheriting Methods with the Same Signature . . . . . .8.4.7Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8.4.8Examples of Method Declarations . . . . . . . . . . . . . . . . . . . . . . .8.4.8.1Example: Overriding . . . . . . . . . . . . . . . . . . . . . . . .8.4.8.2Example: Overloading, Overriding, and Hiding . . .8.4.8.3Example: Incorrect Overriding. . . . . . . . . . . . . . . . .8.4.8.4Example: Overriding versus Hiding . . . . . . . . . . . . .8.4.8.5Example: Invocation of Hidden Class Methods . . . .8.4.8.6Large Example of Overriding. . . . . . . . . . . . . . . . . .8.4.8.7Example: Incorrect Overriding because of Throws .Member Type Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8.5.1Access Modifers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8.5.2Static Member Type Declarations . . . . . . . . . . . . . . . . . . . . . . .Instance Initializers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Static Initializers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Constructor Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8.8.1Formal Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8.8.2Constructor Signature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8.8.3Constructor Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8.8.4Constructor Throws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8.8.5Constructor Body . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8.8.5.1Explicit Constructor Invocations . . . . . . . . . . . . . . .8.8.6Constructor Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8.8.7Default Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8.8.8Preventing Instantiation of a Class. . . . . . . . . . . . . . . . . . . . . . 8.89 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2079.1Interface Declarations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9.1.1Interface Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9.1.1.1abstract Interfaces . . . . . . . . . . . . . . . . . . . . . . . .9.1.1.2strictfp Interfaces . . . . . . . . . . . . . . . . . . . . . . . . .9.1.2Superinterfaces and Subinterfaces . . . . . . . . . . . . . . . . . . . . . . .9.1.3Interface Body and Member Declarations . . . . . . . . . . . . . . . . .208208208209209210xiii

diffs.book Page xiv Thursday, March 2, 2000 3:58 PMTABLE OF CONTENTS9.49.59.1.4Access to Interface Member Names . . . . . . . . . . . . . . . . . . . . . . 210Interface Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210Field (Constant) Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2119.3.1Initialization of Fields in Interfaces. . . . . . . . . . . . . . . . . . . . . . . 2129.3.2Examples of Field Declarations . . . . . . . . . . . . . . . . . . . . . . . . . 2129.3.2.1Ambiguous Inherited Fields . . . . . . . . . . . . . . . . . . . 2129.3.2.2Multiply Inherited Fields . . . . . . . . . . . . . . . . . . . . . . 213Abstract Method Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2139.4.1Inheritance and Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2149.4.2Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2159.4.3Examples of Abstract Method Declarations . . . . . . . . . . . . . . . . 2159.4.3.1Example: Overriding . . . . . . . . . . . . . . . . . . . . . . . . . 2159.4.3.2Example: Overloading . . . . . . . . . . . . . . . . . . . . . . . . 216Member Type Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216FT9.29.310 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219Array Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220Array Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220Array Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221Array Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221Arrays: A Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222Array Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222Array Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223Class Objects for Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225An Array of Characters is Not a String . . . . . . . . . . . . . . . . . . 225Array Store Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226RA10.110.210.310.410.510.610.710.810.910.1011 Exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22911.111.2D11.311.411.5The Causes of Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230Compile-Time Checking of Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . 23111.2.1 Why Errors are Not Checked . . . . . . . . . . . . . . . . . . . . . . . . . . . 23111.2.2 Why Runtime Exceptions are Not Checked . . . . . . . . . . . . . . . . 232Handling of an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23211.3.1 Exceptions are Precise. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23311.3.2 Handling Asynchronous Exceptions . . . . . . . . . . . . . . . . . . . . . . 234An Example of Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234The Exception Hierarchy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23611.5.0.1 Loading and Linkage Errors . . . . . . . . . . . . . . . . . . . 23711.5.0.2 Virtual Machine Errors . . . . . . . . . . . . . . . . . . . . . . . 23712 Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24312.1xivVirtual Machine Start-Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24312.1.1 Load the Class Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24412.1.2 Link Test: Verify, Prepare, (Optionally) Resolve . . . . . . . . . . . 24412.1.3 Initialize Test: Execute Initializers . . . . . . . . . . . . . . . . . . . . . . 24512.1.4 Invoke Test.main. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246

diffs.book Page xv Thursday, March 2, 2000 3:58 PMTABLE OF CONTENTS12.312.412.512.612.712.8Loading of Classes and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12.2.1 The Loading Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Linking of Classes and Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12.3.1 Verification of the Binary Representation . . . . . . . . . . . . . . . . .12.3.2 Preparation of a Class or Interface Type . . . . . . . . . . . . . . . . . .12.3.3 Resolution of Symbolic References . . . . . . . . . . . . . . . . . . . . . .Initialization of Classes and Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . .12.4.1 When Initialization Occurs . . . . . . . . . . . . . . . . . . . . . . . . . . . .12.4.2 Detailed Initialization Procedure . . . . . . . . . . . . . . . . . . . . . . . .12.4.3 Initialization: Implications for Code Generation . . . . . . . . . . . .Creation of New Class Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Finalization of Class Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12.6.1 Implementing Finalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12.6.2 Finalizer Invocations are Not Ordered . . . . . . . . . . . . . . . . . . . .Unloading of Classes and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . .Program Exit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T12.213 Binary Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263The Form of a Binary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .What Binary Compatibility Is and Is Not. . . . . . . . . . . . . . . . . . . . . . . . .Evolution of Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Evolution of Classes . . . . . . . . . . . . . . . . . . . . . . . . . .

The Java Virtual Machine Specification, Second Edition Tim Lindholm and Frank Yellin ISBN 0-201-63452-X The Java Application Programming Interface, Volume 1: Core Packages James Gosling, Frank Yellin, and the Java Team ISBN 0-201-43294-3 The Java Application Pr