Java Notes for Professionals

Programming · notes

Voir tous les documents en programmation

Java

Notes for Professionals

Java

Notes for Professionals

900+ pages

of professional hints and tricks

GoalKicker.com

Free Programming Books

Disclaimer

This is an uno$cial free book created for educational purposes and is

not a$liated with o$cial Java group(s) or company(s).

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

Section 2.2: Basic Numeric Promotion

Section 2.3: Non-numeric primitive casting

Section 2.4: Object casting

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

Section 3.2: Why Use Getters and Setters?

Section 3.3: Adding Getters and Setters

Chapter 4: Reference Data Types

..............................................................................................................

...................................................................................................................

....................................................................................................................

.............................................................................

10

Section 4.1: Dereferencing

Section 4.2: Instantiating a reference type

..........................................................................................................................................

...............................................................................................................

13

Chapter 5: Java Compiler - 'javac'

....................................................................................................................

14

Section 5.1: The 'javac' command - getting started

Section 5.2: Compiling for a di#erent version of Java

................................................................................................

14

............................................................................................

16

Chapter 6: Documenting Java Code

.................................................................................................................

18

.......................................................................................

18

.............................................................................................................................

.........................................................................................................................

.......................................................................................................................

Section 6.1: Building Javadocs From the Command Line

Section 6.2: Class Documentation

Section 6.3: Method Documentation

Section 6.4: Package Documentation

Section 6.5: Links

Section 6.6: Code snippets inside documentation

Section 6.7: Field Documentation

Section 6.8: Inline Code Documentation

.........................................................................................................................................................

...................................................................................................

..............................................................................................................................

...................................................................................................................

Chapter 7: Command line Argument Processing

Section 7.1: Argument processing using GWT ToolBase

Section 7.2: Processing arguments by hand

.......................................................................................

24

.........................................................................................

............................................................................................................

Chapter 8: The Java Command - 'java' and 'javaw'

.................................................................................

27

..................................................................................................................................

..................................................................................................

.....................................................................

29

...........................................................................................................................................

Section 8.1: Entry point classes

Section 8.2: Troubleshooting the 'java' command

Section 8.3: Running a Java application with library dependencies

Section 8.4: Java Options

Section 8.5: Spaces and other special characters in arguments

Section 8.6: Running an executable JAR le

Section 8.7: Running a Java applications via a "main" class

...........................................................................

............................................................................................................

.................................................................................

Chapter 9: Literals

......................................................................................................................................................

35

Section 9.1: Using underscore to improve readability

Section 9.2: Hexadecimal, Octal and Binary literals

Section 9.3: Boolean literals

Section 9.4: String literals

Section 9.5: The Null literal

Section 9.6: Escape sequences in literals

.......................................................................................................................................

...........................................................................................................................................

.........................................................................................................................................

..................................................................................................................

.............................................................................................

35

................................................................................................

8

8

8

9

10

11

13

13

18

Publicité

19

20

20

21

22

22

24

24

27

27

30

31

33

33

35

36

36

37

37

....................................................................................................................................

..........................................................................................................................

.............................................................................................................................

.......................................................................................................................

..................................................................................................................................

.................................................................................................................

.................................................................................................................................

.....................................................................................................................................

............................................................................................................................

Section 9.7: Character literals

Section 9.8: Decimal Integer literals

Section 9.9: Floating-point literals

Chapter 10: Primitive Data Types

Section 10.1: The char primitive

Section 10.2: Primitive Types Cheatsheet

Section 10.3: The oat primitive

Section 10.4: The int primitive

Section 10.5: Converting Primitives

Section 10.6: Memory consumption of primitives vs. boxed primitives

Section 10.7: The double primitive

Section 10.8: The long primitive

Section 10.9: The boolean primitive

Section 10.10: The byte primitive

Section 10.11: Negative value representation

Section 10.12: The short primitive

.............................................................................................................................

.................................................................................................................................

...........................................................................................................................

