HAPPY BOOKSGIVING
Use code BOOKSGIVING during checkout to save 40%-55% on books and eBooks. Shop now.
Register your product to gain access to bonus material or receive a coupon.
Second Edition now available! |
The book specifies all of Java's syntax and semantics. It contains both lexical and syntactic grammars for the language, including a LALR(1) grammar. It describes all aspects of the language as checked by a Java compiler, including the semantics of all types, statements, and expressions. It also covers all aspects of the Java execution model, including exceptions, threads, and binary compatibility. In addition, the book gives specifications for all the types defined in the core packages of Java's Application Programming Interface (API): java.lang, java.io, and java.util.
0201634511B04062001
1. Introduction.
Example Programs.
References.
Other Books in The Java Series.
Context-Free Grammars.
The Lexical Grammar.
The Syntactic Grammar.
Grammar Notation.
Unicode.
Lexical Translations.
Unicode Escapes.
Line Terminators.
Input Elements and Tokens.
White Space.
Comments.
Keywords.
Identifiers.
Literals.
Separators.
Operators.
Primitive Types and Reference Types.
Primitive Types and Values.
Reference Types, Objects, and Reference Values.
When Types Are the Same.
Where Types Are Used.
Variables.
Contexts for Conversions.
Conversions of Primitive Values.
Assignment Conversion.
Method Call Conversion.
Casting Conversions.
Numeric Promotions.
Declarations.
Names.
Scope and Hiding of a Simple Name.
Members and Inheritance.
Qualified Names and Access Control.
Determining the Meaning (Denotation) of a Name.
Fully Qualified Names.
Naming Conventions.
Type Evolution and Name Conflicts.
Packages.
Host Support for Packages.
Compilation Units.
Package Declarations.
Import Declarations.
Type Declarations.
Avoiding Package Name Conflicts.
Class Declarations.
The Class Members.
Field Declarations.
Method Declarations.
Static Initializers.
Constructor Declarations.
Interface Declarations.
The Interface Members.
Field (Constant) Declarations.
Method (Signature) Declarations.
Implementing Interfaces which Are Not Public.
An Example of Interfaces.
Array Basics.
Array Types.
Array Variables.
Array Creation.
Array Access.
A Simple Example.
Array Initializers.
Array Members.
Arrays and Clones.
Array Store Exception.
Additional Array Examples.
Arrays of Characters and Strings.
The Causes of Exceptions.
Compile-Time Checking of Exceptions.
Handling of an Exception.
An Example of an Exception.
The Exception Hierarchy.
Virtual Machine Startup.
Class and Interface Loading and Linking.
Class Initialization.
Creation of New Class Instances.
Calling Methods of Objects.
Cloning Objects.
Finalization of Class Instances.
Class Finalization.
Class Unloading.
Virtual Machine Exit.
Normal and Abrupt Completion of Statements.
Blocks.
Local Variable Declaration Statements.
Statements.
The Empty Statement.
Labeled Statements.
Expression Statements.
The if Statement.
The switch Statement.
The while Statement.
The do Statement.
The for Statement.
The break Statement.
The continue Statement.
The return Statement.
The throw Statement.
The synchronized Statement.
The try statement.
Unreachable Statements.
Evaluation, Denotation, and Result.
Variables as Values.
Compile-Time Type of an Expression.
Run-Time Type of an Expression.
Normal and Abrupt Completion of Evaluation.
Evaluation Order.
Primary Expressions.
Class Instance Creation Expressions.
Array Creation Expressions.
Field Access Expressions.
Method Call Expressions.
Array Access Expressions.
Postfix Expressions.
Unary Operators.
Cast Expressions.
Multiplicative Operators.
Additive Operators.
Shift Operators.
Relational Operators.
Equality Operators.
Bitwise and Logical Operators.
Conditional-And Operator &&.
Conditional-Or Operator ||.
Conditional Operator ? :.
Assignment Operators.
Expression.
Constant Expression.
Definite Assignment and Expressions.
Definite Assignment and Statements.
Terminology and Framework.
Rules about Variables.
Rules about Locks.
Rules about the Interaction of Locks and Variables.
Rules for Volatile Variables.
Discussion.
Threads.
Locks.
The Text of a Documentation Comment.
HTML in a Documentation Comment.
Summary Sentence.
Taglines.
Example.
Problem #1: Field vs. Method Declaration.
Problem #2: Cast vs. Parenthesized Expression.
Problem #3: Modifiers Too Specific.
Productions From §2.3: Syntactic Grammar.
Productions From §3: Lexical Structure.
Productions From §4: Types, Values, and Variables.
Productions From §6: Names.
Productions From §7: Packages.
Only in the LALR(1) Grammar.
Productions From §8: Class Declarations.
Productions From §9: Interface Declarations.
Productions From §10: Array Declarations.
Productions From §13: Blocks and Statements.
Productions From §14: Expressions.
The class Object.
The class Class.
The class Boolean.
The class Character.
The class Number.
The class Integer.
The class Long.
The class Float.
The class Double.
The class Math.
The class String.
The class StringBuffer.
The class ClassLoader.
The class Process.
The class Runtime.
The class SecurityManager.
The class System.
The interface Runnable.
The class Thread.
The class ThreadGroup.
Classes for Exceptions and Errors.
The class BitSet.
The class Date.
The class Dictionary.
The class EmptyStackException.
The interface Enumeration.
he class Hashtable.
The class NoSuchElementException.
The class Observable.
The interface Observer.
The class Properties.
The class Random.
The class Stack.
The class StringTokenizer.
The class Vector.
The class BufferedInputStream.
The class BufferedOutputStream.
The class ByteArrayInputStream.
The class ByteArrayOutputStream.
The interface DataInput.
The class DataInputStream.
The interface DataOutput.
The class DataOutputStream.
The class EOFException.
The class File.
The class FileDescriptor.
The class FileInputStream.
The class FileNotFoundException.
The class FileOutputStream.
The interface FilenameFilter.
The class FilterInputStream.
The class FilterOutputStream.
The class IOException.
The class InputStream.
The class InterruptedIOException.
The class LineNumberInputStream.
The class OutputStream.
The class PipedInputStream.
The class PipedOutputStream.
The class PrintStream.
The class PushbackInputStream.
The class RandomAccessFile.
The class SequenceInputStream.
The class StreamTokenizer.
The class StringBufferInputStream.
The class UTFDataFormatException.
To learn the latest about the Java Platform and Environment, or to download the latest Java release, visit our World Wide Web site at http://java.sun.com. For updated information about The Java Series, including sample code, errata, and previews of forthcoming books, visit http://java.sun.com/Series.
We would like to thank the Corporate and Professional Publishing Group at Addison-Wesley for their partnership in putting together the Series. Our editor Mike Hendrickson and his team have done a superb job of navigating us through the world of publishing. Within Sun, the support of James Gosling, Ruth Hennigar, Jon Kannegaard, and Bill Joy ensured that this series would have the resources it needed to be successful. In addition to the tremendous effort by individual authors, many members of the JavaSoft team have contributed behind the scenes to bring the highest level of quality and engineering to the books in the Series. A personal note of thanks to my children Christopher and James for putting a positive spin on the many trips to my office during the development of the Series.
Lisa Friendly
Series Editor
Java is a general-purpose concurrent class-based object-oriented programming language, specifically designed to have as few implementation dependencies as possible. Java allows application developers to write a program once and then be able to run it everywhere on the Internet.
This book attempts a complete specification of the syntax and semantics of the Java language and the core packages java.lang, java.io, and java.util of its Application Programming Interface. We intend that the behavior of every language construct is specified here, so that all implementations of Java will accept the same programs. Except for timing dependencies or other non-determinisms and given sufficient time and sufficient memory space, a Java program should compute the same result on all machines and in all implementations.
We believe that Java is a mature language, ready for widespread use. Nevertheless, we expect some evolution of the language in the years to come. We intend to manage this evolution in a way that is completely compatible with existing applications. To do this, we intend to make relatively few new versions of the language, and to distinguish each new version with a different filename extension. Java compilers and systems will be able to support the several versions simultannously, with complete compatibility.
Much research and experimentation with Java is already underway. We encourage this work, and will continue to cooperate with external groups to explore improvements to Java. For example, we have already received several interesting proposals for parameterized types. In technically difficult areas, near the state of the art, this kind of research collaboration is essential.
We acknowledge and thank the many people who have contributed to this book through their excellent feedback, assistance and encouragement: Particularly thorough, careful, and thoughtful reviews of drafts were provided by Tom Cargill, Peter Deutsch, Paul Hilfinger, Masayuki Ida, David Moon, Steven Muchnick, Charles L. Perkins, Chris Van Wyk, Steve Vinoski, Philip Wadler, Daniel Weinreb, and Kenneth Zadeck. We are very grateful for their extraordinary volunteer efforts.
We are also grateful for reviews, questions, comments, and suggestions from Stephen Adams, Bowen Alpern, Glenn Ammons, Leonid Arbuzov, Kim Bruce, Edwin Chan, David Chase, Pavel Curtis, Drew Dean, William Dietz, David Dill, Patrick Dussud, Ed Felten, John Giannandrea, John Gilmore, Charles Gust, Warren Harris, Lee Hasiuk, Mike Hendrickson, Mark Hill, Urs Hoelzle, Roger Hoover, Susan Flynn Hummel, Christopher Jang, Mick Jordan, Mukesh Kacker, Peter Kessler, James Larus, Derek Lieber, Bill McKeeman, Steve Naroff, Evi Nemeth, Robert O'Callahan, Dave Papay, Craig Partridge, Scott Pfeffer, Eric Raymond, Jim Roskind, Jim Russell, William Scherlis, Edith Schonberg, Anthony Scian, Matthew Self, Janice Shepherd, Kathy Stark, Barbara Steele, Rob Strom, William Waite, Greg Weeks, and Bob Wilson. (This list was generated semi-automatically from our e-mail records. We apologize if we have omitted anyone.)
The feedback from all these reviewers was invaluable to us in improving the definition of the Java language as well as the form of the presentation in this book. We thank them for their diligence. Any remaining errors in this book-we hope they are few-are our responsibility and not theirs.
We thank Francesca Freedman and Doug Kramer for assistance with matters of typography and layout. We thank Dan Mills of Adobe Systems Incorporated for assistance in exploring possible choices of typefaces.
Many of our colleagues at Sun Microsystems have helped us in one way or another. Lisa Friendly, our series editor, managed our relationship with Addison-Wesley. Susan Stambaugh managed the distribution of many hundreds of copies of drafts to reviewers. We received valuable assistance and technical advice from Ben Adida, Ole Agesen, Ken Arnold, Rick Cattell, Asmus Freytag, Norm Hardy, Steve Heller, David Hough, Doug Kramer, Nancy Lee, Marianne Mueller, Akira Tanaka, Greg Tarsy, David Ungar, Jim Waldo, Ann Wollrath, Geoff Wyant, and Derek White. We thank Alan Baratz, David Bowen, Mike Clary, John Doerr, Jon Kannegaard, Eric Schmidt, Bob Sproull, Bert Sutherland, and Scott McNealy for leadership and encouragement.
The on-line Bartleby Library of Columbia University, at URL: http://www.cc.columbia.edu/acis/bartleby/ was invaluable to us during the process of researching and verifying many of the quotations that are scattered throughout this book. Here is one example:
They lard their lean books with the fat of others' works.We are grateful to those who have toiled on Project Bartleby, for saving us a great deal of effort and reawakening our appreciation for the works of Walt Whitman.-Robert Burton (1576-1640)
We are thankful for the tools and services we had at our disposal in writing this book: telephones, overnight delivery, desktop workstations, laser printers, photocopiers, text formatting and page layout software, fonts, electronic mail, the World Wide Web, and, of course, the Internet. We live in three different states, scattered across a continent, but collaboration with each other and with our reviewers has seemed almost effortless. Kudos to the thousands of people who have worked over the years to make these excellent tools and services work quickly and reliably.
Mike Hendrickson, Katie Duffy, Simone Payment, and Rosa Aimee Gonzalez o