jsp(3)-1-10

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

The Java™

Language Specification
Second Edition
The Java™ Series
Lisa Friendly, Series Editor
Bill Joy, Technical Advisor

The Java™ Programming Language


Ken Arnold and James Gosling
ISBN 0-201-63455-4

The Java™ Language Specification Second Edition


James Gosling, Bill Joy, Guy Steele and Gilad Bracha
ISBN 0-201-31008-2

The Java™ Virtual Machine Specification Second Edition


Tim Lindholm and Frank Yellin
ISBN 0-201-43294-3

The Java™ Application Programming Interface,


Volume 1: Core Packages
James Gosling, Frank Yellin, and the Java Team
ISBN 0-201-63452-X

The Java™ Application Programming Interface,


Volume 2: Window Toolkit and Applets
James Gosling, Frank Yellin, and the Java Team
ISBN 0-201-63459-7

The Java™ Tutorial: Object-Oriented Programming for the Internet


Mary Campione and Kathy Walrath
ISBN 0-201-63454-6

The Java™ Class Libraries: An Annotated Reference


Patrick Chan and Rosanna Lee
ISBN 0-201-63458-9

The Java™ FAQ: Frequently Asked Questions


Jonni Kanerva
ISBN 0-201-63456-2
The Java™
Language Specification
Second Edition
James Gosling
Bill Joy
Guy Steele
Gilad Bracha

ADDISON-WESLEY
An imprint of Addison Wesley Longman, Inc.
Reading, Massachusetts Harlow, England Menlo Park, California
● ●

Berkeley, California Don Mills, Ontario Sydney


● ●

Bonn Amsterdam Tokyo Mexico City


● ● ●
Copyright  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.
RESTRICTED RIGHTS LEGEND: 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 manual 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, per-
petual, 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 cre-
ation and distribution of clean room implementations of this specification that: (i) include a com-
plete implementation of the current version of this specification without subsetting or supersetting;
(ii) implement all the interfaces and functionality of the required packages of the Java™ 2 Platform,
Standard Edition, as defined by SUN, without subsetting or supersetting; (iii) do not add any addi-
tional packages, classes, or interfaces to the java.* or javax.* packages or their subpackages; (iv)
pass all test suites relating to the most recent published version of the specification of the Java™ 2
Platform, Standard Edition, 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 source code or binary materials without an appro-
priate and separate license from SUN.
Sun, Sun Microsystems, Sun Microsystems Computer Corporation, the Sun logo, the Sun
Microsystems Computer Corporation logo, Solaris, Java, JavaSoft, JavaScript, HotJava,
JDK, and all Java-based trademarks or logos 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 TYPO-
GRAPHICAL ERRORS. CHANGES ARE PERIODICALLY ADDED TO THE INFOR-
MATION 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 PRO-
GRAM(S) DESCRIBED IN THIS PUBLICATION AT ANY TIME.
Credits and permissions for quoted material appear in a separate section on page 503.

Text printed on recycled and acid-free paper


ISBN 0-201-31008-2
1 2 3 4 5 6 7 8 9-MA-99989796
First printing, June 2000
Table of Contents

Table of Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii

Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix

Preface to the Second Edition . . . . . . . . . . . . . . . . . . . . . . . . . xxiii

1 Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Example Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2 Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 Relationship to Predefined Classes and Interfaces . . . . . . . . . . . . . . . . . . . . 6
1.4 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

2 Grammars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.1 Context-Free Grammars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.2 The Lexical Grammar. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.3 The Syntactic Grammar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.4 Grammar Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

3 Lexical Structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.1 Unicode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2 Lexical Translations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.3 Unicode Escapes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.4 Line Terminators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.5 Input Elements and Tokens. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.6 White Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.7 Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.8 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.9 Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.10 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.10.1 Integer Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.10.2 Floating-Point Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.10.3 Boolean Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.10.4 Character Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.10.5 String Literals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.10.6 Escape Sequences for Character and String Literals . . . . . . . . . . 28
3.10.7 The Null Literal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
vii
TABLE OF CONTENTS

