Skip to content

Commit 3903e03

Browse files
committed
improve wordings
1 parent 42c8719 commit 3903e03

30 files changed

+193
-431
lines changed

chapter01-basic_types.jsh

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var result = value + value;
4242

4343
// ### primitive conversions
4444
// You have automatic conversions if there is no loose of precision
45-
// unless it's to convert to double or float which you are always allowed.
45+
// and converting to double or float is always allowed
4646
int intValue = 13;
4747
long longValue = intValue;
4848

chapter04-numbers.jsh

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ System.out.println(0.0 / 0.0);
8989
System.out.println(Double.NaN);
9090

9191
// ### NaN
92-
// Not a Number is very weird, because by definition, it's the number which is not equals to itself
92+
// Not a Number is very weird, because by definition, it's the number which is not equal to itself
9393

9494
// Don't use == to test NaN, it will not work
9595
System.out.println(Double.NaN == Double.NaN);
@@ -100,7 +100,7 @@ boolean isNotANumber(double x) {
100100
}
101101
System.out.println(isNotANumber(Double.NaN));
102102

103-
// The method isNotANumber already exist in Double, `Double.isNaN()`
103+
// An equivalent static method to isNotANumber already exist in Double, `Double.isNaN()`
104104
System.out.println(Double.isNaN(Double.NaN));
105105

106106

chapter05-control_flow.jsh

+11
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ System.out.println(x);
2424
// ## If
2525

