ebook img

Java Precisely PDF

119 Pages·2002·0.854 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Java Precisely

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 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. 4 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 5 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). 6 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 7 Chapter 3: Java Naming Conventions The following naming conventions are often followed, although not enforced by Java: (cid:131) If a name is composed of several words, then each word (except possibly the first one) begins with an uppercase letter. Examples: setLayout, addLayoutComponent. (cid:131) Names of variables, fields, and methods begin with a lowercase letter. Examples: vehicle, myVehicle. (cid:131) Names of classes and interfaces begin with an uppercase letter. Examples: Cube, ColorCube. (cid:131) 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. (cid:131) 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. 8 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; 9 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); } } 10

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.