About ................................................................................................................................................................................... 1
Chapter 1: Getting started with Java Language .......................................................................................... 2
Section 1.1: Creating Your First Java Program ........................................................................................................... 2
Chapter 2: Type Conversion .................................................................................................................................... 7
Section 2.1: Numeric primitive casting ......................................................................................................................... 7
Section 2.2: Basic Numeric Promotion ........................................................................................................................ 7
Section 2.3: Non-numeric primitive casting ................................................................................................................ 7
Section 2.4: Object casting ........................................................................................................................................... 8
Section 2.5: Testing if an object can be cast using instanceof ................................................................................. 8
Chapter 3: Getters and Setters ............................................................................................................................. 9
Section 3.1: Using a setter or getter to implement a constraint ............................................................................... 9
Section 3.2: Why Use Getters and Setters? ................................................................................................................ 9
Section 3.3: Adding Getters and Setters ................................................................................................................... 10
Chapter 4: Reference Data Types .................................................................................................................... 12
Section 4.1: Dereferencing .......................................................................................................................................... 12
Section 4.2: Instantiating a reference type ............................................................................................................... 12
Chapter 5: Java Compiler - 'javac' .................................................................................................................... 13
Section 5.1: The 'javac' command - getting started ................................................................................................ 13
Section 5.2: Compiling for a dierent version of Java ............................................................................................ 15
Chapter 6: Documenting Java Code ................................................................................................................. 17
Section 6.1: Building Javadocs From the Command Line ....................................................................................... 17
Section 6.2: Class Documentation ............................................................................................................................. 17
Section 6.3: Method Documentation ......................................................................................................................... 18
Section 6.4: Package Documentation ....................................................................................................................... 19
Section 6.5: Links ......................................................................................................................................................... 19
Section 6.6: Code snippets inside documentation ................................................................................................... 20
Section 6.7: Field Documentation .............................................................................................................................. 21
Section 6.8: Inline Code Documentation ................................................................................................................... 21
Chapter 7: Command line Argument Processing ....................................................................................... 23
Section 7.1: Argument processing using GWT ToolBase ......................................................................................... 23
Section 7.2: Processing arguments by hand ............................................................................................................ 23
Chapter 8: The Java Command - 'java' and 'javaw' ................................................................................. 26
Section 8.1: Entry point classes .................................................................................................................................. 26
Section 8.2: Troubleshooting the 'java' command .................................................................................................. 26
Section 8.3: Running a Java application with library dependencies ..................................................................... 28
Section 8.4: Java Options ........................................................................................................................................... 29
Section 8.5: Spaces and other special characters in arguments ........................................................................... 30
Section 8.6: Running an executable JAR file ............................................................................................................ 32
Section 8.7: Running a Java applications via a "main" class ................................................................................. 32
Chapter 9: Date Class ............................................................................................................................................... 34
Section 9.1: Convert java.util.Date to java.sql.Date .................................................................................................. 34
Section 9.2: A basic date output ................................................................................................................................ 34
Section 9.3: Java 8 LocalDate and LocalDateTime objects ................................................................................... 35
Section 9.4: Creating a Specific Date ........................................................................................................................ 36
Section 9.5: Converting Date to a certain String format ......................................................................................... 36
Section 9.6: LocalTime ................................................................................................................................................ 37
, Section 9.7: Convert formatted string representation of date to Date object ..................................................... 37
Section 9.8: Creating Date objects ............................................................................................................................ 38
Section 9.9: Comparing Date objects ........................................................................................................................ 38
Section 9.10: Converting String into Date ................................................................................................................. 41
Section 9.11: Time Zones and java.util.Date .............................................................................................................. 41
Chapter 10: Dates and Time (java.time.*) ...................................................................................................... 43
Section 10.1: Calculate Dierence between 2 LocalDates ....................................................................................... 43
Section 10.2: Date and time ........................................................................................................................................ 43
Section 10.3: Operations on dates and times ........................................................................................................... 43
Section 10.4: Instant ..................................................................................................................................................... 43
Section 10.5: Usage of various classes of Date Time API ....................................................................................... 44
Section 10.6: Date Time Formatting .......................................................................................................................... 46
Section 10.7: Simple Date Manipulations ................................................................................................................... 46
Chapter 11: LocalTime ............................................................................................................................................... 48
Section 11.1: Amount of time between two LocalTime ............................................................................................. 48
Section 11.2: Intro ......................................................................................................................................................... 49
Section 11.3: Time Modification ................................................................................................................................... 49
Section 11.4: Time Zones and their time dierence ................................................................................................. 49
Chapter 12: Literals .................................................................................................................................................... 51
Section 12.1: Using underscore to improve readability ............................................................................................ 51
Section 12.2: Hexadecimal, Octal and Binary literals ............................................................................................... 51
Section 12.3: Boolean literals ...................................................................................................................................... 52
Section 12.4: String literals .......................................................................................................................................... 52
Section 12.5: The Null literal ........................................................................................................................................ 53
Section 12.6: Escape sequences in literals ................................................................................................................ 53
Section 12.7: Character literals ................................................................................................................................... 54
Section 12.8: Decimal Integer literals ......................................................................................................................... 54
Section 12.9: Floating-point literals ............................................................................................................................ 55
Chapter 13: Operators .............................................................................................................................................. 58
Section 13.1: The Increment/Decrement Operators (++/--) .................................................................................... 58
Section 13.2: The Conditional Operator (? :) ............................................................................................................. 58
Section 13.3: The Bitwise and Logical Operators (~, &, |, ^) ..................................................................................... 60
Section 13.4: The String Concatenation Operator (+) .............................................................................................. 61
Section 13.5: The Arithmetic Operators (+, -, *, /, %) ................................................................................................ 63
Section 13.6: The Shift Operators (<<, >> and >>>) ................................................................................................... 65
Section 13.7: The Instanceof Operator ...................................................................................................................... 66
Section 13.8: The Assignment Operators (=, +=, -=, *=, /=, %=, <<=, >>= , >>>=, &=, |= and ^=) ................................. 67
Section 13.9: The conditional-and and conditional-or Operators ( && and || ) ..................................................... 69
Section 13.10: The Relational Operators (<, <=, >, >=) ............................................................................................... 70
Section 13.11: The Equality Operators (==, !=) ............................................................................................................. 71
Section 13.12: The Lambda operator ( -> ) ................................................................................................................ 73
Chapter 14: Primitive Data Types ...................................................................................................................... 74
Section 14.1: The char primitive .................................................................................................................................. 74
Section 14.2: Primitive Types Cheatsheet .................................................................................................................. 74
Section 14.3: The float primitive ................................................................................................................................. 75
Section 14.4: The int primitive ..................................................................................................................................... 76
Section 14.5: Converting Primitives ............................................................................................................................ 77
Section 14.6: Memory consumption of primitives vs. boxed primitives .................................................................. 77
Section 14.7: The double primitive ............................................................................................................................. 78
Section 14.8: The long primitive .................................................................................................................................. 79
, Section 14.9: The boolean primitive ........................................................................................................................... 80
Section 14.10: The byte primitive ................................................................................................................................ 80
Section 14.11: Negative value representation ............................................................................................................ 80
Section 14.12: The short primitive ............................................................................................................................... 81
Chapter 15: Constructors ........................................................................................................................................ 83
Section 15.1: Default Constructor ............................................................................................................................... 83
Section 15.2: Call parent constructor ......................................................................................................................... 84
Section 15.3: Constructor with Arguments ................................................................................................................ 85
Chapter 16: Object Class Methods and Constructor ................................................................................. 87
Section 16.1: hashCode() method ............................................................................................................................... 87
Section 16.2: toString() method .................................................................................................................................. 89
Section 16.3: equals() method .................................................................................................................................... 90
Section 16.4: wait() and notify() methods ................................................................................................................. 92
Section 16.5: getClass() method ................................................................................................................................. 94
Section 16.6: clone() method ...................................................................................................................................... 95
Section 16.7: Object constructor ................................................................................................................................. 96
Section 16.8: finalize() method ................................................................................................................................... 97
Chapter 17: Annotations .......................................................................................................................................... 99
Section 17.1: The idea behind Annotations ................................................................................................................ 99
Section 17.2: Defining annotation types .................................................................................................................... 99
Section 17.3: Runtime annotation checks via reflection ........................................................................................ 101
Section 17.4: Built-in annotations ............................................................................................................................. 101
Section 17.5: Compile time processing using annotation processor .................................................................... 104
Section 17.6: Repeating Annotations ....................................................................................................................... 108
Section 17.7: Inherited Annotations .......................................................................................................................... 109
Section 17.8: Getting Annotation values at run-time ............................................................................................. 110
Section 17.9: Annotations for 'this' and receiver parameters ............................................................................... 111
Section 17.10: Add multiple annotation values ....................................................................................................... 112
Chapter 18: Immutable Class .............................................................................................................................. 113
Section 18.1: Example without mutable refs ............................................................................................................ 113
Section 18.2: What is the advantage of immutability? .......................................................................................... 113
Section 18.3: Rules to define immutable classes .................................................................................................... 113
Section 18.4: Example with mutable refs ................................................................................................................. 114
Chapter 19: Immutable Objects ......................................................................................................................... 115
Section 19.1: Creating an immutable version of a type using defensive copying .............................................. 115
Section 19.2: The recipe for an immutable class .................................................................................................... 115
Section 19.3: Typical design flaws which prevent a class from being immutable .............................................. 116
Chapter 20: Visibility (controlling access to members of a class) .................................................. 120
Section 20.1: Private Visibility ................................................................................................................................... 120
Section 20.2: Public Visibility .................................................................................................................................... 120
Section 20.3: Package Visibility ............................................................................................................................... 121
Section 20.4: Protected Visibility .............................................................................................................................. 121
Section 20.5: Summary of Class Member Access Modifiers ................................................................................. 122
Section 20.6: Interface members ............................................................................................................................ 122
Chapter 21: Generics ................................................................................................................................................ 123
Section 21.1: Creating a Generic Class ..................................................................................................................... 123
Section 21.2: Deciding between `T`, `? super T`, and `? extends T` ........................................................................ 125
Section 21.3: The Diamond ....................................................................................................................................... 127
Section 21.4: Declaring a Generic Method .............................................................................................................. 127
, Section 21.5: Requiring multiple upper bounds ("extends A & B") ....................................................................... 128
Section 21.6: Obtain class that satisfies generic parameter at runtime .............................................................. 128
Section 21.7: Benefits of Generic class and interface ............................................................................................ 129
Section 21.8: Instantiating a generic type ............................................................................................................... 130
Section 21.9: Creating a Bounded Generic Class ................................................................................................... 130
Section 21.10: Referring to the declared generic type within its own declaration .............................................. 132
Section 21.11: Binding generic parameter to more than 1 type ............................................................................. 133
Section 21.12: Using Generics to auto-cast ............................................................................................................. 134
Section 21.13: Use of instanceof with Generics ....................................................................................................... 134
Section 21.14: Dierent ways for implementing a Generic Interface (or extending a Generic Class) .............. 136
Chapter 22: Classes and Objects ...................................................................................................................... 138
Section 22.1: Overloading Methods ......................................................................................................................... 138
Section 22.2: Explaining what is method overloading and overriding ................................................................ 139
Section 22.3: Constructors ........................................................................................................................................ 141
Section 22.4: Initializing static final fields using a static initializer ........................................................................ 142
Section 22.5: Basic Object Construction and Use .................................................................................................. 143
Section 22.6: Simplest Possible Class ...................................................................................................................... 145
Section 22.7: Object Member vs Static Member .................................................................................................... 145
Chapter 23: Local Inner Class ............................................................................................................................. 147
Section 23.1: Local Inner Class ................................................................................................................................. 147
Chapter 24: Nested and Inner Classes .......................................................................................................... 148
Section 24.1: A Simple Stack Using a Nested Class ............................................................................................... 148
Section 24.2: Static vs Non Static Nested Classes ................................................................................................. 148
Section 24.3: Access Modifiers for Inner Classes ................................................................................................... 150
Section 24.4: Anonymous Inner Classes ................................................................................................................. 151
Section 24.5: Create instance of non-static inner class from outside ................................................................. 152
Section 24.6: Method Local Inner Classes .............................................................................................................. 153
Section 24.7: Accessing the outer class from a non-static inner class ................................................................ 153
Chapter 25: The java.util.Objects Class ......................................................................................................... 155
Section 25.1: Basic use for object null check .......................................................................................................... 155
Section 25.2: Objects.nonNull() method reference use in stream api ................................................................. 155
Chapter 26: Default Methods ............................................................................................................................. 156
Section 26.1: Basic usage of default methods ........................................................................................................ 156
Section 26.2: Accessing overridden default methods from implementing class ............................................... 156
Section 26.3: Why use Default Methods? ............................................................................................................... 157
Section 26.4: Accessing other interface methods within default method ........................................................... 157
Section 26.5: Default method multiple inheritance collision ................................................................................. 158
Section 26.6: Class, Abstract class and Interface method precedence .............................................................. 159
Chapter 27: Packages ............................................................................................................................................ 161
Section 27.1: Using Packages to create classes with the same name ................................................................. 161
Section 27.2: Using Package Protected Scope ...................................................................................................... 161
Chapter 28: Inheritance ......................................................................................................................................... 163
Section 28.1: Inheritance ........................................................................................................................................... 163
Section 28.2: Abstract Classes ................................................................................................................................. 164
Section 28.3: Using 'final' to restrict inheritance and overriding .......................................................................... 166
Section 28.4: The Liskov Substitution Principle ...................................................................................................... 167
Section 28.5: Abstract class and Interface usage: "Is-a" relation vs "Has-a" capability ................................... 168
Section 28.6: Static Inheritance ................................................................................................................................ 171
Section 28.7: Programming to an interface ........................................................................................................... 172