2626
// ### Test with `if`
27+
// the construct `if` execute the block of code that follow if the condition in between
28+
// the parenthesis is true
2729
void oldEnough(int age) {
2830
if (age >= 21) {
2931
System.out.println("you are old enough to drink a beer");
@@ -32,6 +34,8 @@ void oldEnough(int age) {
3234
oldEnough(22);
3335

3436
// ### Test with `if ... else`
37+
// you can append the construct `else` after an `if` to execute a block of code
38+
// if the condition is not true
3539
void oldEnough(int age) {
3640
if (age >= 21) {
3741
System.out.println("you are old enough to drink a beer");
@@ -141,6 +145,7 @@ printKind(new Car(4));
141145
// ## Loops
142146

143147
// ### `while` loop
148+
// a `while` loop execute the block of code while the condition in between parenthesis is true
144149
void printFirstIntegers(int n) {
145150
var i = 0;
146151
while(i < n) {
@@ -151,14 +156,20 @@ void printFirstIntegers(int n) {
151156
printFirstIntegers(5);
152157

153158
// ### `for` loop
159+
// a for loop is a convenient way to write a `while` loop in case you do a while on a variable,
160+
// so instead of using the `while` loop above, one can write this for loop
154161
void printFirstIntegers(int n) {
155162
for(var i = 0; i < n; i++) {
156163
System.out.println(i);
157164
}
158165
}
159166
printFirstIntegers(5);
160167

168+
169+
161170
// ### `for` loop on array or list
171+
// Java as a special loop for iterating over the content of an array or a list,
172+
// it using the keyword `for`, but the declared variable contains each element one by one
162173
var list = List.of("iron man", "captain america", "black panther");
163174
for(var value: list) {
164175
System.out.println(value);

chapter07-interface.jsh

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// # Interface
66
// Java is a typed language, even if you don't explicitly write a type
77
// the compiler you compute the type of every variables
8-
// Once you start to want to mix several records, you need to declare
8+
// Once you start to want to mix several records, you may need to declare
99
// common type between records, such type are known as interface
1010

1111
// ## The problem
@@ -31,7 +31,7 @@ for(var figure: figures) {
3131

3232
// The problem is that compiler try to find the type of the element of the list
3333
// and find that they are java.lang.Object, and Object has no method area()
34-
// so it doens't compile
34+
// so the code does not compile
3535

3636

3737
// ### Interface and abstract method
@@ -68,7 +68,7 @@ for(var figure: figures) {
6868

6969
// An interface is a common type that you need to declare when you want to
7070
// call the same method on different records
71-
// At runtime, when you call a method of the interface, the interpreter calls
71+
// At runtime, when you call a method of the interface, the virtual machine calls
7272
// the correct implementation (this is called polymorphism)
7373

7474

@@ -100,14 +100,14 @@ System.out.println(new Rectangle(3, 4).isBig());
100100

101101
// Because a default method is declared on the interface, all records that
102102
// implement that interface will have that method. Default methods are named like this
103-
// because if the record doesn't define the method itself, the method will be provided
104-
// by default.
103+
// because if a record that implements the interface doesn't define the method,
104+
// the method will be provided by default.
105105

106106

107107
// ## Functional interface
108108
// An interface with only one abstract method is equivalent to a function type.
109109
// We name this kind of interfaces, _functional_ interfaces.
110-
// They can be implemented by supplementary constructs.
110+
// They can be implemented by two special constructs.
111111

112112
// ### Lambda
113113
// The parameter are declared in between the parenthesis and the body of the method

chapter09-list_and_map.jsh

+5-4
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ countries.forEach(country -> System.out.println(country));
7474
var unmodifiableList = List.copyOf(modifiableCountries);
7575
System.out.println(unmodifiableList);
7676

77-
// To create a modifiable list from an unmodifiable one using ArrayList(List)
77+
// To create a modifiable list from an unmodifiable one using `new ArrayList(List)`
7878
// In that case you don't have to specify the type of the elements
7979
// the compiler already knows the type of list hence the <> (diamond)
8080
var modifiableList = new ArrayList<>(unmodifiableList);
8181
System.out.println(modifiableList);
8282

8383
// ### Useful patterns
84-
// To remove some elements depending on a predicate
84+
// To remove some elements depending on a predicate (if something is true)
8585
var elements = new ArrayList<>(List.of("table", "chair", "stool"));
8686
elements.removeIf(element -> element.charAt(0) == 'c');
8787
System.out.println(elements);
@@ -171,12 +171,13 @@ persons.forEach(person -> group.computeIfAbsent(person.name(), name -> new Array
171171
.add(person));
172172
System.out.println(group);
173173

174-
// to count the number of occurrence, use `merge()`
174+
// to count the number of occurrence, use `merge()` that takes a key, a value and the function
175+
// to call if there is already an existing value to combine them
175176
var letters = List.of("a", "b", "e", "b");
176177
var occurenceMap = new HashMap<String, Integer>();
177178
letters.forEach(letter -> occurenceMap.merge(letter, 1, Integer::sum));
178179
System.out.println(occurenceMap);
179180

180181

181182
// ### More on transformations
182-
// To do more transformations of lists and maps, the Stream API is richer
183+
// To do more transformations of lists and maps, use the Stream API.

chapter10-string_formatting.jsh

+3-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ String concatenate(List<?> list) {
4848
var string = "";
4949
var separator = "";
5050
for(var item: list) {
51-
string = string + separator + item;
51+
string = string + separator + item; // creates two many strings, ahhhh
5252
separator = ", ";
5353
}
5454
return string;
@@ -71,7 +71,8 @@ String concatenate(List<?> list) {
7171
System.out.println(concatenate(strings));
7272
System.out.println(concatenate(friends));
7373

74-
// > Don't use '+' inside a `append()`, you already have a StringBuilder, so use append() instead
74+
// > Don't use '+' inside a call to `append()`, you already have a StringBuilder,
75+
// so use append() instead
7576

7677

7778
// ### Concatenation with String.join()

chapter17-enum.jsh

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ enum FileListMode {
136136
DIRECTORY(path -> NORMAL.test(path) && Files.isDirectory(path))
137137
;
138138
private final FilePredicate predicate;
139-
private FileListMode(FilePredicate predicate) {
139+
FileListMode(FilePredicate predicate) {
140140
this.predicate = predicate;
141141
}
142142
public boolean test(Path path) throws IOException {

chapter18-nested_classes.jsh

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ Object box = new Object() {
173173
};
174174
System.out.println(box.sum); // doesn't compile
175175

176-
// If we are using var, it works !
176+
// using `var`, it works !
177177
var box = new Object() {
178178
int sum;
179179
};

chapter23-limitation_of_generics.jsh

+31-9
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,39 @@
77

88
// ## No reification
99

10-
In Java, generics are purely a compiler construct, the type arguments of a generics
11-
are not available at runtime.
10+
// In Java, generics are purely a compiler construct, the type arguments of a generics
11+
// are not available at runtime.
12+
// - for a type variable (`T`, `E`, etc), the actual value at the execution, the type argument is not present at runtime.
13+
// - for a parameterized type (`List&gt;String&lt;`) the type argument (`String` here) is not available too
1214

13-
For a type variable (`T`, `E`, etc), the actual value at the execution, the type argument
14-
is not present at runtime. For a parameterized type (`List&gt;String&lt;`
15+
// ### No type argument
1516

16-
- instanceof
17-
- cast
18-
-
19-
- array creation
17+
// Which means that all operations that requires the type argument at runtime doesn't work.
18+
// So the compiler doesn't allow you to write
19+
// - `new T(...)`
20+
// - `new T[5]`
21+
// - `instanceof T` and `instanceof Foo<String>`
22+
// - `catch(T ..)` and `catch(Foo<String>)`, moreover
23+
24+
// ### Unchecked Warnings
25+
26+
// You can still write a cast like `(T)` or `(Foo<String>)`,
27+
// but the complier will warn you that this is not a safe cast.
28+
29+
// An example of cast that works
30+
List<Object> objectList = List.of("foo", "bar");
31+
List<String> stringList = (List<String>) (List<?>) objectList; // warning, this is dangerous
32+
// but it works in practice
33+
34+
// An example of cast that doesn't work
35+
List<String> stringList = new ArrayList<>();
36+
stringList.add("foo");
37+
List<Object> objectList = (List<Object>) (List<?>) stringList; // dangerous, don't work !
38+
objectList.add(4321);
39+
System.out.println(stringList.get(1)); // fail at runtime
40+
41+
// Hopefully, those casts are rare in practice because casts in general are rare
42+
// mostly because if you have a cast it means that you have lost the type at some point.
2043

2144

2245
// ## Array of parameterized type
@@ -46,7 +69,6 @@ objectArray[0] = 3; // ArrayStoreException
4669
// the array. Anyway, it's unsafe.
4770

4871

49-
5072
// ### varargs
5173

5274

chapter30-data_structure.jsh

+2-68
Original file line numberDiff line numberDiff line change
@@ -5,72 +5,6 @@
55
// In Java, there are already plenty of data structures already available
66
// there are grouped under the name the collection API.
77

8-
// to create a simple list
9-
var list = List.of(1, 2, 3);
10-
11-
// a list is an indexed data structure that stores object in the order of insertions
12-
// get() access to an element given an index
13-
var firstElement = list.get(0);
14-
var lastElement = list.get(list.size() - 1);
15-
16-
// contains return true if a value is contained in the list
17-
System.out.println(list.contains(4));
18-
19-
// indexOf returns the first index of the element in the list
20-
System.out.println(list.indexOf(2));
21-
22-
// to loop over the elements of a list, we have a special syntax using the keyword 'for'
23-
var countries = List.of('UK', 'US', 'France');
24-
for(var country: countries) {
25-
System.out.println(country);
26-
}
27-
28-
// you can also loop over the elements using a method forEach
29-
// if you don't understand this one, don't panic, we will see it later
30-
countries.forEach(country -> System.out.println(country));
31-
32-
33-
// a list also defines the method equals() and toString(), so
34-
// you can print a list or test if two list are equals
35-
System.out.println(countries);
36-
System.out.println(list.equals(countries));
37-
38-
39-
// in Java, depending on how you create a collection it can be changed
40-
// after creation or not. Implementation that allow mutation after creation
41-
// are called modifiable
42-
43-
// by example, the list above (created with the static method of()) is not modifiable
44-
//countries.set(0, 'Poland') // throws an UnsupportedOperationException
45-
46-
// To create a modifiable list, we use an ArrayList, created using the operator 'new'
47-
// here because there is no element in the list, the compiler has no way to know
48-
// the type of the elements so we have to provide it in between angle brackets ('<' and '>')
49-
var modifiableCountries = new ArrayList<String>();
50-
51-
// To add elements in a list, we have the method add()
52-
modifiableCountries.add("UK");
53-
modifiableCountries.add("US");
54-
modifiableCountries.add("France");
55-
modifiableCountries.add("Poland");
56-
57-
// to remove an element, we have the method remove()
58-
modifiableCountries.remove("UK");
59-
60-
// an unmodifiable list or a modifiable list have the same set of methods,
61-
// so you can loop over the modiable list the same way
62-
for(var country: modifiableCountries) {
63-
System.out.println(country);
64-
}
65-
66-
// You can create a modifiable list from an unmodifiable one using new ArrayList
67-
// with the unmodifiable list as argument
68-
// In that case you don't have to specify the type of the elements
69-
// the compiler already knows the type of list hence the <> (diamond)
70-
var modifiableList = new ArrayList<>(list);
71-
System.out.println(modifiableList);
72-
73-
748
// Lists are not the only data structure in Java, you also have set, queue and map
759
// - a set is set where you can not store the same object twice
7610
// (object are the same is equals() return true)
@@ -82,8 +16,8 @@ System.out.println(modifiableList);
8216
var authors = Set.of("J.R.R. Tolkien", "Philip K. Dick", "George R.R. Martin");
8317
System.out.println(authors);
8418

85-
// elements inside a set are organized in a way that make contains fast
86-
System.out.println(authors.contains(""Philip K. Dick""));
19+
// elements inside a set are organized in a way that make `contains` fast
20+
System.out.println(authors.contains("Philip K. Dick"));
8721

8822
// there are 3 modifiable sets
8923
// - HashSet

guide/chapter01-basic_types.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ var result = value + value;
5252

5353
### primitive conversions
5454
You have automatic conversions if there is no loose of precision
55-
unless it's to convert to double or float which you are always allowed.
55+
and converting to double or float is always allowed
5656
```java
5757
int intValue = 13;
5858
long longValue = intValue;

guide/chapter04-numbers.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ System.out.println(Double.NaN);
113113
```
114114

115115
### NaN
116-
Not a Number is very weird, because by definition, it's the number which is not equals to itself
116+
Not a Number is very weird, because by definition, it's the number which is not equal to itself
117117

118118
Don't use == to test NaN, it will not work
119119
```java
@@ -128,7 +128,7 @@ boolean isNotANumber(double x) {
128128
System.out.println(isNotANumber(Double.NaN));
129129
```
130130

131-
The method isNotANumber already exist in Double, `Double.isNaN()`
131+
An equivalent static method to isNotANumber already exist in Double, `Double.isNaN()`
132132
```java
133133
System.out.println(Double.isNaN(Double.NaN));
134134
```

0 commit comments

Comments
 (0)