Khalid mughal scjp 1.6


Published on

SCJP 1.6

Published in: Technology, Education
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Khalid mughal scjp 1.6

  1. 1. A Programmer’s Guide toJava™ SCJP CertificationThird Edition
  2. 2. This page intentionally left blank
  3. 3. A Programmer’s Guide toJava™ SCJP CertificationA Comprehensive PrimerThird EditionKhalid A. MughalRolf W. RasmussenUpper Saddle River, New Jersey • Boston • Indianapolis • San FranciscoNew York • Toronto • Montreal • London • Munich • Paris • MadridCapetown • Sidney • Tokyo • Singapore • Mexico City
  4. 4. Many of the designations used by manufacturers and sellers to distinguish their productsare claimed as trademarks. Where those designations appear in this book, and the publisherwas aware of a trademark claim, the designations have been printed with initial capital let-ters or in all capitals.The authors and publisher have taken care in the preparation of this book, but make noexpressed or implied warranty of any kind and assume no responsibility for errors or omis-sions. No liability is assumed for incidental or consequential damages in connection with orarising out of the use of the information or programs contained herein.The publisher offers excellent discounts on this book when ordered in quantity for bulk pur-chases or special sales, which may include electronic versions and/or custom covers andcontent particular to your business, training goals, marketing focus, and branding interests.For more information, please contact:U.S. Corporate and Government Sales(800) 382-3419corpsales@pearsontechgroup.comFor sales outside the United States please contact:International Salesinternational@pearson.comVisit us on the Web: of Congress Cataloging-in-Publication DataMughal, Khalid Azim.A programmers guide to Java SCJP certification : a comprehensive primer / Khalid A.Mughal, Rolf W. Rasmussen.—3rd ed.p. cm.Previously published under title: A programmer’s guide to Java certification.Includes bibliographical references and index.ISBN 0-321-55605-4 (pbk. : alk. paper)1. Electronic data processing personnel--Certification. 2. Operating systems (Comput-ers)—Examinations--Study guides. 3. Java (Computer program language)--Examinations--Study guides. I. Rasmussen, Rolf (Rolf W.) II. Mughal, Khalid Azim. Programmer’s guideto Java certification. III. Title.QA76.3.M846 2008005.2762--dc22 2008048822Copyright © 2009 Pearson Education, Inc.All rights reserved. Printed in the United States of America. This publication is protected bycopyright, and permission must be obtained from the publisher prior to any prohibitedreproduction, storage in a retrieval system, or transmission in any form or by any means,electronic, mechanical, photocopying, recording, or likewise. For information regardingpermissions, write to:ISBN-13: 978-0-321-55605-9ISBN-10: 0-321-55605-4Text printed in the United States on recycled paper at Courier in Stoughton, Massachusetts.First printing, December 2008
  5. 5. To the loving memory of my mother, Zubaida Begum,and my father, Mohammed Azim.—K.A.M.For Olivia E. Rasmussen andLouise J. Dahlmo.—R.W.R.
  6. 6. This page intentionally left blank
  7. 7. viiContents OverviewForeword xxxvPreface xxxvii1 Basics of Java Programming 12 Language Fundamentals 193 Declarations 394 Access Control 1035 Operators and Expressions 1596 Control Flow 2037 Object-Oriented Programming 2838 Nested Type Declarations 3519 Object Lifetime 38910 Fundamental Classes 42311 Files and Streams 46712 Localization, Pattern Matching and Formatting 53113 Threads 61314 Generics 661
  8. 8. viii CONTENTS15 Collections and Maps 747A Taking the SCJP 1.6 Exam 851B Objectives for the SCJP 1.6 Exam 857C Objectives for the SCJP 1.6 Upgrade Exam 863D Annotated Answers to Review Questions 869E Solutions to Programming Exercises 935F Mock Exam 959G Number Systems and Number Representation 1005Index 1013
  9. 9. ixContentsList of Figures xxiiiList of Tables xxviiList of Examples xxixForeword xxxvPreface xxxvii1 Basics of Java Programming 11.1 Introduction 21.2 Classes 2Declaring Members: Fields and Methods 31.3 Objects 4Class Instantiation, Reference Values, and References 4Object Aliases 61.4 Instance Members 6Invoking Methods 71.5 Static Members 71.6 Inheritance 101.7 Aggregation 121.8 Tenets of Java 13Review Questions 131.9 Java Programs 151.10 Sample Java Application 15Essential Elements of a Java Application 15Compiling and Running an Application 16Review Questions 17Chapter Summary 18Programming Exercise 18
  10. 10. x CONTENTS2 Language Fundamentals 192.1 Basic Language Elements 20Lexical Tokens 20Identifiers 20Keywords 20Literals 21Integer Literals 22Floating-Point Literals 22Boolean Literals 23Character Literals 23String Literals 25White Spaces 25Comments 26Review Questions 272.2 Primitive Data Types 28Integer Types 28The char Type 29The Floating-Point Types 29The boolean Type 30Review Questions 312.3 Variable Declarations 31Declaring and Initializing Variables 31Reference Variables 322.4 Initial Values for Variables 33Default Values for Fields 33Initializing Local Variables of Primitive Data Types 34Initializing Local Reference Variables 35Lifetime of Variables 35Review Questions 36Chapter Summary 37Programming Exercise 373 Declarations 393.1 Class Declarations 403.2 JavaBeans Standard 41Naming Patterns for Properties 41Naming Patterns for the Event Model 423.3 Method Declarations 44Statements 45Instance Methods and the Object Reference this 45Method Overloading 473.4 Constructors 48The Default Constructor 49Overloaded Constructors 51Review Questions 52
  11. 11. CONTENTS xi3.5 Enumerated Types 54Declaring Typesafe Enums 54Using Typesafe Enums 54Declaring Enum Constructors and Members 55Implicit Static Methods for Enum Types 57Inherited Methods from the Enum Class 58Extending Enum Types: Constant-Specific Class Bodies 59Declaring Typesafe Enums Revisited 62Review Questions 633.6 Arrays 69Declaring Array Variables 70Constructing an Array 70Initializing an Array 71Using an Array 72Anonymous Arrays 74Multidimensional Arrays 75Review Questions 793.7 Parameter Passing 81Passing Primitive Data Values 82Passing Reference Values 84Passing Arrays 86Array Elements as Actual Parameters 87final Parameters 893.8 Variable Arity Methods 90Calling a Varargs Method 91Varargs and Non-Varargs Method Calls 933.9 The main() Method 94Program Arguments 95Review Questions 96Chapter Summary 100Programming Exercises 1014 Access Control 1034.1 Java Source File Structure 1044.2 Packages 105Defining Packages 106Using Packages 107Compiling Code into Packages 115Running Code from Packages 1174.3 Searching for Classes 1174.4 The JAR Utility 1204.5 System Properties 122Review Questions 1234.6 Scope Rules 129Class Scope for Members 129
  12. 12. xii CONTENTSBlock Scope for Local Variables 1314.7 Accessibility Modifiers for Top-Level Type Declarations 1324.8 Other Modifiers for Classes 135abstract Classes 135final Classes 136Review Questions 1384.9 Member Accessibility Modifiers 138public Members 139protected Members 141Default Accessibility for Members 142private Members 143Review Questions 1444.10 Other Modifiers for Members 146static Members 147final Members 148abstract Methods 150synchronized Methods 150native Methods 151transient Fields 152volatile Fields 153Review Questions 154Chapter Summary 157Programming Exercise 1575 Operators and Expressions 1595.1 Conversions 160Widening and Narrowing Primitive Conversions 160Widening and Narrowing Reference Conversions 161Boxing and Unboxing Conversions 162Other Conversions 1625.2 Type Conversion Contexts 163Assignment Context 164Method Invocation Context 164Casting Context of the Unary Type Cast Operator: (type) 164Numeric Promotion Context 1655.3 Precedence and Associativity Rules for Operators 1665.4 Evaluation Order of Operands 168Left-Hand Operand Evaluation First 168Operand Evaluation before Operation Execution 168Left to Right Evaluation of Argument Lists 1695.5 The Simple Assignment Operator = 169Assigning Primitive Values 169Assigning References 169Multiple Assignments 170Type Conversions in Assignment Context 171
  13. 13. CONTENTS xiiiReview Questions 1735.6 Arithmetic Operators: *, /, %, +, - 174Arithmetic Operator Precedence and Associativity 174Evaluation Order in Arithmetic Expressions 174Range of Numeric Values 175Unary Arithmetic Operators: -, + 177Multiplicative Binary Operators: *, /, % 178Additive Binary Operators: +, - 180Numeric Promotions in Arithmetic Expressions 180Arithmetic Compound Assignment Operators: *=, /=, %=, +=, -= 182Review Questions 1845.7 The Binary String Concatenation Operator + 1855.8 Variable Increment and Decrement Operators: ++, -- 186The Increment Operator ++ 187The Decrement Operator -- 187Review Questions 1885.9 Boolean Expressions 1905.10 Relational Operators: <, <=, >, >= 1905.11 Equality 191Primitive Data Value Equality: ==, != 191Object Reference Equality: ==, != 192Object Value Equality 1935.12 Boolean Logical Operators: !, ^, &, | 194Operand Evaluation for Boolean Logical Operators 195Boolean Logical Compound Assignment Operators: &=, ^=, |= 1955.13 Conditional Operators: &&, || 196Short-Circuit Evaluation 197Review Questions 1995.14 The Conditional Operator: ?: 2015.15 Other Operators: new, [], instanceof 201Chapter Summary 202Programming Exercise 2026 Control Flow 2036.1 Overview of Control Flow Statements 2046.2 Selection Statements 204The Simple if Statement 204The if-else Statement 205The switch Statement 207Review Questions 2126.3 Iteration Statements 216The while Statement 217The do-while Statement 217The for(;;) Statement 218The for(:) Statement 220
  14. 14. xiv CONTENTS6.4 Transfer Statements 223Labeled Statements 223The break Statement 224The continue Statement 226The return Statement 228Review Questions 2296.5 Stack-Based Execution and Exception Propagation 2356.6 Exception Types 239The Exception Class 241The RuntimeException Class 241The Error Class 242Checked and Unchecked Exceptions 243Defining New Exceptions 2446.7 Exception Handling: try, catch, and finally 245The try Block 245The catch Block 246The finally Block 2516.8 The throw Statement 2556.9 The throws Clause 257Review Questions 2606.10 Assertions 265The assert Statement and the AssertionError Class 265Compiling Assertions 267Runtime Enabling and Disabling of Assertions 269Using Assertions 272Review Questions 276Chapter Summary 279Programming Exercises 2797 Object-Oriented Programming 2837.1 Single Implementation Inheritance 284Inheritance Hierarchy 286Relationships: is-a and has-a 286The Supertype-Subtype Relationship 2877.2 Overriding Methods 288Instance Method Overriding 288Covariant return in Overriding Methods 290Overriding vs. Overloading 2927.3 Hiding Members 294Field Hiding 294Static Method Hiding 2947.4 The Object Reference super 295Review Questions 2977.5 Chaining Constructors Using this() and super() 302The this() Constructor Call 302