3.11 Separators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.12 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

4 Types, Values, and Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31


4.1 The Kinds of Types and Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
4.2 Primitive Types and Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
4.2.1 Integral Types and Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
4.2.2 Integer Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
4.2.3 Floating-Point Types, Formats, and Values. . . . . . . . . . . . . . . . . . 35
4.2.4 Floating-Point Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
4.2.5 The boolean Type and boolean Values. . . . . . . . . . . . . . . . . . . . 40
4.3 Reference Types and Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.3.1 Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.3.2 The Class Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
4.3.3 The Class String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.3.4 When Reference Types Are the Same . . . . . . . . . . . . . . . . . . . . . . 45
4.4 Where Types Are Used . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
4.5 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
4.5.1 Variables of Primitive Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
4.5.2 Variables of Reference Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
4.5.3 Kinds of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
4.5.4 final Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
4.5.5 Initial Values of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
4.5.6 Types, Classes, and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

5 Conversions and Promotions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55


5.1 Kinds of Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
5.1.1 Identity Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
5.1.2 Widening Primitive Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . 58
5.1.3 Narrowing Primitive Conversions . . . . . . . . . . . . . . . . . . . . . . . . . 59
5.1.4 Widening Reference Conversions . . . . . . . . . . . . . . . . . . . . . . . . . 62
5.1.5 Narrowing Reference Conversions . . . . . . . . . . . . . . . . . . . . . . . . 63
5.1.6 String Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
5.1.7 Forbidden Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
5.1.8 Value Set Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
5.2 Assignment Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
5.3 Method Invocation Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
5.4 String Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
5.5 Casting Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
5.6 Numeric Promotions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
5.6.1 Unary Numeric Promotion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
5.6.2 Binary Numeric Promotion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

6 Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
6.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

viii
TABLE OF CONTENTS

6.2 Names and Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83


6.3 Scope of a Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
6.3.1 Shadowing Declarations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
6.3.2 Obscured Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
6.4 Members and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
6.4.1 The Members of a Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
6.4.2 The Members of a Class Type . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
6.4.3 The Members of an Interface Type . . . . . . . . . . . . . . . . . . . . . . . 91
6.4.4 The Members of an Array Type . . . . . . . . . . . . . . . . . . . . . . . . . . 92
6.5 Determining the Meaning of a Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
6.5.1 Syntactic Classification of a Name According to Context. . . . . . 94
6.5.2 Reclassification of Contextually Ambiguous Names. . . . . . . . . . 96
6.5.3 Meaning of Package Names. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
6.5.3.1 Simple Package Names . . . . . . . . . . . . . . . . . . . . . . . 98
6.5.3.2 Qualified Package Names. . . . . . . . . . . . . . . . . . . . . . 98
6.5.4 Meaning of PackageOrTypeNames . . . . . . . . . . . . . . . . . . . . . . . 98
6.5.4.1 Simple PackageOrTypeNames . . . . . . . . . . . . . . . . . . 98
6.5.4.2 Qualified PackageOrTypeNames . . . . . . . . . . . . . . . . 99
6.5.5 Meaning of Type Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
6.5.5.1 Simple Type Names . . . . . . . . . . . . . . . . . . . . . . . . . . 99
6.5.5.2 Qualified Type Names . . . . . . . . . . . . . . . . . . . . . . . 100
6.5.6 Meaning of Expression Names . . . . . . . . . . . . . . . . . . . . . . . . . 100
6.5.6.1 Simple Expression Names . . . . . . . . . . . . . . . . . . . . 101
6.5.6.2 Qualified Expression Names . . . . . . . . . . . . . . . . . . 102
6.5.7 Meaning of Method Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
6.5.7.1 Simple Method Names . . . . . . . . . . . . . . . . . . . . . . . 104
6.5.7.2 Qualified Method Names . . . . . . . . . . . . . . . . . . . . . 104
6.6 Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
6.6.1 Determining Accessibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
6.6.2 Details on protected Access . . . . . . . . . . . . . . . . . . . . . . . . . . 105
6.6.2.1 Access to a protected Member . . . . . . . . . . . . . . . 106
6.6.2.2 Qualified Access to a protected Constructor. . . . . 106
6.6.3 An Example of Access Control . . . . . . . . . . . . . . . . . . . . . . . . . 106
6.6.4 Example: Access to public and Non-public Classes . . . . . . . 107
6.6.5 Example: Default-Access Fields, Methods, and Constructors. . 108
6.6.6 Example: public Fields, Methods, and Constructors . . . . . . . . 109
6.6.7 Example: protected Fields, Methods, and Constructors . . . . . 110
6.6.8 Example: private Fields, Methods, and Constructors . . . . . . . 111
6.7 Fully Qualified Names and Canonical Names . . . . . . . . . . . . . . . . . . . . . 111
6.8 Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
6.8.1 Package Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
6.8.2 Class and Interface Type Names . . . . . . . . . . . . . . . . . . . . . . . . 114
6.8.3 Method Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
6.8.4 Field Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
6.8.5 Constant Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
6.8.6 Local Variable and Parameter Names . . . . . . . . . . . . . . . . . . . . 116

