ebook img

Java Rules PDF

1000 Pages·2001·5.851 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 Rules

Java™ Rules, Volume 2 Mastering the Fundamentals of the Java Programming Language Java™ Rules, Volume 2 Mastering the Fundamentals of the Java Programming Language Doug Dunn Java is a registered trademark of Sun Microsystems, Inc. Windows 95, Windows NT, Win- dows 2000 and Windows XP are trademarks of Microsoft Corporation. All other product or company names mentioned herein are the property of their respective owners. If the pub- lisher was aware of a trademark claim, the product or company name is capitalized. The publisher has taken care in the preparation of this book, but makes no expressed or implied warranty of any kind and assumes no responsibility for errors or omissions. No lia- bility is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher is excepting back orders for printed editions of this book. For more informa- tion, please write to [email protected]. Visit the author’s Web site at www.javarules.com. Library of Congress Cataloging-in-Publication Data Dunn, Douglas, 1958- Mastering The Fundamentals of the Java Programming Language / Douglas Dunn. p. cm. Includes bibliographical references and index. ISBN 0-201-70916-3 1. Java (Computer program language) I. Title. QA76.73.J38 D84 2001 005.13’3—dc21 2001034111 Copyright © 2003 by Douglas Dunn All rights reserved. No part of this publication may be reproduced, stored in a retrieval sys- tem, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. If you have downloaded an electronic copy from the author’s Web site www.javarules.com, a single copy may be printed for your personal use only. 0-201-70916-3 This book is dedicated to the child in each of us who keeps asking the annoying question “Why?” – Doug Table of Contents Be advised that this table of contents (TOC) only includes the first two section levels (referred to as heads in the publishing industry). For a complete TOC, see the chapter-level TOC at that beginning of each chapter. They include section numbers with four components such as 1.5.2.1 The Problem of Changeable Inlined Constants in the first chapter. Table of Contents VII Preface for Volume 2 XIII About This Book XIX A Java Tradition XXIX A Pet Peeve XXXI Vocabulary List 989 Index 997 Fields and Methods 33 1.1 Introduction 34 1.2 Fields 35 1.3 Field Initialization 38 1.3.1 Automatic Initialization Using Standard Default Values 38 1.3.2 Initialization Blocks 39 1.3.3 Constructors 42 1.4 Field Initialization Anomalies 55 1.4.1 The Problem of Forward Referencing 56 1.4.2 Invoking Overridden Methods During Object Initialization 61 1.4.3 Inlined Constants Always Appear to Have Been Initialized 65 1.4.4 The Variable Initializer for a Field is Always Executed 68 1.4.5 StackOverflowError During Object Initialization 70 1.4.6 Throwing Checked Exceptions During Initialization 73 1.5 Constants 81 1.5.1 Compile-Time Constant Expressions 82 1.5.2 Inlined Constants 89 1.5.3 Declaring Mutable Objects final 101 1.5.4 Blank Finals 102 1.5.5 Enumerated Types 106 1.5.6 Declaring Local Variables and Parameters final 126 1.5.7 The Constant Interface Antipattern 130 VII 1.6 Methods 131 1.6.1 abstract Methods 134 1.6.2 Result Types and the return Statement 139 1.6.3 Formal Parameter Lists 144 1.6.4 The throws Clause 165 1.7 Local Variables 174 1.8 “Write Once, Compile Anywhere” 179 1.8.1 Definite Assignment 181 1.8.2 Unreachable Statements 184 1.9 Qualifying Type versus Compile-Time Declaration 188 1.10 The Five General Forms 193 1.10.1 The Meaning of a Simple Field or Method Name 198 1.10.2 Method Invocation Chaining 204 1.10.3 Casting a Target Reference 206 1.10.4 Accessing static Members using a Primary Expression 208 1.11 Method Signatures 209 1.11.1 The Compiler-Enforced Method Contract 215 1.11.2 Overloaded Methods 225 1.11.3 Overriding and Dynamic Method Lookup 233 1.12 Method Forwarding 246 Scope, Shadowing, and Qualified Access 247 2.1 Introduction 248 2.2 Namespaces 249 2.2.1 The Meaning of a Simple or Partially Qualified Name 252 2.2.2 Disambiguating Type Names 254 2.3 The Fundamentals of Lexical Scoping 258 2.3.1 The Mysterious Scope Hole 266 2.3.2 Compilation Unit Scope 268 2.3.3 Members Shadow Declarations in Enclosing Scopes 268 2.3.4 The Scope of Types in the Unnamed Package 271 2.3.5 Circular Dependencies in Type Declarations 276 2.4 Shadowing 278 2.5 Obscuring 281 2.6 Observable Compilation Units and Packages 284 2.7 Qualified Access 286 2.8 Access Control 290 2.8.1 The protected Access Modifier 301 2.8.2 Full Access to the Members of an Enclosing Class 312 VIII JAVA RULES 2.8.3 Members More Accessible Than Their Class Type 315 2.8.4 Accessing the Implementation of Same Class Objects 323 2.9 Encapsulation 324 Hiding and Inheritance 329 3.1 Introduction 329 3.2 Hiding 332 3.3 The Definition of Baseclass 340 3.4 The Definition of Related Classes 341 3.5 Generalization in Inheritance Hierarchies 342 3.6 Inheritance 344 3.6.1 Interface Inheritance 345 3.6.2 Implementation Inheritance 347 3.6.3 Inheriting Overloaded Methods 362 3.7 Do Interfaces Extend the Object Class? 362 3.8 Inheriting Members With The Same Name 367 3.8.1 Re-Inheritance 367 3.8.2 Ambiguous Names Related to Inheritance 368 3.8.3 Inheriting Methods With the Same Signature 370 3.9 Designing Extensible Classes 374 3.10 Capping a Class Hierarchy 387 Expressions, Statements, and Blocks 391 4.1 Introduction 392 4.2 Expressions 393 4.2.1 Primary Expressions 397 4.2.2 Expression Statements and Other Top-Level Expressions 402 4.3 Operator Expressions 405 4.3.1 Numeric Promotion 407 4.3.2 Operator Order of Precedence and Parenthesized Expressions 415 4.3.3 The Associative Property of Operators 428 4.3.4 Nondestructive Operators 430 4.4 Exceptions are Precise 431 4.5 The 38 Unary, Binary, and Ternary Operators 435 4.5.1 Increment and Decrement Operators -- and ++ 436 4.5.2 Negation Operators -, ~, and ! 438 4.5.3 The Elementary School Operators 444 4.5.4 Remainder Operator % 446 4.5.5 Boolean Logical Operators &&, ||, &, |, and ^ 450 IX 4.5.6 Bitwise Operators &, |, ^, >>, >>>, and << 454 4.5.7 Ternary Conditional Operator ?: 465 4.5.8 The Simple and Compound Assignment Operators 470 4.6 The instanceof Type Comparison Operator 473 4.7 A Bitwise Primer 475 4.7.1 Bits 477 4.7.2 Converting Nybbles to Hexadecimal Digits 492 4.7.3 General-Purpose Integer to String Conversion Methods 498 4.7.4 Unsigned Bytes 501 4.7.5 Some Miscellaneous Uses of the Bitwise Operators 523 4.8 Statements 526 4.8.1 Control-flow Statements 529 4.8.2 Labeled Statements 554 4.8.3 Control-transfer Statements (a.k.a. Abrupt Completion) 560 4.9 Blocks 566 Type Conversions and the Cast Operator 571 5.1 Introduction 572 5.2 The Type of a Variable or Expression versus the Class of an Object 573 5.2.1 The Phrase “type of an object” is in Prevalent Use 576 5.2.2 The Term class type is Where Everything Goes Afoul 577 5.3 Java is a Strongly Typed Language 583 5.4 Substitution is a Higher Concept than Polymorphism 587 5.5 Forbidden Conversions 595 5.6 Permitted Conversions 597 5.6.1 Identity Conversions 600 5.6.2 Primitive Type Conversions 601 5.6.3 Reference Type Conversions 612 5.7 Conversion Contexts 623 5.7.1 Simple Assignment Conversion Context 627 5.7.2 Method Invocation Conversion Context 632 5.7.3 Method Return Conversion Context 636 5.7.4 The Cast Operator 638 5.7.5 The Implicit Cast in a Compound Assignment Operation 643 5.8 Overloaded Method Matching 644 5.8.1 Choosing The Most Specific Applicable Method 645 5.8.2 The Declaring Class of Applicable Methods 650 5.9 Value Set Conversions 656 X JAVA RULES

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.