Example: biology

A Programmer’s Guide to Java - pearsoncmg.com

A programmer s Guide toJava SE 8 Oracle Certified Associate (OCA)This page intentionally left blank A programmer s Guide toJava SE 8 Oracle Certified Associate (OCA)A Comprehensive PrimerKhalid A. MughalRolf W. RasmussenBoston Columbus Indianapolis New York San Francisco Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City S o Paulo Sydney Hong Kong Seoul Singapore Taipei TokyoMany 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 capitalletters or in all 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.

A Programmer’s Guide to Java ® SE 8 Oracle Certified Associate (OCA) A Comprehensive Primer Khalid A. Mughal Rolf W. Rasmussen Boston • Columbus • Indianapolis • New York • San Francisco • Amsterdam • Cape Town

Tags:

  Guide, Programmer, Java, A programmer s guide to java

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of A Programmer’s Guide to Java - pearsoncmg.com

1 A programmer s Guide toJava SE 8 Oracle Certified Associate (OCA)This page intentionally left blank A programmer s Guide toJava SE 8 Oracle Certified Associate (OCA)A Comprehensive PrimerKhalid A. MughalRolf W. RasmussenBoston Columbus Indianapolis New York San Francisco Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City S o Paulo Sydney Hong Kong Seoul Singapore Taipei TokyoMany 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 capitalletters or in all 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.

2 No liability is assumed for incidental or consequential damages in connection with orarising out of the use of the information or programs contained information about buying this title in bulk quantities, or for special sales opportunities(which may include electronic versions; custom cover designs; and content particular toyour business, training goals, marketing focus, or branding interests), please contact ourcorporate sales department at or (800) government sales inquiries, please contact questions about sales outside the , please contact us on the Web: of Congress Control Number: 2016937073 Copyright 2017 Pearson Education, 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.

3 For information regardingpermissions, request forms and the appropriate contacts within the Pearson EducationGlobal Rights & Permissions Department, please visit : 978-0-13-293021-5 ISBN-10: 0-13-293021-8 Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, printing, July 2016To the loving memory of my mother, Zubaida Begum,and my father, Mohammed Azim K. Olivia E. Rasmussen andLouise J. Dahlmo page intentionally left blank viiContents OverviewFigures xixTables xxiExamples xxiiiForeword xxviiPreface xxix1 Basics of java Programming 12 Language Fundamentals 273 Declarations 474 Access Control 955 Operators and Expressions 1436 Control Flow 1997 Object-Oriented Programming 2638

4 Fundamental Classes 3419 Object Lifetime 38310 The ArrayList<E> Class and Lambda Expressions 41311 Date and Time 461viiiCONTENTS OVERVIEWA Taking the java SE 8 programmer I Exam 507B Exam Topics: java SE 8 programmer I 515C Annotated Answers to Review Questions 519D Solutions to Programming Exercises 553E Mock Exam: java SE 8 programmer I 571F Annotated Answers to Mock Exam I 605 Index 619ixContentsFigures xixTables xxiExamples xxiiiForeword xxviiPreface xxix1 Basics of java Members: Fields and Instantiation, Reference Values, and References4 Object Members6 Invoking : Aggregation and of Java13 Review java Application16 Essential Elements of a java Application16 Compiling and Running an Output18 Formatted java Ecosystem21 Object-Oriented Paradigm22 Interpreted.

5 The JVM23 Architecture-Neutral and Portable Bytecode23 Simplicity23 Dynamic and Distributed23 Robust and Secure24 High Performance and Multithreaded24 Review Questions25 Chapter Summary25 Programming Exercise262 Language Language Elements28 Lexical Tokens28 Identifiers28 Keywords29 Separators29 Literals30 Integer Literals30 Floating-Point Literals31 Underscores in Numerical Literals32 Boolean Literals32 Character Literals32 String Literals34 Whitespace35 Comments35 Review Data Types37 The Integer Types38 The char Type38 The Floating-Point Types38 The boolean Type39 Review Declarations40 Declaring and Initializing Variables41 Reference Values for Variables42 Default Values for Fields42 Initializing Local Variables of Primitive Data Types43 Initializing Local Reference Variables43 Lifetime of Variables44 Review Questions45 Chapter Summary46 Programming Declarations49 Statements50 Instance Methods and the Object Reference this50 Method Default Constructor54 Overloaded Constructors56 Review Array Variables59 Constructing an Array59 Initializing an Array60 Using an Array61 Anonymous Arrays63 Multidimensional Arrays64 Sorting Arrays68 Searching Arrays69 Review Passing72 Passing Primitive Data Values73 Passing Reference Values75 Passing Arrays77 Array Elements as Actual Parameters78final Arity Methods81 Calling a Variable Arity Method82 Variable Arity and Fixed Arity Method main()

6 Method85 Program Types87 Declaring Type-safe Enums87 Using Type-safe Enums88 Selected Methods for Enum Types89 Review Questions90 Chapter Summary92 Programming Exercise934 Access Source File Packages98 Using Packages99 Compiling Code into Packages105xiiCONTENTSR unning Code from for Classes107 Review Rules114 Class Scope for Members114 Block Scope for Local Modifiers for Top-Level Type Modifiers for Classes120abstract Classes120final Classes122 Review Accessibility Modifiers123public Members124protected Members126 Default Accessibility for Members127private Members128 Review Modifiers for Members131static Members132final Members133abstract Methods136synchronized Methods136native Methods137transient Fields138volatile Fields139 Review Questions140 Chapter Summary142 Programming Exercise1425 Operators and and Narrowing Primitive Conversions144 Widening and Narrowing Reference Conversions145 Boxing and Unboxing Conversions145 Other Conversion Contexts147 Assignment Context147 Method Invocation Context148 Casting Context of the Unary Type Cast Operator.

