|
42 | 42 | {
|
43 | 43 | "cell_type": "markdown",
|
44 | 44 | "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"] |
60 | 46 | }
|
61 | 47 | ,
|
62 | 48 | {
|
|
98 | 84 | {
|
99 | 85 | "cell_type": "markdown",
|
100 | 86 | "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"] |
122 | 88 | }
|
123 | 89 | ,
|
124 | 90 | {
|
125 | 91 | "cell_type": "code",
|
126 | 92 | "execution_count": null,
|
127 | 93 | "metadata": {},
|
128 | 94 | "outputs": [],
|
129 |
| - "source": ["var blueLightIntensity = blueLight.intensity();\n", "System.out.println(blueLightIntensity);\n"] |
| 95 | + "source": ["record Light(String color, double intensity) {}\n"] |
130 | 96 | }
|
131 | 97 | ,
|
132 | 98 | {
|
133 | 99 | "cell_type": "markdown",
|
134 | 100 | "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"] |
136 | 102 | }
|
137 | 103 | ,
|
138 | 104 | {
|
139 | 105 | "cell_type": "code",
|
140 | 106 | "execution_count": null,
|
141 | 107 | "metadata": {},
|
142 | 108 | "outputs": [],
|
143 |
| - "source": ["System.out.println(blueLight.toString());\n"] |
| 109 | + "source": ["var blueLight = new Light(\"blue\", 1.0);\n", "System.out.println(blueLight);\n"] |
144 | 110 | }
|
145 | 111 | ,
|
146 | 112 | {
|
147 | 113 | "cell_type": "markdown",
|
148 | 114 | "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"] |
158 | 116 | }
|
159 | 117 | ,
|
160 | 118 | {
|
161 | 119 | "cell_type": "markdown",
|
162 | 120 | "metadata": {},
|
163 |
| - "source": ["### equals()\n", "Let's create another Light\n"] |
| 121 | + "source": ["By example to get the intensity of the object blueLight\n"] |
164 | 122 | }
|
165 | 123 | ,
|
166 | 124 | {
|
167 | 125 | "cell_type": "code",
|
168 | 126 | "execution_count": null,
|
169 | 127 | "metadata": {},
|
170 | 128 | "outputs": [],
|
171 |
| - "source": ["var redLight = new Light(\"red\", 1.0);\n"] |
| 129 | + "source": ["System.out.println(blueLight.intensity());\n"] |
172 | 130 | }
|
173 | 131 | ,
|
174 | 132 | {
|
175 | 133 | "cell_type": "markdown",
|
176 | 134 | "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"] |
178 | 136 | }
|
179 | 137 | ,
|
180 | 138 | {
|
181 | 139 | "cell_type": "code",
|
182 | 140 | "execution_count": null,
|
183 | 141 | "metadata": {},
|
184 | 142 | "outputs": [],
|
185 |
| - "source": ["System.out.println(blueLight.equals(redLight));\n"] |
| 143 | + "source": ["System.out.println(blueLight.toString());\n", "System.out.println(blueLight);\n"] |
186 | 144 | }
|
187 | 145 | ,
|
188 | 146 | {
|
189 | 147 | "cell_type": "markdown",
|
190 | 148 | "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"] |
192 | 150 | }
|
193 | 151 | ,
|
194 | 152 | {
|
195 | 153 | "cell_type": "code",
|
196 | 154 | "execution_count": null,
|
197 | 155 | "metadata": {},
|
198 | 156 | "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"] |
200 | 158 | }
|
201 | 159 | ,
|
202 | 160 | {
|
|
210 | 168 | "execution_count": null,
|
211 | 169 | "metadata": {},
|
212 | 170 | "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"] |
214 | 172 | }
|
215 | 173 | ,
|
216 | 174 | {
|
|
0 commit comments