................................................................................................................................

............................................................................................................

...............................................................................................................................

..................................................................

45

Chapter 11: Strings

......................................................................................................................................................

51

..................................................................................................................................

...............................................................................

.................................................................................................

...............................................................................................................

.......................................................................................................................................

............................................................................................................

.............................................................................................

...............................................................................................................................................

..................................................................................................................................

...........................................................................................

Section 11.1: Comparing Strings

Section 11.2: Changing the case of characters within a String

Section 11.3: Finding a String Within Another String

Section 11.4: String pool and heap storage

Section 11.5: Splitting Strings

Section 11.6: Joining Strings with a delimiter

Section 11.7: String concatenation and StringBuilders

Section 11.8: Substrings

Section 11.9: Platform independent new line separator

Section 11.10: Reversing Strings

Section 11.11: Adding toString() method for custom objects

Section 11.12: Remove Whitespace from the Beginning and End of a String

Section 11.13: Case insensitive switch

Section 11.14: Replacing parts of Strings

Section 11.15: Getting the length of a String

Section 11.16: Getting the nth character in a String

Section 11.17: Counting occurrences of a substring or character in a string

.........................................................................................................................

...................................................................................................................

..............................................................................................................

..................................................................................................

.........................................................

....................................................................................

........................................................

64

Chapter 12: StringBu#er

.........................................................................................................................................

68

Section 12.1: String Bu#er class

..................................................................................................................................

68

Chapter 13: StringBuilder

........................................................................................................................................

69

Section 13.1: Comparing StringBu#er, StringBuilder, Formatter and StringJoiner

Section 13.2: Repeat a String n times

........................................................................................................................

70

...............................................

69

Chapter 14: String Tokenizer

................................................................................................................................

71

Section 14.1: StringTokenizer Split by space

Section 14.2: StringTokenizer Split by comma ','

.............................................................................................................

71

......................................................................................................

71

Chapter 15: Splitting a string into xed length parts

...............................................................................

72

Section 15.1: Break a string up into substrings all of a known length

Section 15.2: Break a string up into substrings all of variable length

....................................................................

72

....................................................................

72

Chapter 16: Date Class

.............................................................................................................................................

73

Section 16.1: Convert java.util.Date to java.sql.Date

Section 16.2: A basic date output

Section 16.3: Java 8 LocalDate and LocalDateTime objects

.................................................................................................

...............................................................................................................................

..................................................................................

74

Publicité

38

38

39

42

42

42

43

44

45

46

47

48

48

49

50

51

53

55

56

57

59

60

61

62

62

63

64

65

66

66

66

73

73

.......................................................................................

.......................................................................................................................

...............................................................................................................................................

Section 16.4: Creating a Specic Date

Section 16.5: Converting Date to a certain String format

Section 16.6: LocalTime

Section 16.7: Convert formatted string representation of date to Date object

Section 16.8: Creating Date objects

Section 16.9: Comparing Date objects

Section 16.10: Converting String into Date

Section 16.11: Time Zones and java.util.Date

Chapter 17: Dates and Time (java.time.*)

...........................................................................................................................

......................................................................................................................

................................................................................................................

.............................................................................................................

....................................................

76

.......................................................................................................

.......................................................................................

82

Section 17.1: Calculate Di#erence between 2 LocalDates

Section 17.2: Date and time

Section 17.3: Operations on dates and times

Section 17.4: Instant

Section 17.5: Usage of various classes of Date Time API

Section 17.6: Date Time Formatting

Section 17.7: Simple Date Manipulations

........................................................................................................................................

...........................................................................................................

.....................................................................................................................................................

........................................................................................

...........................................................................................................................

85

...................................................................................................................

85

Chapter 18: LocalTime

..............................................................................................................................................

87

Section 18.1: Amount of time between two LocalTime

Section 18.2: Intro

Section 18.3: Time Modication

Section 18.4: Time Zones and their time di#erence

........................................................................................................................................................