7 (type)148 Numeric Promotion and Associativity Rules for Order of Operands152 Left-Hand Operand Evaluation First152 Operand Evaluation before Operation Execution153 Left-to-Right Evaluation of Argument Integers154 Calculating Two s Complement155 Converting Binary Numbers to Decimals157 Converting Decimals to Binary Numbers157 Relationships among Binary, Octal, and Hexadecimal Simple Assignment Operator =158 Assigning Primitive Values159 Assigning References159 Multiple Assignments159 Type Conversions in an Assignment Context160 Review Operators: *, /, %, +, -163 Arithmetic Operator Precedence and Associativity164 Evaluation Order in Arithmetic Expressions164 Range of Numeric Values164 Unary Arithmetic Operators: -, +167 Multiplicative Binary Operators: *, /, %167 Additive Binary Operators: +, -169 Numeric Promotions in Arithmetic Expressions170 Arithmetic Compound Assignment Operators: *=, /=, %=, +=, -=172 Review Binary String Concatenation Operator + Increment and Decrement Operators: ++, --176 The Increment Operator ++176 The Decrement Operator --176 Review Operators: <, <=, >, >= Data Value Equality: ==, !

8 =181 Object Reference Equality: ==, !=182 Object Value Logical Operators: !, ^, &, |184 Operand Evaluation for Boolean Logical Operators185 Boolean Logical Compound Assignment Operators: &=, ^=, |= Operators: &&, ||186 Short-Circuit Bitwise Operators: ~, &, |, ^189 Bitwise Compound Assignment Operators: &=, ^=, |=192 Review Conditional Operator: ? Operators: new, [], instanceof, ->195 Review Questions196 Chapter Summary197 Programming Exercise197xivCONTENTS6 Control of Control Flow Statements200 The Simple if Statement200 The if-else Statement201 The switch Statement203 Review Statements213 The while Statement213 The do-while Statement214 The for(;;) Statement215 The for(:) Statements219 Labeled Statements220 The break Statement221 The continue Statement223 The return Statement224 Review Execution and Exception Types233 The Exception Class235 The RuntimeException Class236 The Error Class237 Checked and Unchecked Exceptions237 Defining Customized Handling: try, catch, and finally238 The try Block240 The catch Clause240 The finally throw throws Clause251 Overriding the throws of Exception Handling254 Review Questions255 Chapter Summary258 Programming Exercises2587 Object-Oriented Implementation Inheritance264 Relationships.

9 Is-a and has-a266 The Supertype Subtype Methods268 Instance Method Overriding268 Covariant return in Overriding Methods273 Overriding versus Members275 Field Hiding275 Static Method Object Reference super276 Review Constructors Using this() and super()282 The this() Constructor Call282 The super() Constructor Call285 Review Interfaces290 Abstract Methods in Interfaces291 Implementing Interfaces291 Extending Interfaces294 Interface References296 Default Methods in Interfaces297 Static Methods in Interfaces300 Constants in Interfaces302 Review and Subtyping309 Arrays and Subtype Covariance309 Array Store Values and Value Assignment Invocation Conversions Involving References315 Overloaded Method Casting and the instanceof Operator320 The Cast Operator320 The instanceof Operator321 Review and Dynamic Method versus Concepts in Object-Oriented Design334 Encapsulation335 Cohesion335 Coupling336 Review Questions336 Chapter Summary338 Programming Exercises3398 Fundamental of the Object Class342 Review Wrapper Classes346 Common Wrapper

10 Class Constructors347 Common Wrapper Class Utility Methods348 Numeric Wrapper Classes351 The Character Class354 The Boolean Class355 Review String Class357 Immutability357 Creating and Initializing Strings357 The CharSequence Interface360 Reading Characters from a String361 Comparing Strings363 Character Case in a String364 Concatenation of Strings364 Joining of CharSequence Objects365 Searching for Characters and Substrings367 Extracting Substrings369 Converting Primitive Values and Objects to Strings369 Formatted Strings370 Review StringBuilder and StringBuffer Classes374 Thread-Safety374 Mutability374 Constructing String Builders374 Reading and Changing Characters in String Builders375 Constructing Strings from String Builders375 Appending, Inserting, and Deleting Characters in String Builders376 Controlling String Builder Capacity378 Review Questions379 Chapter Summary382 Programming Exercises3829 Object Garbage Garbage Collection Programmatically393 Review Initializer Expressions400 Declaration Order of Initializer Initializer Blocks402 CONTENTS xviiDeclaration Order of Static Initializer Blocks404 Declaration Order of Instance Initial Object State406 Review Questions409 Chapter Summary41110 The ArrayList<E> Class and Lambda ArrayList<E> Class414 Lists414 Declaring References and Constructing ArrayLists415 Modifying an ArrayList419 Querying an ArrayList422 Traversing an ArrayList423 Converting an ArrayLi


Related search queries