Tài liệu Java precisely ppt

119 303 0
Tài liệu Java precisely ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Java Precisely Peter Sestoft The MIT Press Cambridge, , Massachusetts London, England Copyright © 2002 Massachusetts Institute of Technology All rights reserved. No part of this book may be reproduced in any form by any electronic or mechanical means (including photocopying, recording, or information storage and retrieval) without permission in writing from the publisher. This book was set in Times by the author using . Printed and bound in the United States of America. Library of Congress Cataloging-in-Publication Data Sestoft, Peter. Java precisely/Peter Sestoft. p. cm. Includes bibliographic references and index. ISBN 0-262-69276-7 (pbk. : alk. paper) 1. Java (Computer program language) I. Title. QA76.73.J38 S435 2002 005.13'3—dc21 2002016516 Table of Contents Preface 4 Notational Conventions 5 Chapter 1 - Running Java: Compilation, Loading, and Execution . 6 Chapter 2 - Names and Reserved Names 7 Chapter 3 - Java Naming Conventions . 8 Chapter 4 - Comments and Program Layout . 9 Chapter 5 - Types 11 Chapter 6 - Variables, Parameters, Fields, and Scope . 13 Chapter 7 -Strings . 16 Chapter 8 - Arrays . 19 Chapter 9 -Classes . 23 Chapter 10 - Classes and Objects in the Computer . 35 Chapter 11 - Expressions 37 Chapter 12 - Statements . 51 Chapter 13 -Interfaces . 61 Chapter 14 - Exceptions, Checked and Unchecked 63 Chapter 15 - Threads, Concurrent Execution, and Synchronization 66 Chapter 16 - Compilation, Source Files, Class Names, and Class Files . 73 Chapter 17 - Packages and Jar Files . 74 Chapter 18 - Mathematical Functions 76 Chapter 19 -String Buffers . 79 Chapter 20 - Collections and Maps 81 Chapter 21 - Input and Output . 95 References 119 4 Preface This book gives a concise description of the Java 2 programming language, versions 1.3 and 1.4. It is a quick reference for the reader who has already learned (or is learning) Java from a standard textbook and who wants to know the language in more detail. The book presents the entire Java programming language and essential parts of the class libraries: the collection classes and the input-output classes. General rules are shown on left-hand pages mostly, and corresponding examples are shown on right- hand pages only. All examples are fragments of legal Java programs. The complete ready-to-run example programs are available from the book Web site <http://www.dina.kvl.dk/~sestoft/javaprecisely/ >. The book does not cover garbage collection, finalization and weak references, reflection, details of IEEE754 floating-point numbers, or Javadoc. Acknowledgments Thanks to Rasmus Lund, Niels Hallenberg, Hans Henrik Løvengreen, Christian Gram, Jan Clausen, Anders Peter Ravn, Bruce Conrad, Brendan Humphreys, Hans Rischel and Ken Friis Larsen for their useful comments, suggestions, and corrections. Special thanks to Rasmus Lund for letting me adapt his collections diagram for this book. Thanks also to the Royal Veterinary and Agricultural University and the IT University of Copenhagen, Denmark, for their support. 5 Notational Conventions Symbol Meaning v Value of any type x Variable or parameter or field or array element e Expression t Type (primitive type or reference type) s Expression of type String m Method f Field C Class E Exception type I Interface a Expression or value of array type i Expression or value of integer type o Expression or value of object type sig Signature of method or constructor p Package u Expression or value of thread type 6 Chapter 1: Running Java: Compilation, Loading, and Execution Before a Java program can be executed, it must be compiled and loaded. The compiler checks that the Java program is legal: that the program conforms to the Java syntax (grammar), that operators (such as +) are applied operands (such as 5 and x) of the correct type, and so on. If so, the compiler generates so-called class files. Execution then starts by loading the needed class files. Thus running a Java program involves three stages: compilation (checks that the program is well- formed), loading (loads and initializes classes), and execution (runs the program code). 7 Chapter 2: Names and Reserved Names A legal name (of a variable, method, field, parameter, class, interface or package) starts with a letter or dollar sign ($) or underscore (_), and continues with zero or more letters or dollar signs or underscores or digits (0–9). Avoid dollar signs in class names. Uppercase letters and lowercase letters are considered distinct. A legal name cannot be one of the following reserved names: abstract char else goto long public assert class extends if native return boolean const false implements new short break continue final import null static byte default finally instanceof package strictfp case do float int private super catch double for interface protected switch 8 Chapter 3: Java Naming Conventions The following naming conventions are often followed, although not enforced by Java:  If a name is composed of several words, then each word (except possibly the first one) begins with an uppercase letter. Examples: setLayout, addLayoutComponent.  Names of variables, fields, and methods begin with a lowercase letter. Examples: vehicle, myVehicle.  Names of classes and interfaces begin with an uppercase letter. Examples: Cube, ColorCube.  Named constants (that is, final variables and fields) are written entirely in uppercase, and the parts of composite names are separated by underscores (_). Examples: CENTER, MAX_VALUE.  Package names are sequences of dot-separated lowercase names. Example: java.awt.event. For uniqueness, they are often prefixed with reverse domain names, as in com. sun.xml.util. 9 Chapter 4: Comments and Program Layout Comments have no effect on the execution of the program but may be inserted anywhere to help humans understand the program. There are two forms: one-line comments and delimited comments. Program layout has no effect on the computer's execution of the program but is used to help humans understand the structure of the program. Example 1: Comments class Comment { // This is a one-line comment; it extends to the end of the line. /* This is a delimited comment, extending over several lines. */ int /* This delimited comment extends over part of a line */ x = 117; } Example 2: Recommended Program Layout Style For reasons of space this layout style is not always followed in this book. class Layout { // Class declaration int a; Layout(int a) { this.a = a; // One-line body } int sum(int b) { // Multi-line body if (a > 0) { // If statement return a + b; // Single statement } else if (a < 0) { // Nested if-else, block statement int res = -a + b; return res * 117; } else { // a == 0 // Terminal else, block statement int sum = 0; for (int i=0; i<10; i++) { // For loop sum += (b - i) * (b - i) ; } return sum; } } static boolean checkdate(int mth, int day) { int length; switch (mth) { // Switch statement case 2: // Single case length = 28; break; 10 case 4: case 6: case 9: case 11: // Multiple case length = 30; break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: length = 31; break; default: return false; } return (day >= 1) && (day <= length); } } [...]... Array Types An array type has the form t[], where t is any type An array type t[] is a reference type Hence a value of array type t[] is either null, or is a reference to an array whose element type is precisely t (when t is a primitive type), or is a subtype of t (when t is a reference type) 5.4 Subtypes and Compatibility A type t1 may be a subtype of a type t2, in which case t2 is a supertype of t1... encoding If s1 or s2 is null, then the exception NullPointerException is thrown Method compareToIgnoreCase is similar but does not distinguish lowercase and uppercase More String methods are described in the Java class library documentation [3] Example 5: Equality of Strings, and the Subtlety of the (+) Operator \ddd String s1 = "abc"; String s2 = s1 + ""; String s3 = s1; // New object, but contains same text... k) last brackets empty: new t[ℓ1] [ℓ2] [ℓk] [] [] To access an element of an n-dimensional array a, use n index expressions: a[i1] [i2] [in] 20 8.4 The Utility Class Arrays Class Arrays from package java. util provides static utility methods to compare, fill, sort, and search arrays, and to create a collection (chapter 20) from an array The binarySearch, equals, fill, and sort methods are overloaded... boolean The Object versions of binarySearch and sort use the compareTo method of the array elements, unless an explicit Comparator object (section 20.8) is given static List asList (Object[] a) returns a java. util.List view of the elements of a, in index order The resulting list implements RandomAccess (section 20.2 and example 94) static int binarySearch (byte[] a, byte k) returns an index i>=0 for which... array, the object's fields or the array's elements may still be modified The initialization must happen either in the declaration or in an initializer block (section 9.10), or (if the field is nonstatic) precisely once in every constructor in class C A field initializer may be an expression or an array initializer (section 8.2) A static field initializer can refer only to static members of C and can throw... object That object can be referred to as C.this (example 36), so a nonstatic member x of the enclosing object can be referred to as C.this.x An inner class or local class cannot have static members More precisely, all static fields must also be final, and methods and nested classes in an inner class or local class must be nonstatic A static nested class, that is, a static member class SMC or a local class... nonstatic fields, an inner object will always contain a reference to an enclosing object, which is an object of the innermost enclosing class C The enclosing object reference can be written C.this in Java programs An object of a static nested class, on the other hand, contains no reference to an enclosing object Example 34: Objects and Classes This is the computer memory at the end of the main method . Sestoft, Peter. Java precisely/ Peter Sestoft. p. cm. Includes bibliographic references and index. ISBN 0-262-69276-7 (pbk. : alk. paper) 1. Java (Computer. of legal Java programs. The complete ready-to-run example programs are available from the book Web site <http://www.dina.kvl.dk/~sestoft/javaprecisely/

Ngày đăng: 15/12/2013, 20:15

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan