The Java™ Language Specification Second Edition
Total Page:16
File Type:pdf, Size:1020Kb
diffs.book Page i Thursday, March 2, 2000 3:58 PM The Java™ Language Specification Second Edition DRAFT diffs.book Page ii Thursday, March 2, 2000 3:58 PM 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-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 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 TheDRAFT Java™ FAQ: Frequently Asked Questions Jonni Kanerva ISBN 0-201-63456-2 diffs.book Page iii Thursday, March 2, 2000 3:58 PM The Java™ Language Specification Second Edition James Gosling Bill Joy Guy Steele Gilad Bracha DRAFT ADDISON-WESLEY An imprint of Addison Wesley Longman, Inc. diffs.book Page iv Thursday, March 2, 2000 3:58 PM iv THE JAVA LANGUAGE SPECIFICATION Reading, Massachusetts ● Harlow, England ● Menlo Park, California Berkeley, California ● Don Mills, Ontario ● Sydney Bonn ● Amsterdam ● Tokyo ● Mexico City DRAFT diffs.book Page v Thursday, March 2, 2000 3:58 PM 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. Sun Microsystems, Inc has intellectual property rights relating to implementations of the technology described in this publication. In particular, and without limitation, these intel- lectual property rights may include one or more U.S. patents, foreign patents, or pending applications. Sun, Sun Microsystems, Sun Microsystems Computer Corporation, the Sun logo, the Sun Microsystems Computer Corporation logo, Solaris, Java, JavaSoft, JavaS- cript, HotJava, JDK, and all Java-based trademarks or logos are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. 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. U.S. GOVERNMENT USE: This specification relates to commercial items, processes or software. Accordingly, use by the United States Government is subject to these terms and conditions, consistent with FAR 12.211 and 12.212. THIS IS A DRAFT SPECIFICATION FOR COMMUNITY REVIEW AND MAY CHANGE ACCORDINGLY. YOUR COMMENTS ARE ENCOURAGED, BUT MUST BE PROVIDED WITHOUT RESTRICTION. 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 ANY TECHNOLOGY, PRODUCT, OR PROGRAM 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 PM vi / diffs.book Page ix Thursday, March 2, 2000 3:58 PM Table of Contents Table of Contents . ix Series Foreword . xxi Preface . 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. 15 3.1 Unicode. 15 3.2 Lexical Translations . 16 3.3 Unicode Escapes. 16 3.4 Line Terminators . 17 3.5 Input Elements and Tokens. 18 3.6 White Space . 19 3.7 Comments. 19 3.8 Identifiers . 21 3.9 Keywords . 22 3.10 Literals . 23 3.10.1 Integer Literals . 23 DRAFT3.10.2 Floating-Point Literals . 26 3.10.3 Boolean Literals . 27 3.10.4 Character Literals. 28 3.10.5 String Literals. 29 3.10.6 Escape Sequences for Character and String Literals . 31 ix diffs.book Page x Thursday, March 2, 2000 3:58 PM TABLE OF CONTENTS 3.10.7 The Null Literal. 31 3.11 Separators . 32 3.12 Operators . 32 4 Types, Values, and Variables. 35 4.1 The Kinds of Types and Values . 36 4.2 Primitive Types and Values . 36 4.2.1 Integral Types and Values . 37 4.2.2 Integer Operations . 37 4.2.3 Floating-Point Types, Formats, and Values. 39 4.2.4 Floating-Point Operations . 41 4.2.5 The boolean Type and boolean Values . .44 4.3 Reference Types and Values . 45 4.3.1 Objects. 45 4.3.2 The Class Object . .48 4.3.3 The Class String . .49 4.3.4 When Reference Types Are the Same. 49 4.4 Where Types Are Used . 49 4.5 Variables . 51 4.5.1 Variables of Primitive Type. 51 4.5.2 Variables of Reference Type . 51 4.5.3 Kinds of Variables. 52 4.5.4 final Variables. 53 4.5.5 Initial Values of Variables . 54 4.5.6 Types, Classes and Interfaces . 55 5 Conversions and Promotions . 59 5.1 Kinds of Conversion . 62 5.1.1 Identity Conversions . 62 5.1.2 Widening Primitive Conversion . 62 5.1.3 Narrowing Primitive Conversions . 63 5.1.4 Widening Reference Conversions . 66 5.1.5 Narrowing Reference Conversions . 67 5.1.6 String Conversions . 68 5.1.7 Forbidden Conversions . 68 5.1.8 Value Set Conversion . 69 5.2 Assignment Conversion. 70 5.3 Method Invocation Conversion . 75 5.4 String Conversion . 76 5.5 Casting Conversion . 76 5.6 Numeric Promotions . 82 5.6.1 Unary Numeric Promotion . 82 DRAFT5.6.2 Binary Numeric Promotion . 83 6 Names . 87 6.1 Declarations . ..