Java
Java
Notes for Professionals
®
Notes for Professionals
900+ pages
of professional hints and tricks
Disclaimer
GoalKicker.com This is an unocial free book created for educational purposes and is
not aliated with ocial Java® group(s) or company(s).
Free Programming Books All trademarks and registered trademarks are
the property of their respective owners
,Contents
About ................................................................................................................................................................................... 1
Chapter 1: Getting started with Java Language .......................................................................................... 2
Section 1.1: Creating Your First Java Program ........................................................................................................... 2
Chapter 2: Type Conversion .................................................................................................................................... 8
Section 2.1: Numeric primitive casting ......................................................................................................................... 8
Section 2.2: Basic Numeric Promotion ........................................................................................................................ 8
Section 2.3: Non-numeric primitive casting ................................................................................................................ 8
Section 2.4: Object casting ........................................................................................................................................... 9
Section 2.5: Testing if an object can be cast using instanceof ................................................................................. 9
Chapter 3: Getters and Setters ........................................................................................................................... 10
Section 3.1: Using a setter or getter to implement a constraint ............................................................................. 10
Section 3.2: Why Use Getters and Setters? .............................................................................................................. 10
Section 3.3: Adding Getters and Setters ................................................................................................................... 11
Chapter 4: Reference Data Types .................................................................................................................... 13
Section 4.1: Dereferencing .......................................................................................................................................... 13
Section 4.2: Instantiating a reference type ............................................................................................................... 13
Chapter 5: Java Compiler - 'javac' .................................................................................................................... 14
Section 5.1: The 'javac' command - getting started ................................................................................................ 14
Section 5.2: Compiling for a dierent version of Java ............................................................................................ 16
Chapter 6: Documenting Java Code ................................................................................................................. 18
Section 6.1: Building Javadocs From the Command Line ....................................................................................... 18
Section 6.2: Class Documentation ............................................................................................................................. 18
Section 6.3: Method Documentation ......................................................................................................................... 19
Section 6.4: Package Documentation ....................................................................................................................... 20
Section 6.5: Links ......................................................................................................................................................... 20
Section 6.6: Code snippets inside documentation ................................................................................................... 21
Section 6.7: Field Documentation .............................................................................................................................. 22
Section 6.8: Inline Code Documentation ................................................................................................................... 22
Chapter 7: Command line Argument Processing ....................................................................................... 24
Section 7.1: Argument processing using GWT ToolBase ......................................................................................... 24
Section 7.2: Processing arguments by hand ............................................................................................................ 24
Chapter 8: The Java Command - 'java' and 'javaw' ................................................................................. 27
Section 8.1: Entry point classes .................................................................................................................................. 27
Section 8.2: Troubleshooting the 'java' command .................................................................................................. 27
Section 8.3: Running a Java application with library dependencies ..................................................................... 29
Section 8.4: Java Options ........................................................................................................................................... 30
Section 8.5: Spaces and other special characters in arguments ........................................................................... 31
Section 8.6: Running an executable JAR file ............................................................................................................ 33
Section 8.7: Running a Java applications via a "main" class ................................................................................. 33
Chapter 9: Literals ...................................................................................................................................................... 35
Section 9.1: Using underscore to improve readability ............................................................................................. 35
Section 9.2: Hexadecimal, Octal and Binary literals ................................................................................................ 35
Section 9.3: Boolean literals ....................................................................................................................................... 36
Section 9.4: String literals ........................................................................................................................................... 36
Section 9.5: The Null literal ......................................................................................................................................... 37
Section 9.6: Escape sequences in literals .................................................................................................................. 37
, Section 9.7: Character literals .................................................................................................................................... 38
Section 9.8: Decimal Integer literals .......................................................................................................................... 38
Section 9.9: Floating-point literals ............................................................................................................................. 39
Chapter 10: Primitive Data Types ....................................................................................................................... 42
Section 10.1: The char primitive .................................................................................................................................. 42
Section 10.2: Primitive Types Cheatsheet ................................................................................................................. 42
Section 10.3: The float primitive ................................................................................................................................. 43
Section 10.4: The int primitive ..................................................................................................................................... 44
Section 10.5: Converting Primitives ............................................................................................................................ 45
Section 10.6: Memory consumption of primitives vs. boxed primitives .................................................................. 45
Section 10.7: The double primitive ............................................................................................................................. 46
Section 10.8: The long primitive ................................................................................................................................. 47
Section 10.9: The boolean primitive ........................................................................................................................... 48
Section 10.10: The byte primitive ................................................................................................................................ 48
Section 10.11: Negative value representation ............................................................................................................ 49
Section 10.12: The short primitive ............................................................................................................................... 50
Chapter 11: Strings ...................................................................................................................................................... 51
Section 11.1: Comparing Strings .................................................................................................................................. 51
Section 11.2: Changing the case of characters within a String ............................................................................... 53
Section 11.3: Finding a String Within Another String ................................................................................................. 55
Section 11.4: String pool and heap storage ............................................................................................................... 56
Section 11.5: Splitting Strings ....................................................................................................................................... 57
Section 11.6: Joining Strings with a delimiter ............................................................................................................ 59
Section 11.7: String concatenation and StringBuilders ............................................................................................. 60
Section 11.8: Substrings ............................................................................................................................................... 61
Section 11.9: Platform independent new line separator ........................................................................................... 62
Section 11.10: Reversing Strings .................................................................................................................................. 62
Section 11.11: Adding toString() method for custom objects .................................................................................... 63
Section 11.12: Remove Whitespace from the Beginning and End of a String ........................................................ 64
Section 11.13: Case insensitive switch ......................................................................................................................... 64
Section 11.14: Replacing parts of Strings ................................................................................................................... 65
Section 11.15: Getting the length of a String .............................................................................................................. 66
Section 11.16: Getting the nth character in a String .................................................................................................. 66
Section 11.17: Counting occurrences of a substring or character in a string ......................................................... 66
Chapter 12: StringBuer ......................................................................................................................................... 68
Section 12.1: String Buer class .................................................................................................................................. 68
Chapter 13: StringBuilder ........................................................................................................................................ 69
Section 13.1: Comparing StringBuer, StringBuilder, Formatter and StringJoiner ............................................... 69
Section 13.2: Repeat a String n times ........................................................................................................................ 70
Chapter 14: String Tokenizer ................................................................................................................................ 71
Section 14.1: StringTokenizer Split by space ............................................................................................................. 71
Section 14.2: StringTokenizer Split by comma ',' ...................................................................................................... 71
Chapter 15: Splitting a string into fixed length parts ............................................................................... 72
Section 15.1: Break a string up into substrings all of a known length .................................................................... 72
Section 15.2: Break a string up into substrings all of variable length .................................................................... 72
Chapter 16: Date Class ............................................................................................................................................. 73
Section 16.1: Convert java.util.Date to java.sql.Date ................................................................................................. 73
Section 16.2: A basic date output ............................................................................................................................... 73
Section 16.3: Java 8 LocalDate and LocalDateTime objects .................................................................................. 74
, Section 16.4: Creating a Specific Date ....................................................................................................................... 75
Section 16.5: Converting Date to a certain String format ....................................................................................... 75
Section 16.6: LocalTime ............................................................................................................................................... 76
Section 16.7: Convert formatted string representation of date to Date object .................................................... 76
Section 16.8: Creating Date objects ........................................................................................................................... 77
Section 16.9: Comparing Date objects ...................................................................................................................... 77
Section 16.10: Converting String into Date ................................................................................................................ 80
Section 16.11: Time Zones and java.util.Date ............................................................................................................. 80
Chapter 17: Dates and Time (java.time.*) ....................................................................................................... 82
Section 17.1: Calculate Dierence between 2 LocalDates ....................................................................................... 82
Section 17.2: Date and time ........................................................................................................................................ 82
Section 17.3: Operations on dates and times ........................................................................................................... 82
Section 17.4: Instant ..................................................................................................................................................... 82
Section 17.5: Usage of various classes of Date Time API ........................................................................................ 83
Section 17.6: Date Time Formatting ........................................................................................................................... 85
Section 17.7: Simple Date Manipulations ................................................................................................................... 85
Chapter 18: LocalTime .............................................................................................................................................. 87
Section 18.1: Amount of time between two LocalTime ............................................................................................ 87
Section 18.2: Intro ........................................................................................................................................................ 88
Section 18.3: Time Modification .................................................................................................................................. 88
Section 18.4: Time Zones and their time dierence ................................................................................................. 88
Chapter 19: BigDecimal ............................................................................................................................................ 90
Section 19.1: Comparing BigDecimals ........................................................................................................................ 90
Section 19.2: Using BigDecimal instead of float ....................................................................................................... 90
Section 19.3: BigDecimal.valueOf() ............................................................................................................................ 91
Section 19.4: Mathematical operations with BigDecimal ......................................................................................... 91
Section 19.5: Initialization of BigDecimals with value zero, one or ten ................................................................... 94
Section 19.6: BigDecimal objects are immutable ..................................................................................................... 94
Chapter 20: BigInteger ............................................................................................................................................ 96
Section 20.1: Initialization ............................................................................................................................................ 96
Section 20.2: BigInteger Mathematical Operations Examples ............................................................................... 97
Section 20.3: Comparing BigIntegers ........................................................................................................................ 99
Section 20.4: Binary Logic Operations on BigInteger ........................................................................................... 100
Section 20.5: Generating random BigIntegers ....................................................................................................... 101
Chapter 21: NumberFormat ................................................................................................................................. 103
Section 21.1: NumberFormat .................................................................................................................................... 103
Chapter 22: Bit Manipulation .............................................................................................................................. 104
Section 22.1: Checking, setting, clearing, and toggling individual bits. Using long as bit mask ........................ 104
Section 22.2: java.util.BitSet class ............................................................................................................................ 104
Section 22.3: Checking if a number is a power of 2 .............................................................................................. 105
Section 22.4: Signed vs unsigned shift .................................................................................................................... 107
Section 22.5: Expressing the power of 2 ................................................................................................................. 107
Section 22.6: Packing / unpacking values as bit fragments ................................................................................ 108
Chapter 23: Arrays ................................................................................................................................................... 109
Section 23.1: Creating and Initializing Arrays ......................................................................................................... 109
Section 23.2: Creating a List from an Array ........................................................................................................... 115
Section 23.3: Creating an Array from a Collection ................................................................................................ 117
Section 23.4: Multidimensional and Jagged Arrays .............................................................................................. 117
Section 23.5: ArrayIndexOutOfBoundsException .................................................................................................. 119
Java
Notes for Professionals
®
Notes for Professionals
900+ pages
of professional hints and tricks
Disclaimer
GoalKicker.com This is an unocial free book created for educational purposes and is
not aliated with ocial Java® group(s) or company(s).
Free Programming Books All trademarks and registered trademarks are
the property of their respective owners
,Contents
About ................................................................................................................................................................................... 1
Chapter 1: Getting started with Java Language .......................................................................................... 2
Section 1.1: Creating Your First Java Program ........................................................................................................... 2
Chapter 2: Type Conversion .................................................................................................................................... 8
Section 2.1: Numeric primitive casting ......................................................................................................................... 8
Section 2.2: Basic Numeric Promotion ........................................................................................................................ 8
Section 2.3: Non-numeric primitive casting ................................................................................................................ 8
Section 2.4: Object casting ........................................................................................................................................... 9
Section 2.5: Testing if an object can be cast using instanceof ................................................................................. 9
Chapter 3: Getters and Setters ........................................................................................................................... 10
Section 3.1: Using a setter or getter to implement a constraint ............................................................................. 10
Section 3.2: Why Use Getters and Setters? .............................................................................................................. 10
Section 3.3: Adding Getters and Setters ................................................................................................................... 11
Chapter 4: Reference Data Types .................................................................................................................... 13
Section 4.1: Dereferencing .......................................................................................................................................... 13
Section 4.2: Instantiating a reference type ............................................................................................................... 13
Chapter 5: Java Compiler - 'javac' .................................................................................................................... 14
Section 5.1: The 'javac' command - getting started ................................................................................................ 14
Section 5.2: Compiling for a dierent version of Java ............................................................................................ 16
Chapter 6: Documenting Java Code ................................................................................................................. 18
Section 6.1: Building Javadocs From the Command Line ....................................................................................... 18
Section 6.2: Class Documentation ............................................................................................................................. 18
Section 6.3: Method Documentation ......................................................................................................................... 19
Section 6.4: Package Documentation ....................................................................................................................... 20
Section 6.5: Links ......................................................................................................................................................... 20
Section 6.6: Code snippets inside documentation ................................................................................................... 21
Section 6.7: Field Documentation .............................................................................................................................. 22
Section 6.8: Inline Code Documentation ................................................................................................................... 22
Chapter 7: Command line Argument Processing ....................................................................................... 24
Section 7.1: Argument processing using GWT ToolBase ......................................................................................... 24
Section 7.2: Processing arguments by hand ............................................................................................................ 24
Chapter 8: The Java Command - 'java' and 'javaw' ................................................................................. 27
Section 8.1: Entry point classes .................................................................................................................................. 27
Section 8.2: Troubleshooting the 'java' command .................................................................................................. 27
Section 8.3: Running a Java application with library dependencies ..................................................................... 29
Section 8.4: Java Options ........................................................................................................................................... 30
Section 8.5: Spaces and other special characters in arguments ........................................................................... 31
Section 8.6: Running an executable JAR file ............................................................................................................ 33
Section 8.7: Running a Java applications via a "main" class ................................................................................. 33
Chapter 9: Literals ...................................................................................................................................................... 35
Section 9.1: Using underscore to improve readability ............................................................................................. 35
Section 9.2: Hexadecimal, Octal and Binary literals ................................................................................................ 35
Section 9.3: Boolean literals ....................................................................................................................................... 36
Section 9.4: String literals ........................................................................................................................................... 36
Section 9.5: The Null literal ......................................................................................................................................... 37
Section 9.6: Escape sequences in literals .................................................................................................................. 37
, Section 9.7: Character literals .................................................................................................................................... 38
Section 9.8: Decimal Integer literals .......................................................................................................................... 38
Section 9.9: Floating-point literals ............................................................................................................................. 39
Chapter 10: Primitive Data Types ....................................................................................................................... 42
Section 10.1: The char primitive .................................................................................................................................. 42
Section 10.2: Primitive Types Cheatsheet ................................................................................................................. 42
Section 10.3: The float primitive ................................................................................................................................. 43
Section 10.4: The int primitive ..................................................................................................................................... 44
Section 10.5: Converting Primitives ............................................................................................................................ 45
Section 10.6: Memory consumption of primitives vs. boxed primitives .................................................................. 45
Section 10.7: The double primitive ............................................................................................................................. 46
Section 10.8: The long primitive ................................................................................................................................. 47
Section 10.9: The boolean primitive ........................................................................................................................... 48
Section 10.10: The byte primitive ................................................................................................................................ 48
Section 10.11: Negative value representation ............................................................................................................ 49
Section 10.12: The short primitive ............................................................................................................................... 50
Chapter 11: Strings ...................................................................................................................................................... 51
Section 11.1: Comparing Strings .................................................................................................................................. 51
Section 11.2: Changing the case of characters within a String ............................................................................... 53
Section 11.3: Finding a String Within Another String ................................................................................................. 55
Section 11.4: String pool and heap storage ............................................................................................................... 56
Section 11.5: Splitting Strings ....................................................................................................................................... 57
Section 11.6: Joining Strings with a delimiter ............................................................................................................ 59
Section 11.7: String concatenation and StringBuilders ............................................................................................. 60
Section 11.8: Substrings ............................................................................................................................................... 61
Section 11.9: Platform independent new line separator ........................................................................................... 62
Section 11.10: Reversing Strings .................................................................................................................................. 62
Section 11.11: Adding toString() method for custom objects .................................................................................... 63
Section 11.12: Remove Whitespace from the Beginning and End of a String ........................................................ 64
Section 11.13: Case insensitive switch ......................................................................................................................... 64
Section 11.14: Replacing parts of Strings ................................................................................................................... 65
Section 11.15: Getting the length of a String .............................................................................................................. 66
Section 11.16: Getting the nth character in a String .................................................................................................. 66
Section 11.17: Counting occurrences of a substring or character in a string ......................................................... 66
Chapter 12: StringBuer ......................................................................................................................................... 68
Section 12.1: String Buer class .................................................................................................................................. 68
Chapter 13: StringBuilder ........................................................................................................................................ 69
Section 13.1: Comparing StringBuer, StringBuilder, Formatter and StringJoiner ............................................... 69
Section 13.2: Repeat a String n times ........................................................................................................................ 70
Chapter 14: String Tokenizer ................................................................................................................................ 71
Section 14.1: StringTokenizer Split by space ............................................................................................................. 71
Section 14.2: StringTokenizer Split by comma ',' ...................................................................................................... 71
Chapter 15: Splitting a string into fixed length parts ............................................................................... 72
Section 15.1: Break a string up into substrings all of a known length .................................................................... 72
Section 15.2: Break a string up into substrings all of variable length .................................................................... 72
Chapter 16: Date Class ............................................................................................................................................. 73
Section 16.1: Convert java.util.Date to java.sql.Date ................................................................................................. 73
Section 16.2: A basic date output ............................................................................................................................... 73
Section 16.3: Java 8 LocalDate and LocalDateTime objects .................................................................................. 74
, Section 16.4: Creating a Specific Date ....................................................................................................................... 75
Section 16.5: Converting Date to a certain String format ....................................................................................... 75
Section 16.6: LocalTime ............................................................................................................................................... 76
Section 16.7: Convert formatted string representation of date to Date object .................................................... 76
Section 16.8: Creating Date objects ........................................................................................................................... 77
Section 16.9: Comparing Date objects ...................................................................................................................... 77
Section 16.10: Converting String into Date ................................................................................................................ 80
Section 16.11: Time Zones and java.util.Date ............................................................................................................. 80
Chapter 17: Dates and Time (java.time.*) ....................................................................................................... 82
Section 17.1: Calculate Dierence between 2 LocalDates ....................................................................................... 82
Section 17.2: Date and time ........................................................................................................................................ 82
Section 17.3: Operations on dates and times ........................................................................................................... 82
Section 17.4: Instant ..................................................................................................................................................... 82
Section 17.5: Usage of various classes of Date Time API ........................................................................................ 83
Section 17.6: Date Time Formatting ........................................................................................................................... 85
Section 17.7: Simple Date Manipulations ................................................................................................................... 85
Chapter 18: LocalTime .............................................................................................................................................. 87
Section 18.1: Amount of time between two LocalTime ............................................................................................ 87
Section 18.2: Intro ........................................................................................................................................................ 88
Section 18.3: Time Modification .................................................................................................................................. 88
Section 18.4: Time Zones and their time dierence ................................................................................................. 88
Chapter 19: BigDecimal ............................................................................................................................................ 90
Section 19.1: Comparing BigDecimals ........................................................................................................................ 90
Section 19.2: Using BigDecimal instead of float ....................................................................................................... 90
Section 19.3: BigDecimal.valueOf() ............................................................................................................................ 91
Section 19.4: Mathematical operations with BigDecimal ......................................................................................... 91
Section 19.5: Initialization of BigDecimals with value zero, one or ten ................................................................... 94
Section 19.6: BigDecimal objects are immutable ..................................................................................................... 94
Chapter 20: BigInteger ............................................................................................................................................ 96
Section 20.1: Initialization ............................................................................................................................................ 96
Section 20.2: BigInteger Mathematical Operations Examples ............................................................................... 97
Section 20.3: Comparing BigIntegers ........................................................................................................................ 99
Section 20.4: Binary Logic Operations on BigInteger ........................................................................................... 100
Section 20.5: Generating random BigIntegers ....................................................................................................... 101
Chapter 21: NumberFormat ................................................................................................................................. 103
Section 21.1: NumberFormat .................................................................................................................................... 103
Chapter 22: Bit Manipulation .............................................................................................................................. 104
Section 22.1: Checking, setting, clearing, and toggling individual bits. Using long as bit mask ........................ 104
Section 22.2: java.util.BitSet class ............................................................................................................................ 104
Section 22.3: Checking if a number is a power of 2 .............................................................................................. 105
Section 22.4: Signed vs unsigned shift .................................................................................................................... 107
Section 22.5: Expressing the power of 2 ................................................................................................................. 107
Section 22.6: Packing / unpacking values as bit fragments ................................................................................ 108
Chapter 23: Arrays ................................................................................................................................................... 109
Section 23.1: Creating and Initializing Arrays ......................................................................................................... 109
Section 23.2: Creating a List from an Array ........................................................................................................... 115
Section 23.3: Creating an Array from a Collection ................................................................................................ 117
Section 23.4: Multidimensional and Jagged Arrays .............................................................................................. 117
Section 23.5: ArrayIndexOutOfBoundsException .................................................................................................. 119