You are previewing Beginning Java 8 Fundamentals.
O'Reilly logo
Beginning Java 8 Fundamentals

Book Description

"

Beginning Java 8 Fundamentals provides a comprehensive approach to learning the Java programming language, especially the object-oriented fundamentals necessary at all levels of Java development.

Author Kishori Sharan provides over 90 diagrams and 240 complete programs to help beginners and intermediate level programmers learn the topics faster. Starting with basic programming concepts, the author walks you through writing your first Java program step-by-step. Armed with that practical experience, you'll be ready to learn the core of the Java language.

The book continues with a series of foundation topics, including using data types, working with operators, and writing statements in Java. These basics lead onto the heart of the Java language: object-oriented programming. By learning topics such as classes, objects, interfaces, and inheritance you'll have a good understanding of Java's object-oriented model.

The final collection of topics takes what you've learned and turns you into a real Java programmer. You'll see how to take the power of object-oriented programming and write programs that can handle errors and exceptions, process strings and dates, format data, and work with arrays to manipulate data.

""

"

Table of Contents

  1. Cover
  2. Title
  3. Copyright
  4. Dedication
  5. Contents at a Glance
  6. Contents
  7. Foreword
  8. About the Author
  9. About the Technical Reviewer
  10. Acknowledgments
  11. Introduction
  12. Chapter 1: Programming Concepts
    1. What Is Programming?
    2. Components of a Programming Language
    3. Programming Paradigms
      1. Imperative Paradigm
      2. Procedural Paradigm
      3. Declarative Paradigm
      4. Functional Paradigm
      5. Logic Paradigm
      6. Object-Oriented Paradigm
    4. What Is Java?
    5. The Object-Oriented Paradigm and Java
      1. Abstraction
      2. Encapsulation and Information Hiding
      3. Inheritance
      4. Polymorphism
    6. Summary
  13. Chapter 2: Writing Java Programs
    1. What is a Java Program?
    2. System Requirements
    3. Writing the Source Code
      1. Package Declaration
      2. Import Declarations
      3. Class Declaration
    4. Compiling the Source Code
    5. Running the Compiled Code
    6. Using NetBeans IDE
      1. Creating a Java Project
      2. Adding Classes to the Project
      3. Customizing Project Properties
      4. Opening an Existing NetBeans Project
    7. Behind the Scenes
    8. Summary
  14. Chapter 3: Data Types
    1. What Is a Data Type?
    2. What Is an Identifier?
    3. Data Types in Java
    4. Primitive Data Types in Java
      1. Integral Data Types
      2. Floating-Point Data Types
    5. Underscores in Numeric Literals
    6. Java Compiler and Unicode Escape Sequence
    7. A Short Break
    8. Binary Representation of Integers
      1. Diminished Radix Complement
      2. Radix Complement
    9. Binary Representation of Floating-Point Numbers
      1. 32-bit Single-Precision Floating-Point Format
    10. Special Floating-Point Numbers
      1. Signed Zeros
      2. Signed Infinities
      3. NaN
      4. Denormals
    11. Rounding Modes
      1. Rounding Toward Zero
      2. Rounding Toward Positive Infinity
      3. Rounding Toward Negative Infinity
      4. Rounding Toward Nearest
    12. IEEE Floating-Point Exceptions
      1. Division by Zero Exception
      2. Invalid Operation Exception
      3. Overflow Exception
      4. Underflow Exception
      5. Inexact Exception
    13. Java and IEEE Floating-Point Standards
    14. Little-Endian and Big-Endian
    15. Summary
  15. Chapter 4: Operators
    1. What Is an Operator?
    2. Assignment Operator (=)
    3. Declaration, Initialization, and Assignment
    4. Arithmetic Operators
      1. Addition Operator (+)
      2. Subtraction Operator (-)
      3. Multiplication Operator (*)
      4. Division Operator (/)
      5. Modulus Operator (%)
      6. Unary Plus Operator (+)
      7. Unary Minus Operator (-)
      8. Compound Arithmetic Assignment Operators
      9. Increment (++) and Decrement (--) Operators
    5. String Concatenation Operator (+)
    6. Relational Operators
      1. Equality Operator (==)
      2. Inequality Operator (!=)
      3. Greater Than Operator (>)
      4. Greater Than or Equal to Operator (>=)
      5. Less Than Operator (<)
      6. Less Than or Equal to Operator (<=)
    7. Boolean Logical Operators
      1. Logical NOT Operator (!)
      2. Logical Short-Circuit AND Operator (&&)
      3. Logical AND Operator (&)
      4. Logical Short-Circuit OR Operator (||)
      5. Logical OR Operator (|)
      6. Logical XOR Operator (^)
      7. Compound Boolean Logical Assignment Operators
    8. Ternary Operator (? :)
    9. Operator Precedence
    10. Bitwise Operators
    11. Summary
  16. Chapter 5: Statements
    1. What Is a Statement?
    2. Types of Statements
      1. Declaration Statement
      2. Expression Statement
      3. Control Flow Statement
    3. A Block Statement
    4. The if-else Statement
    5. The switch Statement
    6. The for Statement
      1. Initialization
      2. Condition-expression
      3. Expression-list
    7. The for-each Statement
    8. The while Statement
    9. The do-while Statement
    10. The break Statement
    11. The continue Statement
    12. An Empty Statement
    13. Summary
  17. Chapter 6: Classes and Objects
    1. What Is a Class?
    2. Declaring a Class
    3. Declaring Fields in a Class
    4. Creating Instances of a Class
    5. The null Reference Type
    6. Using Dot Notation to Access Fields of a Class
    7. Default Initialization of Fields
    8. Access Level Modifiers for a Class
    9. Import Declarations
      1. Single-Type Import Declaration
      2. Import-on-Demand Declaration
      3. Import Declarations and Type Search Order
      4. Automatic Import Declarations
    10. Static Import Declarations
    11. Declaring Methods of a Class
    12. Local Variables
    13. Instance Method and Class Method
    14. Invoking a Method
    15. The Special main() Method
    16. What Is this?
    17. Access Levels for Class Members
    18. Access Level—A Case Study
    19. Parameter Passing Mechanisms
      1. Pass By Value
      2. Pass By Constant Value
      3. Pass By Reference
      4. Pass By Reference Value
      5. Pass by Constant Reference Value
      6. Pass by Result
      7. Pass by Value Result
      8. Pass By Name
      9. Pass by Need
    20. Parameter Passing Mechanisms in Java
    21. Constructors
      1. Declaring a Constructor
      2. Overloading a Constructor
      3. Writing Code for a Constructor
      4. Calling a Constructor from another Constructor
      5. Using a return Statement Inside a Constructor
      6. Access Level Modifier for a Constructor
      7. Default Constructor
      8. A static Constructor
    22. Instance Initialization Block
    23. static Initialization Block
    24. The final Keyword
      1. final Local Variables
      2. final Parameters
      3. final Instance Variables
      4. final Class Variables
      5. final Reference Variables
      6. Compile-time vs. Runtime final Variables
    25. What is a varargs Method?
      1. Overloading a Varargs Method
      2. Varargs Methods and the main() Method
    26. Generic Classes
    27. Summary
  18. Chapter 7: The Object and Objects Classes
    1. The Object Class
      1. Rule #1
      2. Rule #2
    2. What Is the Class of an Object?
    3. Computing Hash Code of an Object
    4. Comparing Objects for Equality
    5. String Representation of an Object
    6. Cloning Objects
    7. Finalizing an Object
    8. Immutable Objects
    9. The Objects Class
    10. Summary
  19. Chapter 8: Wrapper Classes
    1. Wrapper Classes
      1. Numeric Wrapper Classes
      2. The Character Wrapper Class
      3. The Boolean Wrapper Class
    2. Unsigned Numeric Operations
    3. Autoboxing and Unboxing
    4. Beware of Null Values
    5. Overloaded Methods and Autoboxing/Unboxing
    6. Comparison Operators and AutoBoxing/Unboxing
    7. Collections and Autoboxing/Unboxing
    8. Summary
  20. Chapter 9: Exception Handling
    1. What Is an Exception?
    2. An Exception Is an Object
    3. Using a try-catch Block
    4. Transfer of Control
    5. Exception Class Hierarchy
    6. Arranging Multiple catch Blocks
    7. Checked and Unchecked Exceptions
    8. Checked Exception - Catch or Declare
    9. Checked Exceptions and Initializers
    10. Throwing an Exception
    11. Creating an Exception Class
    12. The finally Block
    13. Rethrowing an Exception
    14. Analysis of Rethrown Exceptions
    15. Throwing too Many Exceptions
    16. Accessing the Stack of a Thread
    17. The try-with-resources Block
    18. A Multi-Catch Block
    19. Summary
  21. Chapter 10: Assertions
    1. What Is an Assertion?
    2. Testing Assertions
    3. Enabling/Disabling Assertions
    4. Using Assertions
    5. Checking for Assertion Status
    6. Summary
  22. Chapter 11: Strings
    1. What is a String?
    2. String Literals
    3. Escape Sequence Characters in String Literals
    4. Unicode Escapes in String Literals
    5. What is a CharSequence?
    6. Creating String Objects
    7. Length of a String
    8. String Literals Are String Objects
    9. String Objects Are Immutable
    10. Comparing Two Strings
    11. String Pool
    12. String Operations
      1. Getting the Character at an Index
      2. Testing Strings for Equality
      3. Testing a String to be Empty
      4. Changing the Case
      5. Searching for a String
      6. Representing Values as Strings
      7. Getting a Substring
      8. Trimming a String
      9. Replacing Part of a String
      10. Matching Start and End of a String
    13. Splitting and Joining Strings
    14. Strings in a switch Statement
    15. Testing a String for Palindrome
    16. StringBuilder and StringBuffer
    17. String Concatenation Operator (+)
    18. Language-Sensitive String Comparison
    19. Summary
  23. Chapter 12: Dates and Times
    1. The Date-Time API
    2. Design Principles
    3. A Quick Example
    4. Evolution of Timekeeping
    5. Time Zones and Daylight Saving Time
    6. Calendar Systems
      1. The Julian Calendar
      2. The Gregorian Calendar
    7. ISO-8601 Standards for Datetime
    8. Exploring the New Date-Time API
      1. The ofXXX() Methods
      2. The from() Methods
      3. The withXXX() Methods
      4. The getXXX() Methods
      5. The toXXX() Methods
      6. The atXXX() Methods
      7. The plusXXX() and minusXXX() Methods
      8. The multipliedBy(), dividedBy(), and negated() Methods
    9. Instants and Durations
    10. Human-Scale Time
      1. The ZoneOffset Class
      2. The ZoneId Class
      3. Useful Datetime-Related Enums
      4. Local Date, Time, and Datetime
      5. Offset Time and Datetime
      6. Zoned Datetime
    11. Same Instant, Different Times
    12. Clocks
    13. Periods
    14. Period Between Two Dates and Times
    15. Partials
    16. Adjusting Dates
    17. Querying Datetime Objects
    18. Non-ISO Calendar Systems
    19. Formatting Dates and Times
      1. Using Predefined Formatters
      2. Using the format() Method of Datetime Classes
      3. Using User-Defined Patterns
      4. Using Locale Specific Formats
      5. Using the DateTimeFormatterBuilder Class
    20. Parsing Dates and Times
    21. Legacy Datetime Classes
      1. The Date Class
      2. The Calendar Class
      3. The add() Method
      4. The roll() Method
    22. Interoperability with Legacy Datetime Classes
    23. Summary
  24. Chapter 13: Formatting Data
    1. Formatting Dates
    2. Formatting Numbers
    3. Printf-style Formatting
      1. The Big Picture
      2. The Details
      3. Referencing an Argument inside a Format Specifier
      4. Using Flags in a Format Specifier
      5. Conversion Characters
    4. Summary
  25. Chapter 14: Regular Expressions
    1. What Is a Regular Expression?
    2. Metacharacters
    3. Character Classes
      1. Predefined Character Classes
    4. More Powers to Regular Expressions
      1. Compiling Regular Expressions
      2. Creating a Matcher
      3. Matching the Pattern
    5. Beware of Backslashes
    6. Quantifiers in Regular Expressions
    7. Matching Boundaries
    8. Groups and Back Referencing
    9. Using Named Groups
    10. Resetting the Matcher
    11. Final Words on E-mail Validations
    12. Find-and-Replace Using Regular Expressions
    13. Summary
  26. Chapter 15: Arrays
    1. What Is an Array?
    2. Arrays Are Objects
    3. Accessing Array Elements
    4. Length of an Array
    5. Initializing Array Elements
    6. Beware of Reference Type Arrays
    7. Explicit Array Initialization
    8. Limitations of Using Arrays
    9. Variable-Length Arrays
    10. Passing an Array as a Parameter
      1. Array Parameter Reference
      2. Elements of the Array Parameter
      3. The Object Referred by the Array Parameter Elements
    11. Command-Line Arguments
    12. Multi-Dimensional Arrays
      1. Accessing Elements of a Multi-Dimensional Array
      2. Initializing Multi-Dimensional Arrays
    13. Enhanced for Loop for Arrays
    14. Array Declaration Syntax
    15. Runtime Array Bounds Checks
    16. What Is the Class of an Array Object
    17. Array Assignment Compatibility
    18. Converting an ArrayList/Vector to an Array
    19. Summary
  27. Chapter 16: Inheritance
    1. What is Inheritance?
    2. Object Class is the Default Superclass
    3. Inheritance and Hierarchical Relationship
    4. What Is Inherited by a Subclass?
    5. Upcasting and Downcasting
    6. The instanceof Operator
    7. Binding
      1. Early Binding
      2. Late Binding
    8. Method Overriding
      1. Method Overriding Rule #1
      2. Method Overriding Rule #2
      3. Method Overriding Rule #3
      4. Method Overriding Rule #4
      5. Method Overriding Rule #5
      6. Method Overriding Rule #6
    9. Accessing Overridden Method
    10. Method Overloading
    11. Inheritance and Constructors
    12. Method Hiding
    13. Field Hiding
    14. Disabling Inheritance
    15. Abstract Classes and Methods
    16. Method Overriding and Generic Method Signatures
    17. Typo Danger in Method Overriding
    18. Is-a, has-a, and part-of Relationships
    19. No Multiple Inheritance of Classes
    20. Summary
  28. Chapter 17: Interfaces
    1. What Is an Interface?
      1. Proposed Solution #1
      2. Proposed Solution #2
      3. Proposed Solution #3
      4. An Ideal Solution
    2. Declaring an Interface
    3. Declaring Interface Members
      1. Constant Fields Declarations
      2. Methods Declarations
      3. Nested Type Declarations
    4. An Interface Defines a New Type
    5. Implementing an Interface
    6. Implementing Interface Methods
    7. Implementing Multiple Interfaces
    8. Partially Implementing an Interface
    9. The Supertype-Subtype Relationship
    10. Interface Inheritance
    11. The Superinterface-Subinterface Relationship
    12. Inheriting Conflicting Implementations
      1. The Superclass Always Wins
      2. The Most Specific Superinterface Wins
      3. The Class Must Override the Conflicting Method
    13. The instanceof Operator
    14. Marker Interfaces
    15. Functional Interfaces
    16. Comparing Objects
      1. Using the Comparable Interface
      2. Using the Comparator Interface
    17. Polymorphism—One Object, Many Views
    18. Dynamic Binding and Interfaces
    19. Summary
  29. Chapter 18: Enum Types
    1. What Is an Enum Type?
    2. Superclass of an Enum Type
    3. Using Enum Types in switch Statements
    4. Associating Data and Methods to Enum Constants
    5. Associating a Body to an Enum Constant
    6. Comparing Two Enum Constants
    7. Nested Enum Types
    8. Implementing an Interface to an Enum Type
    9. Reverse Lookup for Enum Constants
    10. Range of Enum Constants
    11. Summary
  30. Appendix A: Character Encodings
    1. ASCII
    2. 8-bit Character Sets
    3. Universal Multiple-Octet Coded Character Set (UCS)
      1. UCS-2
      2. UCS-4
      3. UTF-16 (UCS Transformation Format 16)
      4. UTF-8 (UCS Transformation Format 8)
    4. Java and Character Encodings
  31. Appendix B: Documentation Comments
    1. Writing Documentation Comments
    2. List of Block and Inline Tags
      1. @author <author-name(s)>
      2. @deprecated <explanation-text>
      3. @exception <class-name> <description>
      4. @param <parameter-name> <description>
      5. @return <description>
      6. @see <reference>
      7. @serial <field-description or include/exclude>
      8. @serialData <data-description>
      9. @serialField <field-name> <field-type> <field-description>
      10. @since <description>
      11. @throws <class-name> <description>
      12. @version <version-text>
      13. {@code <text>}
      14. {@docRoot}
      15. {@inheritDoc}
      16. {@link <package.class#member> <label>}
      17. {@linkplain <package.class#member> <label>}
      18. {@literal <text>}
      19. {@value <package.class#field>}
    3. Documenting Packages
      1. com/jdojo/utility/package-info.java file
      2. com/jdojo/utility/package.html file
    4. Overview Documentation
    5. Including Unprocessed Files in Documentation
    6. Skipping Source Files Processing
    7. An Example of Documentation Comments
    8. Running the javadoc Tool
    9. Generated Documentation Files
    10. Viewing Generated HTML Documentation
    11. Summary
  32. Appendix C: Compact Profiles
    1. Enhancement in the javac Command
    2. Enhancement in Java API Documentation
    3. Checking for Profile Dependencies
    4. Creating Custom JREs Using the EJDK
      1. Installing EJDK
      2. Running the jrecreate Command
      3. Deploying the Custom JRE
      4. Running the Custom JRE
  33. Index