Skip to content

Commit b58e0e8

Browse files
committed
change examples to be more independent
1 parent 8e3fca6 commit b58e0e8

File tree

3 files changed

+55
-107
lines changed

3 files changed

+55
-107
lines changed

Diff for: chapter00-genesis.jsh

+20-22
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ var colorName = "black"; // it's a reference to String somewhere in memory
1717
// if you are using var, you are asking the compiler to find the type for you.
1818
String colorName = "black";
1919

20-
// ### A record is a user defined type
21-
// here Light is defined as containing two components: a color (typed as a String) and
22-
// an intensity (typed as a 64 bits floating number double).
23-
record Light(String color, double intensity) {}
24-
25-
// ## System.out.println()
20+
// ### System.out.println()
2621
// To print a value in Java we have a weird incantation `System.out.println()` that we will detail later.
2722
System.out.println(maxIntensity);
2823

@@ -33,48 +28,51 @@ System.out.println(colorName);
3328
// If we want to print a text followed by a value, we use the operator `+`.
3429
System.out.println("the value of colorName is " + colorName);
3530

36-
// ## Object creation with `new`
31+
32+
// ## A record is a user defined type
33+
// here Light is defined as containing two components: a color (typed as a String) and
34+
// an intensity (typed as a 64 bits floating number double).
35+
record Light(String color, double intensity) {}
36+
37+
// ### Object creation with `new`
3738
// To create an object in memory, we use the operator `new` followed by the value of each record components
3839
// the following instruction create a Light with "blue" as color and 1.0 as intensity.
3940
var blueLight = new Light("blue", 1.0);
41+
System.out.println(blueLight);
4042

41-
// ## Record methods
43+
// ### Record methods
4244
// To interact with an object in Java, we use methods, that are functions attached to an object.
4345
// To call a method, we use the operator `.` followed by the name of the method and its arguments.
4446
// A record automatically declares methods to access its components so Light declares two methods
4547
// color() and intensity().
4648

4749
// By example to get the intensity of the object blueLight
48-
var blueLightIntensity = blueLight.intensity();
49-
System.out.println(blueLightIntensity);
50+
System.out.println(blueLight.intensity());
5051

5152
// ### toString()
5253
// By default a record knows how to transform itself into a String
5354
// in Java, the method to transform an object to a String is named toString().
54-
System.out.println(blueLight.toString());
55-
5655
// In fact, println() calls toString() if the argument is an object
5756
// so when using println(), calling explicitly toString() is not necessary.
57+
System.out.println(blueLight.toString());
5858
System.out.println(blueLight);
5959

6060
// ### equals()
61-
// Let's create another Light
62-
var redLight = new Light("red", 1.0);
63-
6461
// In Java, you can ask if two objects are equals, using the method equals(Object).
65-
// the return value is a boolean (a primitive type that is either true or false).
62+
// The return value is a boolean (a primitive type that is either true or false).
63+
var redLight = new Light("red", 0.5);
64+
var redLight2 = new Light("red", 0.5);
6665
System.out.println(blueLight.equals(redLight));
67-
68-
// Let's create another red light
69-
var anotherRedLight = new Light("red", 1.0);
70-
System.out.println(redLight.equals(anotherRedLight));
66+
System.out.println(redLight.equals(redLight2));
7167

7268
// ### hashCode()
7369
// You can also ask to get an integer summary (a hash) of any objects.
7470
// This is used to speed up data structures (hash tables).
7571
// Two objects that are equals() must have the same hashCode().
76-
System.out.println(redLight.hashCode());
77-
System.out.println(anotherRedLight.hashCode());
72+
var greenLight = new Light("green", 0.2);
73+
var greenLight2 = new Light("green", 0.2);
74+
System.out.println(greenLight.hashCode());
75+
System.out.println(greenLight2.hashCode());
7876

7977

8078
// ## Summary

Diff for: guide/chapter00-genesis.md

