You are previewing A Programmer’s Guide to Java® SE 8 Oracle Certified Associate (OCA).
O'Reilly logo
A Programmer’s Guide to Java® SE 8 Oracle Certified Associate (OCA)

Book Description

Unique among Java tutorials, A Programmer’s Guide to Java® SE 8 Oracle Certified Associate (OCA): A Comprehensive Primer combines an integrated, expert introduction to Java SE 8 with comprehensive coverage of Oracle’s new Java SE 8 OCA exam 1Z0-808.

Based on Mughal and Rasmussen’s highly regarded guide to the original SCJP Certification, this streamlined volume has been thoroughly revised to reflect major changes in the new Java SE 8 OCA exam. It features an increased focus on analyzing code scenarios and not just individual language constructs, and each exam objective is thoroughly addressed, reflecting the latest Java SE 8 features, API classes, and best practices for effective programming.

Other features include

  • Summaries that clearly state what topics to read for each objective of the Java SE 8 OCA exam

  • Dozens of exam-relevant review questions with annotated answers

  • Programming exercises and solutions to put theory into practice

  • A mock exam with realistic questions to find out if you are ready to take the official exam

  • An introduction to essential concepts in object-oriented programming (OOP) and functional-style programming

  • In-depth coverage of declarations, access control, operators, flow control, OOP techniques, lambda expressions, key API classes, and more

  • Program output demonstrating expected results from complete Java programs

  • Advice on avoiding common pitfalls in writing Java code and on taking the certification exam

  • Extensive use of UML (Unified Modeling Language) for illustration purposes

  • Table of Contents

    1. About This E-Book
    2. Title Page
    3. Copyright Page
    4. Dedication Page
    5. Contents Overview
    6. Contents
    7. Figures
    8. Tables
    9. Examples
    10. Foreword
    11. Preface
      1. Writing This Book
      2. About This Book
      3. Using This Book
      4. Review Questions
      5. Chapter Summary
      6. Programming Exercises
        1. Mock Exam
        2. Java SE Platform API Documentation
      7. Book Website
      8. Request for Feedback
      9. About the Authors
        1. Khalid A. Mughal
        2. Rolf W. Rasmussen
      10. Acknowledgments
    12. 1. Basics of Java Programming
      1. 1.1 Introduction
      2. 1.2 Classes
        1. Declaring Members: Fields and Methods
      3. 1.3 Objects
        1. Class Instantiation, Reference Values, and References
        2. Object Aliases
      4. 1.4 Instance Members
        1. Invoking Methods
      5. 1.5 Static Members
      6. 1.6 Inheritance
      7. 1.7 Associations: Aggregation and Composition
      8. 1.8 Tenets of Java
      9. Review Questions
      10. 1.9 Java Programs
      11. 1.10 Sample Java Application
        1. Essential Elements of a Java Application
        2. Compiling and Running an Application
      12. 1.11 Program Output
        1. Formatted Output
      13. 1.12 The Java Ecosystem
        1. Object-Oriented Paradigm
        2. Interpreted: The JVM
        3. Architecture-Neutral and Portable Bytecode
        4. Simplicity
        5. Dynamic and Distributed
        6. Robust and Secure
        7. High Performance and Multithreaded
      14. Review Questions
      15. Chapter Summary
      16. Programming Exercise
    13. 2. Language Fundamentals
      1. 2.1 Basic Language Elements
        1. Lexical Tokens
        2. Identifiers
        3. Keywords
        4. Separators
        5. Literals
        6. Integer Literals
        7. Floating-Point Literals
        8. Underscores in Numerical Literals
        9. Boolean Literals
        10. Character Literals
        11. String Literals
        12. Whitespace
        13. Comments
      2. Review Questions
      3. 2.2 Primitive Data Types
        1. The Integer Types
        2. The char Type
        3. The Floating-Point Types
        4. The boolean Type
      4. Review Questions
      5. 2.3 Variable Declarations
        1. Declaring and Initializing Variables
        2. Reference Variables
      6. 2.4 Initial Values for Variables
        1. Default Values for Fields
        2. Initializing Local Variables of Primitive Data Types
        3. Initializing Local Reference Variables
        4. Lifetime of Variables
      7. Review Questions
      8. Chapter Summary
      9. Programming Exercise
    14. 3. Declarations
      1. 3.1 Class Declarations
      2. 3.2 Method Declarations
        1. Statements
        2. Instance Methods and the Object Reference this
        3. Method Overloading
      3. 3.3 Constructors
        1. The Default Constructor
        2. Overloaded Constructors
      4. Review Questions
      5. 3.4 Arrays
        1. Declaring Array Variables
        2. Constructing an Array
        3. Initializing an Array
        4. Using an Array
        5. Anonymous Arrays
        6. Multidimensional Arrays
        7. Sorting Arrays
        8. Searching Arrays
      6. Review Questions
      7. 3.5 Parameter Passing
        1. Passing Primitive Data Values
        2. Passing Reference Values
        3. Passing Arrays
        4. Array Elements as Actual Parameters
        5. final Parameters
      8. 3.6 Variable Arity Methods
        1. Calling a Variable Arity Method
        2. Variable Arity and Fixed Arity Method Calls
      9. 3.7 The main() Method
        1. Program Arguments
      10. 3.8 Enumerated Types
        1. Declaring Type-safe Enums
        2. Using Type-safe Enums
        3. Selected Methods for Enum Types
      11. Review Questions
      12. Chapter Summary
      13. Programming Exercise
    15. 4. Access Control
      1. 4.1 Java Source File Structure
      2. 4.2 Packages
        1. Defining Packages
        2. Using Packages
        3. Compiling Code into Packages
        4. Running Code from Packages
      3. 4.3 Searching for Classes
      4. Review Questions
      5. 4.4 Scope Rules
        1. Class Scope for Members
        2. Block Scope for Local Variables
      6. 4.5 Accessibility Modifiers for Top-Level Type Declarations
      7. 4.6 Non-Accessibility Modifiers for Classes
        1. abstract Classes
        2. final Classes
      8. Review Questions
      9. 4.7 Member Accessibility Modifiers
        1. public Members
        2. protected Members
        3. Default Accessibility for Members
        4. private Members
      10. Review Questions
      11. 4.8 Non-Accessibility Modifiers for Members
        1. static Members
        2. final Members
        3. abstract Methods
        4. synchronized Methods
        5. native Methods
        6. transient Fields
        7. volatile Fields
      12. Review Questions
      13. Chapter Summary
      14. Programming Exercise
    16. 5. Operators and Expressions
      1. 5.1 Conversions
        1. Widening and Narrowing Primitive Conversions
        2. Widening and Narrowing Reference Conversions
        3. Boxing and Unboxing Conversions
        4. Other Conversions
      2. 5.2 Type Conversion Contexts
        1. Assignment Context
        2. Method Invocation Context
        3. Casting Context of the Unary Type Cast Operator: (type)
        4. Numeric Promotion Context
      3. 5.3 Precedence and Associativity Rules for Operators
      4. 5.4 Evaluation Order of Operands
        1. Left-Hand Operand Evaluation First
        2. Operand Evaluation before Operation Execution
        3. Left-to-Right Evaluation of Argument Lists
      5. 5.5 Representing Integers
        1. Calculating Two’s Complement
        2. Converting Binary Numbers to Decimals
        3. Converting Decimals to Binary Numbers
        4. Relationships among Binary, Octal, and Hexadecimal Numbers
      6. 5.6 The Simple Assignment Operator =
        1. Assigning Primitive Values
        2. Assigning References
        3. Multiple Assignments
        4. Type Conversions in an Assignment Context
      7. Review Questions
      8. 5.7 Arithmetic Operators: *, /, %, +, -
        1. Arithmetic Operator Precedence and Associativity
        2. Evaluation Order in Arithmetic Expressions
        3. Range of Numeric Values
        4. Unary Arithmetic Operators: -, +
        5. Multiplicative Binary Operators: *, /, %
        6. Additive Binary Operators: +, -
        7. Numeric Promotions in Arithmetic Expressions
        8. Arithmetic Compound Assignment Operators: *=, /=, %=, +=, -=
      9. Review Questions
      10. 5.8 The Binary String Concatenation Operator +
      11. 5.9 Variable Increment and Decrement Operators: ++, --
        1. The Increment Operator ++
        2. The Decrement Operator --
      12. Review Questions
      13. 5.10 Boolean Expressions
      14. 5.11 Relational Operators: <, <=, >, >=
      15. 5.12 Equality
        1. Primitive Data Value Equality: ==, !=
        2. Object Reference Equality: ==, !=
        3. Object Value Equality
      16. 5.13 Boolean Logical Operators: !, ^, &, |
        1. Operand Evaluation for Boolean Logical Operators
        2. Boolean Logical Compound Assignment Operators: &=, ^=, |=
      17. 5.14 Conditional Operators: &&, ||
        1. Short-Circuit Evaluation
      18. 5.15 Integer Bitwise Operators: ~, &, |, ^
        1. Bitwise Compound Assignment Operators: &=, ^=, |=
      19. Review Questions
      20. 5.16 The Conditional Operator: ?:
      21. 5.17 Other Operators: new, [], instanceof, ->
      22. Review Questions
      23. Chapter Summary
      24. Programming Exercise
    17. 6. Control Flow
      1. 6.1 Overview of Control Flow Statements
      2. 6.2 Selection Statements
        1. The Simple if Statement
        2. The if-else Statement
        3. The switch Statement
      3. Review Questions
      4. 6.3 Iteration Statements
        1. The while Statement
        2. The do-while Statement
        3. The for(;;) Statement
        4. The for(:) Statement
      5. 6.4 Transfer Statements
        1. Labeled Statements
        2. The break Statement
        3. The continue Statement
        4. The return Statement
      6. Review Questions
      7. 6.5 Stack-Based Execution and Exception Propagation
      8. 6.6 Exception Types
        1. The Exception Class
        2. The RuntimeException Class
        3. The Error Class
        4. Checked and Unchecked Exceptions
        5. Defining Customized Exceptions
      9. 6.7 Exception Handling: try, catch, and finally
        1. The try Block
        2. The catch Clause
        3. The finally Clause
      10. 6.8 The throw Statement
      11. 6.9 The throws Clause
        1. Overriding the throws Clause
      12. 6.10 Advantages of Exception Handling
      13. Review Questions
      14. Chapter Summary
      15. Programming Exercises
    18. 7. Object-Oriented Programming
      1. 7.1 Single Implementation Inheritance
        1. Relationships: is-a and has-a
        2. The Supertype–Subtype Relationship
      2. 7.2 Overriding Methods
        1. Instance Method Overriding
        2. Covariant return in Overriding Methods
        3. Overriding versus Overloading
      3. 7.3 Hiding Members
        1. Field Hiding
        2. Static Method Hiding
      4. 7.4 The Object Reference super
      5. Review Questions
      6. 7.5 Chaining Constructors Using this() and super()
        1. The this() Constructor Call
        2. The super() Constructor Call
      7. Review Questions
      8. 7.6 Interfaces
        1. Defining Interfaces
        2. Abstract Methods in Interfaces
        3. Implementing Interfaces
        4. Extending Interfaces
        5. Interface References
        6. Default Methods in Interfaces
        7. Static Methods in Interfaces
        8. Constants in Interfaces
      9. Review Questions
      10. 7.7 Arrays and Subtyping
        1. Arrays and Subtype Covariance
        2. Array Store Check
      11. 7.8 Reference Values and Conversions
      12. 7.9 Reference Value Assignment Conversions
      13. 7.10 Method Invocation Conversions Involving References
        1. Overloaded Method Resolution
      14. 7.11 Reference Casting and the instanceof Operator
        1. The Cast Operator
        2. The instanceof Operator
      15. Review Questions
      16. 7.12 Polymorphism and Dynamic Method Lookup
      17. 7.13 Inheritance versus Aggregation
      18. 7.14 Basic Concepts in Object-Oriented Design
        1. Encapsulation
        2. Cohesion
        3. Coupling
      19. Review Questions
      20. Chapter Summary
      21. Programming Exercises
    19. 8. Fundamental Classes
      1. 8.1 Overview of the java.lang Package
      2. 8.2 The Object Class
      3. Review Questions
      4. 8.3 The Wrapper Classes
        1. Common Wrapper Class Constructors
        2. Common Wrapper Class Utility Methods
        3. Numeric Wrapper Classes
        4. The Character Class
        5. The Boolean Class
      5. Review Questions
      6. 8.4 The String Class
        1. Immutability
        2. Creating and Initializing Strings
        3. The CharSequence Interface
        4. Reading Characters from a String
        5. Comparing Strings
        6. Character Case in a String
        7. Concatenation of Strings
        8. Joining of CharSequence Objects
        9. Searching for Characters and Substrings
        10. Extracting Substrings
        11. Converting Primitive Values and Objects to Strings
        12. Formatted Strings
      7. Review Questions
      8. 8.5 The StringBuilder and StringBuffer Classes
        1. Thread-Safety
        2. Mutability
        3. Constructing String Builders
        4. Reading and Changing Characters in String Builders
        5. Constructing Strings from String Builders
        6. Appending, Inserting, and Deleting Characters in String Builders
        7. Controlling String Builder Capacity
      9. Review Questions
      10. Chapter Summary
      11. Programming Exercises
    20. 9. Object Lifetime
      1. 9.1 Garbage Collection
      2. 9.2 Reachable Objects
      3. 9.3 Facilitating Garbage Collection
      4. 9.4 Object Finalization
      5. 9.5 Finalizer Chaining
      6. 9.6 Invoking Garbage Collection Programmatically
      7. Review Questions
      8. 9.7 Initializers
      9. 9.8 Field Initializer Expressions
        1. Declaration Order of Initializer Expressions
      10. 9.9 Static Initializer Blocks
        1. Declaration Order of Static Initializers
      11. 9.10 Instance Initializer Blocks
        1. Declaration Order of Instance Initializers
      12. 9.11 Constructing Initial Object State
      13. Review Questions
      14. Chapter Summary
    21. 10. The ArrayList<E> Class and Lambda Expressions
      1. 10.1 The ArrayList<E> Class
        1. Lists
        2. Declaring References and Constructing ArrayLists
        3. Modifying an ArrayList
        4. Querying an ArrayList
        5. Traversing an ArrayList
        6. Converting an ArrayList to an Array
        7. Sorting an ArrayList
        8. Arrays versus ArrayList
      2. Review Questions
      3. 10.2 Lambda Expressions
        1. Behavior Parameterization
        2. Functional Interfaces
        3. Defining Lambda Expressions
        4. Type Checking and Execution of Lambda Expressions
        5. Filtering Revisited: The Predicate<T> Functional Interface
      4. Review Questions
      5. Chapter Summary
      6. Programming Exercise
    22. 11. Date and Time
      1. 11.1 Basic Date and Time Concepts
      2. 11.2 Working with Temporal Classes
        1. Creating Temporal Objects
        2. Querying Temporal Objects
        3. Comparing Temporal Objects
        4. Creating Modified Copies of Temporal Objects
        5. Temporal Arithmetic
      3. 11.3 Working with Periods
        1. Creating Periods
        2. Querying Periods
        3. Creating Modified Copies of Periods
        4. More Temporal Arithmetic
      4. Review Questions
      5. 11.4 Formatting and Parsing
        1. Default Formatters
        2. Predefined Formatters
        3. Localized Formatters
        4. Customized Formatters
      6. Review Questions
      7. Chapter Summary
      8. Programming Exercise
    23. Appendix A. Taking the Java SE 8 Programmer I Exam
      1. A.1 Preparing for the Exam
      2. A.2 Registering for the Exam
        1. Contact Information
        2. Obtaining an Exam Voucher
        3. Signing Up for the Test
        4. After Taking the Exam
      3. A.3 How the Exam Is Conducted
        1. The Testing Locations
        2. Utilizing the Allotted Time
        3. The Exam Program
        4. The Exam Result
      4. A.4 The Questions
        1. Assumptions about the Exam Questions
        2. Types of Questions Asked
        3. Types of Answers Expected
        4. Topics Covered by the Questions
    24. Appendix B. Exam Topics: Java SE 8 Programmer I
    25. Appendix C. Annotated Answers to Review Questions
      1. 1 Basics of Java Programming
      2. 2 Language Fundamentals
      3. 3 Declarations
      4. 4 Access Control
      5. 5 Operators and Expressions
      6. 6 Control Flow
      7. 7 Object-Oriented Programming
      8. 8 Fundamental Classes
      9. 9 Object Lifetime
      10. 10 The ArrayList<E> Class and Lambda Expressions
      11. 11 Date and Time
    26. Appendix D. Solutions to Programming Exercises
      1. 1 Basics of Java Programming
      2. 2 Language Fundamentals
      3. 3 Declarations
      4. 4 Access Control
      5. 5 Operators and Expressions
      6. 6 Control Flow
      7. 7 Object-Oriented Programming
      8. 8 Fundamental Classes
      9. 9 Object Lifetime
      10. 10 The ArrayList<E> Class and Lambda Expressions
      11. 11 Date and Time
    27. Appendix E. Mock Exam: Java SE 8 Programmer I
      1. Questions
    28. Appendix F. Annotated Answers to Mock Exam I
      1. Annotated Answers
    29. Index
    30. Code Snippets