..................................................................................................................................

.................................................................................................

............................................................................................

87

Chapter 19: BigDecimal

............................................................................................................................................

90

Section 19.1: Comparing BigDecimals

Section 19.2: Using BigDecimal instead of oat

Section 19.3: BigDecimal.valueOf()

Section 19.4: Mathematical operations with BigDecimal

Section 19.5: Initialization of BigDecimals with value zero, one or ten

Section 19.6: BigDecimal objects are immutable

........................................................................................................................

.......................................................................................................

............................................................................................................................

.........................................................................................

...................................................................

94

.....................................................................................................

............................................................................................................................................

............................................................................................................................................

Chapter 20: BigInteger

Section 20.1: Initialization

Section 20.2: BigInteger Mathematical Operations Examples

Section 20.3: Comparing BigIntegers

Section 20.4: Binary Logic Operations on BigInteger

Section 20.5: Generating random BigIntegers

........................................................................................................................

99

...............................................................................

97

...........................................................................................

.......................................................................................................

Chapter 21: NumberFormat

Section 21.1: NumberFormat

Chapter 22: Bit Manipulation

.................................................................................................................................

....................................................................................................................................

..............................................................................................................................

104

Section 22.1: Checking, setting, clearing, and toggling individual bits. Using long as bit mask

Section 22.2: java.util.BitSet class

Section 22.3: Checking if a number is a power of 2

Section 22.4: Signed vs unsigned shift

Section 22.5: Expressing the power of 2

Section 22.6: Packing / unpacking values as bit fragments

....................................................................................................................

.................................................................................................................

............................................................................................................................

..............................................................................................

................................................................................

........................

104

Chapter 23: Arrays

...................................................................................................................................................

Publicité

109

Section 23.1: Creating and Initializing Arrays

Section 23.2: Creating a List from an Array

Section 23.3: Creating an Array from a Collection

Section 23.4: Multidimensional and Jagged Arrays

Section 23.5: ArrayIndexOutOfBoundsException

.........................................................................................................

...........................................................................................................

................................................................................................

..............................................................................................

117

..................................................................................................

119

75

75

76

77

77

80

80

82

82

82

82

83

88

88

88

90

90

91

91

94

96

96

100

101

103

103

104

105

107

107

108

109

115

117

...............................................................................................................................

................................................................................................................................

..........................................................................................................................

...............................................................................................................................

....................................................................................................................................

Section 23.6: Array Covariance

Section 23.7: Arrays to Stream

Section 23.8: Iterating over arrays

Section 23.9: Arrays to a String

Section 23.10: Sorting arrays

Section 23.11: Getting the Length of an Array

Section 23.12: Finding an element in an array

Section 23.13: How do you change the size of an array?

Section 23.14: Converting arrays between primitives and boxed types

Section 23.15: Remove an element from an array

Section 23.16: Comparing arrays for equality

Section 23.17: Copying arrays

Section 23.18: Casting Arrays

........................................................................................................

.......................................................................................................

.....................................................................................

..............................................................

128

................................................................................................

........................................................................................................

..................................................................................................................................

...................................................................................................................................

Chapter 24: Collections

.........................................................................................................................................

133

........................................................................................

....................................................................................

.....................................................................................

Section 24.1: Removing items from a List within a loop

Section 24.2: Constructing collections from existing data

Section 24.3: Declaring an ArrayList and adding objects

Section 24.4: Iterating over Collections

Section 24.5: Immutable Empty Collections

Section 24.6: Sub Collections

Section 24.7: Unmodiable Collection

Section 24.8: Pitfall: concurrent modication exceptions

Section 24.9: Removing matching items from Lists using Iterator

Section 24.10: Join lists

Section 24.11: Creating your own Iterable structure for use with Iterator or for-each loop

Section 24.12: Collections and Primitive Values

..................................................................................................................

...........................................................................................................

...................................................................................................................................

.....................................................................................................................

.....................................................................................

