-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
552 lines (487 loc) · 16.4 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Kotlin - From a Java Developer's perspective</title>
<meta name="description" content="An introduction to Kotlin from a Java developer's perspective">
<meta name="author" content="Dennis Lotz">
<link rel="stylesheet" href="dist/reset.css">
<link rel="stylesheet" href="dist/reveal.css">
<link rel="stylesheet" href="css/theme/kotlin.css" id="theme">
<!-- Theme used for syntax highlighted code -->
<link rel="stylesheet" href="plugin/highlight/darcula.css" id="highlight-theme">
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1>Kotlin</h1>
<h3>From a Java Developer's perspective</h3>
</section>
<section>
<h2>What is Kotlin?</h2>
<ul>
<li>object-oriented & functional</li>
<li>statically typed with type inference</li>
<li>concise</li>
<li>compiles to JVM byte code</li>
</ul>
</section>
<section>
<h2>Less verbose</h2>
<q cite="https://developer.android.com/kotlin/">
Kotlin's modern language features allow you to focus on expressing your ideas and write less boilerplate code.
Less code written also means less code to test and maintain.
</q>
<p class="text-right">developer.android.com</p>
</section>
<section>
<h2>Less verbose</h2>
<p>Java</p>
<pre><code class="java" data-trim>
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello World!”);
}
}
</code></pre>
<p>Kotlin</p>
<pre><code class="kotlin" data-trim>
fun main() {
println(“Hello World”)
}
</code></pre>
</section>
<section>
<h2>Semicolons are optional</h2>
<pre><code class="kotlin" data-trim>
println(“Hello World”);
</code></pre>
<p>is equivalent to</p>
<pre><code class="kotlin" data-trim>
println(“Hello World”)
</code></pre>
</section>
<section>
<h2>Return types</h2>
<p><code>Unit</code> return type can be omitted</p>
<pre><code class="kotlin" data-trim>
fun main(): Unit {
println(“Hello World”)
}
</code></pre>
<p>is equivalent to</p>
<pre><code class="kotlin" data-trim>
fun main() {
println(“Hello World”)
}
</code></pre>
</section>
<section>
<h2>Type inference</h2>
<pre><code class="kotlin" data-trim>
val a: Int = 3
val b: Int
b = 5
</code></pre>
<p>is equivalent to</p>
<pre><code class="kotlin" data-trim>
val a = 3 // type Int gets inferred
val b: Int
b = 5
</code></pre>
</section>
<section>
<h2>Visibility Modifiers</h2>
<ul>
<li><code>private</code></li>
<li><code>protected</code></li>
<li><code><span class="out-of-scope">internal</span></code></li>
<li><code>public</code> (default)</li>
</ul>
<pre><code class="kotlin" data-trim>
private val a = 3
val b = 5 // public by default
</code></pre>
</section>
<section>
<h2><code>var</code> and <code>val</code></h2>
<pre><code class="kotlin" data-trim data-noescape>
var a = "mutable String"
val b = "immutable String"
a = "new value"
<span class="compiler-error">b = "new value"</span> // compilation error
</code></pre>
</section>
<section>
<h2>Accessing properties</h2>
<p>Java</p>
<pre><code class="Java" data-trim>
String name = somePerson.getName();
</code></pre>
<p>Kotlin</p>
<pre><code class="kotlin" data-trim>
val name = somePerson.name
</code></pre>
</section>
<section>
<h2>Assigning properties</h2>
<p>Java</p>
<pre><code class="Java" data-trim>
somePerson.setName("Dennis");
</code></pre>
<p>Kotlin</p>
<pre><code class="kotlin" data-trim>
somePerson.name = "Dennis"
</code></pre>
</section>
<section>
<h2>Creating new instances</h2>
<p>Java</p>
<pre><code class="java" data-trim>
Person somePerson = new Person("Dennis");
</code></pre>
<p>Kotlin (no <code>new</code> keyword)</p>
<pre><code class="kotlin" data-trim>
val somePerson = Person("Dennis")
</code></pre>
</section>
<section>
<h2>Named arguments</h2>
<pre><code class="kotlin" data-trim>
class User(val username: String, val password: String)
val admin = User(username = "admin", password = "12345")
</code></pre>
<p>order doesn't matter</p>
<pre><code class="kotlin" data-trim>
val user = User(password = "54321", username = "user")
</code></pre>
</section>
<section>
<h2>Default arguments</h2>
<pre><code class="kotlin" data-trim>
class User(
val username: String,
val password: String = "not-so-secure-password"
)
val admin = User(username = "admin")
</code></pre>
</section>
<section>
<h2>Single-expression functions</h2>
<pre><code class="kotlin" data-trim>
fun theAnswer(): Int {
return 42
}
</code></pre>
<p>is equivalent to</p>
<pre><code class="kotlin" data-trim>
fun theAnswer() = 42
</code></pre>
</section>
<section>
<h2>Null Safety</h2>
<q cite="https://en.wikipedia.org/wiki/Tony_Hoare#Apologies_and_retractions">
I call it my billion-dollar mistake. It was the invention of the null reference in 1965.
</q>
<p class="text-right">Tony Hoare</p>
</section>
<section>
<h2>Null Safety</h2>
<pre><code class="kotlin" data-trim data-noescape>
var a: String = "abc"
<span class="compiler-error">a = null</span> // compilation error
var b: String? = "abc"
b = null // ok
</code></pre>
</section>
<section>
<h2>Safe call operator - <code>?.</code></h2>
<pre><code class="kotlin" data-trim data-noescape>
val a: String = "abc"
a.length
val b: String? = "abc"
<span class="compiler-error">b.length</span> // compilation error
b?.length
</code></pre>
</section>
<section>
<h2>Elvis operator - <code>?:</code></h2>
<p>If the expression to the left of <code>?:</code> is not null, the elvis operator returns it, otherwise it
returns the expression to the right.</p>
<pre><code class="kotlin" data-trim data-noescape>
val b: String? = "abc"
val l = b?.length ?: -1
</code></pre>
</section>
<section>
<h2>Template Strings</h2>
<pre><code class="kotlin" data-trim>
val name = "Dennis"
println("My name is $name")
</code></pre>
<pre><code class="shell" data-trim>
$ My name is Dennis
</code></pre>
</section>
<section>
<h2>Data classes</h2>
<ul>
<li><code>equals()</code></li>
<li><code>hashCode()</code></li>
<li><code>toString()</code></li>
<li><code>copy()</code></li>
<li>Destructuring Declarations</li>
</ul>
</section>
<section>
<h2>Data classes</h2>
<pre><code class="kotlin" data-trim>
data class User(
val username: String,
val password: String
)
val admin = User("admin", "12345")
</code></pre>
</section>
<section>
<h2>Destructuring Declarations</h2>
<pre><code class="kotlin" data-trim>
data class User(
val username: String,
val password: String
)
val (username, password) = User("admin", "12345")
</code></pre>
</section>
<section>
<h2>Destructuring Declarations</h2>
<p>very handy in maps</p>
<pre><code class="kotlin" data-trim>
for ((key, value) in map) {
println("Key: $key, value: $value")
}
</code></pre>
</section>
<section>
<h2>Smart Casts</h2>
<p><code>is</code> check</p>
<pre><code class="kotlin" data-trim>
if (x is String) {
println(x.length) // x is automatically cast to String
}
</code></pre>
<p>negative <code>is</code> check</p>
<pre><code class="kotlin" data-trim>
if (x !is String) return
println(x.length) // x is automatically cast to String
</code></pre>
</section>
<section>
<h2>Smart Casts</h2>
<p><code>when-expressions</code></p>
<pre><code class="kotlin" data-trim>
when (x) {
is Int -> println(x + 1) // x is Int
is String -> println(x.length + 1) // x is String
is IntArray -> println(x.sum()) // x is IntArray
}
</code></pre>
</section>
<section>
<h2>Collections</h2>
<p>Immutable</p>
<pre><code class="kotlin" data-trim>
val x = listOf(a, b, c)
val y = setOf(a, b, c)
val z = mapOf(
Pair("a", a),
Pair("b", b),
Pair("c", c)
)
</code></pre>
</section>
<section>
<h2>Collections</h2>
<p>Mutable</p>
<pre><code class="kotlin" data-trim>
val x = mutableListOf(a, b, c)
val y = mutableSetOf(a, b, c)
val z = mutableMapOf(
Pair("a", a),
Pair("b", b),
Pair("c", c)
)
</code></pre>
</section>
<section>
<h2>Ranges</h2>
<p>ascending</p>
<pre><code class="kotlin" data-trim>
for(i in 1..4) {
println("Current Value is $i")
}
</code></pre>
<p>descending</p>
<pre><code class="kotlin" data-trim>
for(i in 4 downTo 1) {
println("Current Value is $i")
}
</code></pre>
</section>
<section>
<h2>Ranges with step</h2>
<p>ascending</p>
<pre><code class="kotlin" data-trim>
for(i in 1..4 step 2) {
println("Current Value is $i")
}
</code></pre>
<p>descending</p>
<pre><code class="kotlin" data-trim>
for(i in 4 downTo 1 step 2) {
println("Current Value is $i")
}
</code></pre>
</section>
<section>
<h2>Extension Functions</h2>
<q cite="https://kotlinlang.org/docs/reference/extensions.html">
Provide the ability to extend a class with new functionality without having to inherit from the class
or use any type of design pattern such as Decorator.</q>
<p class="text-right">kotlinlang.org</p>
</section>
<section>
<h2>Util classes in Java</h2>
<pre><code class="java" data-trim>
public class CollectionUtil {
public static <T> void swap(
List<T> list,
int index1,
int index2
) {
T tmp = list.get(index1);
list.set(index1, list.get(index2));
list.set(index2, tmp);
}
}
</code></pre>
<pre><code class="java" data-trim>
List<Integer> list = Arrays.asList(1,2,3);
CollectionUtil.swap(list, 0, 2);
</code></pre>
</section>
<section>
<h2>Extension Functions</h2>
<p>Replacement of Java Util function</p>
<pre><code class="kotlin" data-trim>
fun <T> MutableList<T>.swap(index1: Int, index2: Int) {
val tmp = this[index1]
this[index1] = this[index2]
this[index2] = tmp
}
</code></pre>
<pre><code class="java" data-trim>
val list = mutableListOf(1, 2, 3)
list.swap(0, 2)
</code></pre>
</section>
<section>
<h2>Java Interoperability</h2>
<q cite="https://kotlinlang.org/docs/reference/java-interop.html#calling-java-code-from-kotlin">
Kotlin is designed with Java Interoperability in mind. Existing Java code can be called from Kotlin in a
natural way, and Kotlin code can be used from Java rather smoothly as well.
</q>
<p class="text-right">kotlinlang.org</p>
</section>
<section>
<h2>Java Interoperability</h2>
<p>Calling Java from Kotlin</p>
<pre><code class="kotlin" data-trim>
import java.time.LocalDate
fun getDate7DaysAgo(): LocalDate {
return LocalDate.now().minusDays(7)
}
</code></pre>
</section>
<section>
<h2>Java Interoperability</h2>
<p>Calling Kotlin from Java</p>
<pre><code class="kotlin" data-trim>
data class Person(var name: String)
</code></pre>
<pre><code class="java" data-trim>
import util.Person;
public class JavaInterop {
public static void main(String[] args) {
Person dennis = new Person("Dennis");
String name = dennis.getName();
}
}
</code></pre>
</section>
<section>
<h2>Java Interoperability</h2>
<p>Control JVM byte code generation via annotations</p>
<ul>
<li>@JvmName</li>
<li>@JvmMultifileClass</li>
<li>@JvmField</li>
<li>@JvmStatic</li>
<li>@JvmDefault</li>
<li>...</li>
</ul>
</section>
<section>
<h2>More awesome features</h2>
<ul>
<li>Coroutines</li>
<li>Kotlin Native</li>
<li>Scope functions</li>
<li>Infix notation</li>
<li>Ability to create DSLs</li>
<li>Contracts</li>
<li>...</li>
</ul>
</section>
<section>
<h2>Further information</h2>
<ul>
<li><a
href="https://play.kotlinlang.org/byExample/overview">https://play.kotlinlang.org/byExample/overview</a>
</li>
<li><a href="https://play.kotlinlang.org/koans/overview">https://play.kotlinlang.org/koans/overview</a></li>
<li><a href="https://kotlinlang.org/docs/reference/">https://kotlinlang.org/docs/reference/</a></li>
<li><a href="https://kotlinlang.org/docs/books.html">https://kotlinlang.org/docs/books.html</a></li>
<li><a href="https://medium.com/@elizarov">Medium - Roman Elizarov</a></li>
<li><a href="https://www.youtube.com/playlist?list=PLQ176FUIyIUbVvFMqDc2jhxS-t562uytr">YouTube - KotlinConf
2018</a></li>
</ul>
</section>
<section>
<h2>Thank you!</h2>
<p> Questions?</p>
</section>
</div>
<div class="author">
Dennis Lotz
</div>
</div>
<script src="dist/reveal.js"></script>
<script src="plugin/notes/notes.js"></script>
<script src="plugin/markdown/markdown.js"></script>
<script src="plugin/highlight/highlight.js"></script>
<script>
// More info about initialization & config:
// - https://revealjs.com/initialization/
// - https://revealjs.com/config/
Reveal.initialize({
hash: true,
controls: false,
// Learn about plugins: https://revealjs.com/plugins/
plugins: [RevealMarkdown, RevealHighlight, RevealNotes]
});
</script>
</body>
</html>