+22-30
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,7 @@ if you are using var, you are asking the compiler to find the type for you.
1818
String colorName = "black";
1919
```
2020

21-
### A record is a user defined type
22-
here Light is defined as containing two components: a color (typed as a String) and
23-
an intensity (typed as a 64 bits floating number double).
24-
```java
25-
record Light(String color, double intensity) {}
26-
```
27-
28-
## System.out.println()
21+
### System.out.println()
2922
To print a value in Java we have a weird incantation `System.out.println()` that we will detail later.
3023
```java
3124
System.out.println(maxIntensity);
@@ -42,63 +35,62 @@ If we want to print a text followed by a value, we use the operator `+`.
4235
System.out.println("the value of colorName is " + colorName);
4336
```
4437

45-
## Object creation with `new`
38+
39+
## A record is a user defined type
40+
here Light is defined as containing two components: a color (typed as a String) and
41+
an intensity (typed as a 64 bits floating number double).
42+
```java
43+
record Light(String color, double intensity) {}
44+
```
45+
46+
### Object creation with `new`
4647
To create an object in memory, we use the operator `new` followed by the value of each record components
4748
the following instruction create a Light with "blue" as color and 1.0 as intensity.
4849
```java
4950
var blueLight = new Light("blue", 1.0);
51+
System.out.println(blueLight);
5052
```
5153

52-
## Record methods
54+
### Record methods
5355
To interact with an object in Java, we use methods, that are functions attached to an object.
5456
To call a method, we use the operator `.` followed by the name of the method and its arguments.
5557
A record automatically declares methods to access its components so Light declares two methods
5658
color() and intensity().
5759

5860
By example to get the intensity of the object blueLight
5961
```java
60-
var blueLightIntensity = blueLight.intensity();
61-
System.out.println(blueLightIntensity);
62+
System.out.println(blueLight.intensity());
6263
```
6364

6465
### toString()
6566
By default a record knows how to transform itself into a String
6667
in Java, the method to transform an object to a String is named toString().
67-
```java
68-
System.out.println(blueLight.toString());
69-
```
70-
7168
In fact, println() calls toString() if the argument is an object
7269
so when using println(), calling explicitly toString() is not necessary.
7370
```java
71+
System.out.println(blueLight.toString());
7472
System.out.println(blueLight);
7573
```
7674

7775
### equals()
78-
Let's create another Light
79-
```java
80-
var redLight = new Light("red", 1.0);
81-
```
82-
8376
In Java, you can ask if two objects are equals, using the method equals(Object).
84-
the return value is a boolean (a primitive type that is either true or false).
77+
The return value is a boolean (a primitive type that is either true or false).
8578
```java
79+
var redLight = new Light("red", 0.5);
80+
var redLight2 = new Light("red", 0.5);
8681
System.out.println(blueLight.equals(redLight));
87-
```
88-
89-
Let's create another red light
90-
```java
91-
var anotherRedLight = new Light("red", 1.0);
92-
System.out.println(redLight.equals(anotherRedLight));
82+
System.out.println(redLight.equals(redLight2));
9383
```
9484

9585
### hashCode()
9686
You can also ask to get an integer summary (a hash) of any objects.
9787
This is used to speed up data structures (hash tables).
9888
Two objects that are equals() must have the same hashCode().
9989
```java
100-
System.out.println(redLight.hashCode());
101-
System.out.println(anotherRedLight.hashCode());
90+
var greenLight = new Light("green", 0.2);
91+
var greenLight2 = new Light("green", 0.2);
92+
System.out.println(greenLight.hashCode());
93+
System.out.println(greenLight2.hashCode());
10294
```
10395

10496

Diff for: jupyter/chapter00-genesis.ipynb

+13-55
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,7 @@
4242
{
4343
"cell_type": "markdown",
4444
"metadata": {},
45-
"source": ["### A record is a user defined type\n", "here Light is defined as containing two components: a color (typed as a String) and\n", "an intensity (typed as a 64 bits floating number double).\n"]
46-
}
47-
,
48-
{
49-
"cell_type": "code",
50-
"execution_count": null,
51-
"metadata": {},
52-
"outputs": [],
53-
"source": ["record Light(String color, double intensity) {}\n"]
54-
}
55-
,
56-
{
57-
"cell_type": "markdown",
58-
"metadata": {},
59-
"source": ["## System.out.println()\n", "To print a value in Java we have a weird incantation `System.out.println()` that we will detail later.\n"]
45+
"source": ["### System.out.println()\n", "To print a value in Java we have a weird incantation `System.out.println()` that we will detail later.\n"]
6046
}
6147
,
6248
{
@@ -98,105 +84,77 @@
9884
{
9985
"cell_type": "markdown",
10086
"metadata": {},
101-
"source": ["## Object creation with `new`\n", "To create an object in memory, we use the operator `new` followed by the value of each record components\n", "the following instruction create a Light with \"blue\" as color and 1.0 as intensity.\n"]
102-
}
103-
,
104-
{
105-
"cell_type": "code",
106-
"execution_count": null,
107-
"metadata": {},
108-
"outputs": [],
109-
"source": ["var blueLight = new Light(\"blue\", 1.0);\n"]
110-
}
111-
,
112-
{
113-
"cell_type": "markdown",
114-
"metadata": {},
115-
"source": ["## Record methods\n", "To interact with an object in Java, we use methods, that are functions attached to an object.\n", "To call a method, we use the operator `.` followed by the name of the method and its arguments.\n", "A record automatically declares methods to access its components so Light declares two methods\n", "color() and intensity().\n"]
116-
}
117-
,
118-
{
119-
"cell_type": "markdown",
120-
"metadata": {},
121-
"source": ["By example to get the intensity of the object blueLight\n"]
87+
"source": ["## A record is a user defined type\n", "here Light is defined as containing two components: a color (typed as a String) and\n", "an intensity (typed as a 64 bits floating number double).\n"]
12288
}
12389
,
12490
{
12591
"cell_type": "code",
12692
"execution_count": null,
12793
"metadata": {},
12894
"outputs": [],
129-
"source": ["var blueLightIntensity = blueLight.intensity();\n", "System.out.println(blueLightIntensity);\n"]
95+
"source": ["record Light(String color, double intensity) {}\n"]
13096
}
13197
,
13298
{
13399
"cell_type": "markdown",
134100
"metadata": {},
135-
"source": ["### toString()\n", "By default a record knows how to transform itself into a String\n", "in Java, the method to transform an object to a String is named toString().\n"]
101+
"source": ["### Object creation with `new`\n", "To create an object in memory, we use the operator `new` followed by the value of each record components\n", "the following instruction create a Light with \"blue\" as color and 1.0 as intensity.\n"]
136102
}
137103
,
138104
{
139105
"cell_type": "code",
140106
"execution_count": null,
141107
"metadata": {},
142108
"outputs": [],
143-
"source": ["System.out.println(blueLight.toString());\n"]
109+
"source": ["var blueLight = new Light(\"blue\", 1.0);\n", "System.out.println(blueLight);\n"]
144110
}
145111
,
146112
{
147113
"cell_type": "markdown",
148114
"metadata": {},
149-
"source": ["In fact, println() calls toString() if the argument is an object\n", "so when using println(), calling explicitly toString() is not necessary.\n"]
150-
}
151-
,
152-
{
153-
"cell_type": "code",
154-
"execution_count": null,
155-
"metadata": {},
156-
"outputs": [],
157-
"source": ["System.out.println(blueLight);\n"]
115+
"source": ["### Record methods\n", "To interact with an object in Java, we use methods, that are functions attached to an object.\n", "To call a method, we use the operator `.` followed by the name of the method and its arguments.\n", "A record automatically declares methods to access its components so Light declares two methods\n", "color() and intensity().\n"]
158116
}
159117
,
160118
{
161119
"cell_type": "markdown",
162120
"metadata": {},
163-
"source": ["### equals()\n", "Let's create another Light\n"]
121+
"source": ["By example to get the intensity of the object blueLight\n"]
164122
}
165123
,
166124
{
167125
"cell_type": "code",
168126
"execution_count": null,
169127
"metadata": {},
170128
"outputs": [],
171-
"source": ["var redLight = new Light(\"red\", 1.0);\n"]
129+
"source": ["System.out.println(blueLight.intensity());\n"]
172130
}
173131
,
174132
{
175133
"cell_type": "markdown",
176134
"metadata": {},
177-
"source": ["In Java, you can ask if two objects are equals, using the method equals(Object).\n", "the return value is a boolean (a primitive type that is either true or false).\n"]
135+
"source": ["### toString()\n", "By default a record knows how to transform itself into a String\n", "in Java, the method to transform an object to a String is named toString().\n", "In fact, println() calls toString() if the argument is an object\n", "so when using println(), calling explicitly toString() is not necessary.\n"]
178136
}
179137
,
180138
{
181139
"cell_type": "code",
182140
"execution_count": null,
183141
"metadata": {},
184142
"outputs": [],
185-
"source": ["System.out.println(blueLight.equals(redLight));\n"]
143+
"source": ["System.out.println(blueLight.toString());\n", "System.out.println(blueLight);\n"]
186144
}
187145
,
188146
{
189147
"cell_type": "markdown",
190148
"metadata": {},
191-
"source": ["Let's create another red light\n"]
149+
"source": ["### equals()\n", "In Java, you can ask if two objects are equals, using the method equals(Object).\n", "The return value is a boolean (a primitive type that is either true or false).\n"]
192150
}
193151
,
194152
{
195153
"cell_type": "code",
196154
"execution_count": null,
197155
"metadata": {},
198156
"outputs": [],
199-
"source": ["var anotherRedLight = new Light(\"red\", 1.0);\n", "System.out.println(redLight.equals(anotherRedLight));\n"]
157+
"source": ["var redLight = new Light(\"red\", 0.5);\n", "var redLight2 = new Light(\"red\", 0.5);\n", "System.out.println(blueLight.equals(redLight));\n", "System.out.println(redLight.equals(redLight2));\n"]
200158
}
201159
,
202160
{
@@ -210,7 +168,7 @@
210168
"execution_count": null,
211169
"metadata": {},
212170
"outputs": [],
213-
"source": ["System.out.println(redLight.hashCode());\n", "System.out.println(anotherRedLight.hashCode());\n"]
171+
"source": ["var greenLight = new Light(\"green\", 0.2);\n", "var greenLight2 = new Light(\"green\", 0.2);\n", "System.out.println(greenLight.hashCode());\n", "System.out.println(greenLight2.hashCode());\n"]
214172
}
215173
,
216174
{

0 commit comments

Comments
 (0)