......................................................................

.............................................................................................................................................

.....................................................................................................

144

..............................

142

Chapter 25: Lists

.......................................................................................................................................................

146

...........................................................................................................................

.....................................................................................

.....................................................................................

.....................................................................................

...........................................................................................

Section 25.1: Sorting a generic list

Section 25.2: Convert a list of integers to a list of strings

Section 25.3: Classes implementing List - Pros and Cons

Section 25.4: Finding common elements between 2 lists

Section 25.5: In-place replacement of a List element

Section 25.6: Making a list unmodiable

Section 25.7: Moving objects around in the list

Section 25.8: Creating, Adding and Removing element from an ArrayList

Section 25.9: Creating a List

Section 25.10: Positional Access Operations

Section 25.11: Iterating over elements in a list

Section 25.12: Removing elements from list B that are present in the list A

................................................................................................................

......................................................................................................

........................................................

.....................................................................................................................................

..........................................................................................................

........................................................................................................

.......................................................

155

Chapter 26: Sets

........................................................................................................................................................

Publicité

157

Section 26.1: Initialization

Section 26.2: Basics of Set

Section 26.3: Types and Usage of Sets

Section 26.4: Create a list from an existing Set

Section 26.5: Eliminating duplicates using Set

Section 26.6: Declaring a HashSet with values

..........................................................................................................................................

........................................................................................................................................

...................................................................................................................

......................................................................................................

159

.......................................................................................................

......................................................................................................

Chapter 27: List vs Set

Section 27.1: List vs Set

...........................................................................................................................................

161

.............................................................................................................................................

161

Chapter 28: Maps

......................................................................................................................................................

162

Section 28.1: Iterating Map Entries E$ciently

.........................................................................................................

162

120

121

121

123

124

126

126

127

129

130

130

131

133

135

137

137

139

139

140

141

141

142

146

147

147

150

150

151

151

152

152

153

155

157

157

158

159

160

............................................................................................................................

164

Section 28.2: Usage of HashMap

Section 28.3: Using Default Methods of Map from Java 8

Section 28.4: Iterating through the contents of a Map

Section 28.5: Merging, combine and composing Maps

Section 28.6: Add multiple items

Section 28.7: Creating and Initializing Maps

Section 28.8: Check if key exists

Section 28.9: Add an element

Section 28.10: Clear the map

Section 28.11: Use custom object as key

..............................................................................................................................

...........................................................................................................

..............................................................................................................................

..................................................................................................................................

....................................................................................................................................

.................................................................................................................

...................................................................................

165

.........................................................................................

........................................................................................

Chapter 29: LinkedHashMap

...............................................................................................................................

175

Section 29.1: Java LinkedHashMap class

...............................................................................................................

175

Chapter 30: WeakHashMap

.................................................................................................................................

176

Section 30.1: Concepts of WeakHashmap

..............................................................................................................

176

Chapter 31: SortedMap

..........................................................................................................................................

177

Section 31.1: Introduction to sorted Map

Chapter 32: TreeMap and TreeSet

Section 32.1: TreeMap of a simple Java type

Section 32.2: TreeSet of a simple Java Type

Section 32.3: TreeMap/TreeSet of a custom Java type

Section 32.4: TreeMap and TreeSet Thread Safety

.................................................................................................................

177

..................................................................................................................

........................................................................................................

.........................................................................................................

.......................................................................................

179

..............................................................................................

180

Chapter 33: Queues and Deques

......................................................................................................................

182

Section 33.1: The usage of the PriorityQueue

Section 33.2: Deque

Section 33.3: Stacks

Section 33.4: BlockingQueue

Section 33.5: LinkedList as a FIFO Queue

Section 33.6: Queue Interface

Chapter 34: Dequeue Interface

.........................................................................................................

182

...................................................................................................................................................

...................................................................................................................................................

....................................................................................................................................

...............................................................................................................

..................................................................................................................................

Publicité

......................................................................................................................