Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

A Programmer’s Guide to Java® SE 8 Oracle Certified Associate (OCA)
Nội dung xem thử
Mô tả chi tiết
About This E-Book
EPUB is an open, industry-standard format for e-books. However, support for EPUB
and its many features varies across reading devices and applications. Use your device or
app settings to customize the presentation to your liking. Settings that you can customize
often include font, font size, single or double column, landscape or portrait mode, and
figures that you can click or tap to enlarge. For additional information about the settings
and features on your reading device or app, visit the device manufacturer’s Web site.
Many titles include programming code or configuration examples. To optimize the
presentation of these elements, view the e-book in single-column, landscape mode and
adjust the font size to the smallest setting. In addition to presenting code and
configurations in the reflowable text format, we have included images of the code that
mimic the presentation found in the print book; therefore, where the reflowable format
may compromise the presentation of the code listing, you will see a “Click here to view
code image” link. Click the link to view the print-fidelity code image. To return to the
previous page viewed, click the Back button on your device or app.
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
Dubai • London • Madrid • Milan • Munich • Paris • Montreal • Toronto • Delhi • Mexico
City
São Paulo • Sydney • Hong Kong • Seoul • Singapore • Taipei • Tokyo
Many of the designations used by manufacturers and sellers to distinguish their products
are claimed as trademarks. Where those designations appear in this book, and the
publisher was aware of a trademark claim, the designations have been printed with initial
capital letters or in all capitals.
The authors and publisher have taken care in the preparation of this book, but make no
expressed or implied warranty of any kind and assume no responsibility for errors or
omissions. No liability is assumed for incidental or consequential damages in connection
with or arising out of the use of the information or programs contained herein.
For information about buying this title in bulk quantities, or for special sales opportunities
(which may include electronic versions; custom cover designs; and content particular to
your business, training goals, marketing focus, or branding interests), please contact our
corporate sales department at [email protected] or (800) 382-3419.
For government sales inquiries, please contact [email protected].
For questions about sales outside the U.S., please contact [email protected].
Visit us on the Web: informit.com/aw
Library of Congress Control Number: 2016937073
Copyright © 2017 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected
by copyright, and permission must be obtained from the publisher prior to any prohibited
reproduction, storage in a retrieval system, or transmission in any form or by any means,
electronic, mechanical, photocopying, recording, or likewise. For information regarding
permissions, request forms and the appropriate contacts within the Pearson Education
Global Rights & Permissions Department, please visit www.pearsoned.com/permissions/.
ISBN-13: 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,
Indiana.
First printing, July 2016
To the loving memory of my mother, Zubaida Begum,
and my father, Mohammed Azim
—K.A.M.
For Olivia E. Rasmussen and
Louise J. Dahlmo
—R.W.R.
Contents Overview
Figures
Tables
Examples
Foreword
Preface
1 Basics of Java Programming
2 Language Fundamentals
3 Declarations
4 Access Control
5 Operators and Expressions
6 Control Flow
7 Object-Oriented Programming
8 Fundamental Classes
9 Object Lifetime
10 The ArrayList<E> Class and Lambda Expressions
11 Date and Time
A Taking the Java SE 8 Programmer I Exam
B Exam Topics: Java SE 8 Programmer I
C Annotated Answers to Review Questions
D Solutions to Programming Exercises
E Mock Exam: Java SE 8 Programmer I
F Annotated Answers to Mock Exam I
Index
Contents
Figures
Tables
Examples
Foreword
Preface
1 Basics of Java Programming
1.1 Introduction
1.2 Classes
Declaring Members: Fields and Methods
1.3 Objects
Class Instantiation, Reference Values, and References
Object Aliases
1.4 Instance Members
Invoking Methods
1.5 Static Members
1.6 Inheritance
1.7 Associations: Aggregation and Composition
1.8 Tenets of Java
Review Questions
1.9 Java Programs
1.10 Sample Java Application
Essential Elements of a Java Application
Compiling and Running an Application
1.11 Program Output
Formatted Output
1.12 The Java Ecosystem
Object-Oriented Paradigm
Interpreted: The JVM
Architecture-Neutral and Portable Bytecode
Simplicity
Dynamic and Distributed
Robust and Secure
High Performance and Multithreaded
Review Questions
Chapter Summary
Programming Exercise
2 Language Fundamentals
2.1 Basic Language Elements
Lexical Tokens
Identifiers
Keywords
Separators
Literals
Integer Literals
Floating-Point Literals
Underscores in Numerical Literals
Boolean Literals
Character Literals
String Literals
Whitespace
Comments
Review Questions
2.2 Primitive Data Types
The Integer Types
The char Type
The Floating-Point Types
The boolean Type
Review Questions
2.3 Variable Declarations
Declaring and Initializing Variables
Reference Variables
2.4 Initial Values for Variables
Default Values for Fields
Initializing Local Variables of Primitive Data Types
Initializing Local Reference Variables
Lifetime of Variables
Review Questions
Chapter Summary
Programming Exercise
3 Declarations
3.1 Class Declarations
3.2 Method Declarations
Statements
Instance Methods and the Object Reference this
Method Overloading
3.3 Constructors
The Default Constructor
Overloaded Constructors
Review Questions
3.4 Arrays
Declaring Array Variables
Constructing an Array
Initializing an Array
Using an Array
Anonymous Arrays
Multidimensional Arrays
Sorting Arrays
Searching Arrays
Review Questions
3.5 Parameter Passing
Passing Primitive Data Values
Passing Reference Values
Passing Arrays
Array Elements as Actual Parameters
final Parameters
3.6 Variable Arity Methods
Calling a Variable Arity Method
Variable Arity and Fixed Arity Method Calls
3.7 The main() Method
Program Arguments
3.8 Enumerated Types
Declaring Type-safe Enums
Using Type-safe Enums
Selected Methods for Enum Types
Review Questions
Chapter Summary
Programming Exercise
4 Access Control
4.1 Java Source File Structure
4.2 Packages
Defining Packages
Using Packages
Compiling Code into Packages
Running Code from Packages
4.3 Searching for Classes
Review Questions
4.4 Scope Rules
Class Scope for Members
Block Scope for Local Variables
4.5 Accessibility Modifiers for Top-Level Type Declarations
4.6 Non-Accessibility Modifiers for Classes
abstract Classes
final Classes
Review Questions
4.7 Member Accessibility Modifiers
public Members
protected Members
Default Accessibility for Members
private Members
Review Questions
4.8 Non-Accessibility Modifiers for Members
static Members
final Members
abstract Methods
synchronized Methods
native Methods
transient Fields
volatile Fields
Review Questions
Chapter Summary
Programming Exercise
5 Operators and Expressions
5.1 Conversions
Widening and Narrowing Primitive Conversions
Widening and Narrowing Reference Conversions
Boxing and Unboxing Conversions
Other Conversions
5.2 Type Conversion Contexts
Assignment Context
Method Invocation Context
Casting Context of the Unary Type Cast Operator: (type)
Numeric Promotion Context
5.3 Precedence and Associativity Rules for Operators
5.4 Evaluation Order of Operands
Left-Hand Operand Evaluation First
Operand Evaluation before Operation Execution
Left-to-Right Evaluation of Argument Lists
5.5 Representing Integers
Calculating Two’s Complement
Converting Binary Numbers to Decimals
Converting Decimals to Binary Numbers
Relationships among Binary, Octal, and Hexadecimal Numbers
5.6 The Simple Assignment Operator =
Assigning Primitive Values
Assigning References
Multiple Assignments
Type Conversions in an Assignment Context
Review Questions
5.7 Arithmetic Operators: *, /, %, +, -
Arithmetic Operator Precedence and Associativity
Evaluation Order in Arithmetic Expressions
Range of Numeric Values
Unary Arithmetic Operators: -, +
Multiplicative Binary Operators: *, /, %
Additive Binary Operators: +, -
Numeric Promotions in Arithmetic Expressions
Arithmetic Compound Assignment Operators: *=, /=, %=, +=, -=
Review Questions
5.8 The Binary String Concatenation Operator +
5.9 Variable Increment and Decrement Operators: ++, --
The Increment Operator ++
The Decrement Operator --
Review Questions
5.10 Boolean Expressions
5.11 Relational Operators: <, <=, >, >=
5.12 Equality
Primitive Data Value Equality: ==, !=
Object Reference Equality: ==, !=
Object Value Equality
5.13 Boolean Logical Operators: !, ^, &, |
Operand Evaluation for Boolean Logical Operators
Boolean Logical Compound Assignment Operators: &=, ^=, |=
5.14 Conditional Operators: &&, ||
Short-Circuit Evaluation
5.15 Integer Bitwise Operators: ~, &, |, ^
Bitwise Compound Assignment Operators: &=, ^=, |=
Review Questions
5.16 The Conditional Operator: ?:
5.17 Other Operators: new, [], instanceof, ->
Review Questions
Chapter Summary
Programming Exercise
6 Control Flow
6.1 Overview of Control Flow Statements
6.2 Selection Statements
The Simple if Statement
The if-else Statement
The switch Statement
Review Questions
6.3 Iteration Statements
The while Statement
The do-while Statement
The for(;;) Statement
The for(:) Statement
6.4 Transfer Statements
Labeled Statements
The break Statement
The continue Statement
The return Statement
Review Questions
6.5 Stack-Based Execution and Exception Propagation
6.6 Exception Types
The Exception Class
The RuntimeException Class
The Error Class
Checked and Unchecked Exceptions
Defining Customized Exceptions
6.7 Exception Handling: try, catch, and finally
The try Block
The catch Clause
The finally Clause
6.8 The throw Statement
6.9 The throws Clause
Overriding the throws Clause
6.10 Advantages of Exception Handling
Review Questions
Chapter Summary
Programming Exercises
7 Object-Oriented Programming
7.1 Single Implementation Inheritance
Relationships: is-a and has-a
The Supertype–Subtype Relationship
7.2 Overriding Methods
Instance Method Overriding
Covariant return in Overriding Methods
Overriding versus Overloading
7.3 Hiding Members
Field Hiding
Static Method Hiding
7.4 The Object Reference super
Review Questions
7.5 Chaining Constructors Using this() and super()
The this() Constructor Call
The super() Constructor Call
Review Questions
7.6 Interfaces
Defining Interfaces
Abstract Methods in Interfaces
Implementing Interfaces
Extending Interfaces
Interface References
Default Methods in Interfaces
Static Methods in Interfaces
Constants in Interfaces
Review Questions
7.7 Arrays and Subtyping
Arrays and Subtype Covariance
Array Store Check
7.8 Reference Values and Conversions
7.9 Reference Value Assignment Conversions
7.10 Method Invocation Conversions Involving References
Overloaded Method Resolution
7.11 Reference Casting and the instanceof Operator
The Cast Operator
The instanceof Operator
Review Questions
7.12 Polymorphism and Dynamic Method Lookup
7.13 Inheritance versus Aggregation
7.14 Basic Concepts in Object-Oriented Design
Encapsulation
Cohesion
Coupling
Review Questions