ix
TABLE OF CONTENTS

7 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
7.1 Package Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
7.2 Host Support for Packages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
7.2.1 Storing Packages in a File System . . . . . . . . . . . . . . . . . . . . . . . 121
7.2.2 Storing Packages in a Database. . . . . . . . . . . . . . . . . . . . . . . . . . 122
7.3 Compilation Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
7.4 Package Declarations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
7.4.1 Named Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
7.4.2 Unnamed Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
7.4.3 Observability of a Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
7.4.4 Scope of a Package Declaration . . . . . . . . . . . . . . . . . . . . . . . . . 125
7.5 Import Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
7.5.1 Single-Type-Import Declaration . . . . . . . . . . . . . . . . . . . . . . . . . 126
7.5.2 Type-Import-on-Demand Declaration . . . . . . . . . . . . . . . . . . . . . 127
7.5.3 Automatic Imports. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
7.5.4 A Strange Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
7.6 Top Level Type Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
7.7 Unique Package Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132

8 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
8.1 Class Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
8.1.1 Class Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
8.1.1.1 abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
8.1.1.2 final Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
8.1.1.3 strictfp Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
8.1.2 Inner Classes and Enclosing Instances . . . . . . . . . . . . . . . . . . . . 140
8.1.3 Superclasses and Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
8.1.4 Superinterfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
8.1.5 Class Body and Member Declarations . . . . . . . . . . . . . . . . . . . . 147
8.2 Class Members. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
8.2.1 Examples of Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
8.2.1.1 Example: Inheritance with Default Access . . . . . . . . 149
8.2.1.2 Inheritance with public and protected . . . . . . . . . 150
8.2.1.3 Inheritance with private . . . . . . . . . . . . . . . . . . . . . 151
8.2.1.4 Accessing Members of Inaccessible Classes. . . . . . . 151
8.3 Field Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
8.3.1 Field Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
8.3.1.1 static Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
8.3.1.2 final Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
8.3.1.3 transient Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
8.3.1.4 volatile Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
8.3.2 Initialization of Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
8.3.2.1 Initializers for Class Variables . . . . . . . . . . . . . . . . . . 159
8.3.2.2 Initializers for Instance Variables . . . . . . . . . . . . . . . 159
8.3.2.3 Restrictions on the use of Fields during Initialization160
8.3.3 Examples of Field Declarations . . . . . . . . . . . . . . . . . . . . . . . . . 162
8.3.3.1 Example: Hiding of Class Variables . . . . . . . . . . . . . 162

You might also like