Bezeichner |
.small-font.inline-code[```ebnf
java_id = {letter|"$"|"_"}{letter|"$"|"_"|digit}*
```]
.small-font.inline-code[```ebnf
identifier = java_id | "`" java_id "`"
```]
- .darkblue[einheitliche Schreibweise] für alle Arten von Bezeichnern
- .darkblue[Unterscheidung von Klein-/Großschreibung] (Case Sensitive)
- .darkblue[Escaping für Schlüsselwörter] (Java-Interop.) Bsp: .small-font.inline-code[```kotlin2
foo.`is`()```]
|
Anweisungen |
.small-font.inline-code[```kotlin2
{ var k = 42; println(5*k) }```]
- .darkblue[.bold[optionales] Semikolon am Zeilenende]
- .darkblue[Semikolon erforderlich zwischen Anweisungen in einer Zeile]
- .darkblue[Anweisungsfolge definiert Scope]: Variablen können gleichnamige in beliebigen äußeren Scopes überdecken
|
Bedingte Anweisung |
.small-font.inline-code[```kotlin2
if(aBei einzelnen Anweisungen sind die .monospace.hljs-operator[{}]-Klammern optional
.darkgray[(fast)] .darkblue[alle Konstrukte sind Anweisungen wie auch Ausdrücke]
|
while-Schleife |
.small-font.inline-code[```kotlin2
var i = 0; while(i < n) { println("+"); i++; }```]
|
for-Schleife |
.small-font.inline-code[```kotlin2
for (i in 0 .. n-1) { println("+"); }```]
|
return-Anweisung |
.small-font.inline-code[```kotlin2
return n*42; return "+";```]
|
Parameterübergabe |
- standardmäßig mittels .darkblue.bold[call-by-value]
|
Kommentare |
- .darkblue[Einzeilig]: .monospace.hljs-comment[// Kommentar]
- .darkblue[Mehrzeilig (verschachtelbar)]: .small-font.inline-code[```javascript
/* Kommentar … */```]
|
]
---
# .darkblue[(Un-)Veränderbare Var. und Collections]
.condensed.small-font[
|
Veränderbar |
Unveränderbar |
Deklaration Variable/ Konstante |
.small-font.smaller-padding-top.small-padding-bottom[```kotlin2
var i = 0 // Typ Int inferiert!
var j:Int // Typangabe notw.
j=42; i++; j-=2
```]
|
.small-font.smaller-padding-top.small-padding-bottom[```kotlin2
val i = 0 // Typ Int inferiert!
val j:Int // Typangabe notw.
j=42
```]
|
Listen |
.small-font.smaller-padding-top.small-padding-bottom[```kotlin2
// Typ: MutableList
val n = mutableListOf(1, 2, 3)
```]
- Liste .monospace[n] ist veränderbar, aber eine andere Liste kann nicht zugewiesen werden
$\Rightarrow$ In dem Fall Verwendung von .monospace.hljs-keyword[var]
|
.small-font.smaller-padding-top.small-padding-bottom[```kotlin2
val readOnly: List = n
val immutable = listOf(1, 2, 3)
```]
- Durch .monospace[readOnly] ist die Liste .monospace[n] nicht veränderbar, aber durch .monospace[n] die zugrundeliegende Liste von .monospace[readOnly]. Liste .monospace[immutable] ist vollständig unveränderbar!
|
Sets |
.small-font.smaller-padding-top.small-padding-bottom[```kotlin2
val ms = hashSetOf(1,2)
ms += 3 // ms.add(3)
```]
|
.small-font.smaller-padding-top.small-padding-bottom[```kotlin2
val s = setOf(1,2)
if(1 in s) println("in")
```]
|
Maps |
.small-font.smaller-padding-top.small-padding-bottom[```kotlin2
val map = mutableMapOf(2 to "y")
map[1] = "x" // map.put(1,"x")
println(map) // {1=x, 2=y}
```]
|
.small-font.smaller-padding-top.small-padding-bottom[```kotlin2
val readMap = mapOf("foo" to 1,
"bar" to 2)
println(readMap["foo"]) // "1"
```]
|
]
---
# .darkblue[Primitive Datentypen]
.small-font[
Einträge |
Paare $(k, v)$ mit $k$ ganzzahl. Schlüssel und zugeordneter Wert $v$ |
Erzeugung von Arrays |
Leeres Array |
.inline-code[```kotlin2
val emptyArray = arrayOfNulls(3)```]
|
Liste von Werten |
.inline-code[```kotlin2
// indiziert von 0 an:```] .inline-code[```kotlin2
val monthName = arrayOf("", "Jan", "Feb", "Mar")```]
|
Initialisierungs- funktion |
.inline-code[```php
// Creates an Array with values ["0", "1", "4"]```] .inline-code[```kotlin2
val squares = Array(3, { i -> (i * i).toString() })```]
|
Assoziatives Array |
≈ .monospace.hljs-built_in[Map], schon besprochen
|
Zugriff |
Explizite Zuweisung |
.inline-code[```kotlin2
emptyArray[0] = "hello" // emptyArray.set(0,"hello")```]
|
Lesezugriff |
.inline-code[```kotlin2
println(squares[2]) // squares.get(2)```]
|
Aufzählung aller Elemente |
.inline-code[```kotlin2
val a = arrayOf(1,2,3)```] .inline-code[```kotlin2
for(v in a) println(v)```] .inline-code[```kotlin2
a.forEachIndexed { k, v -> println("$k => $v")}```]
|
]
---
# .darkblue[Smart Casts]
.more-condensed[
- Ermittlung impliziter Typen durch statische Kontrollflussanalyse .darkblue.bold[⇝ automatische Typumwandlungen]
- unter Berücksichtigung von Datentypüberprüfungen mittels .monospace.hljs-keyword[is] und Typumwandlungen mittels .monospace.hljs-keyword[as] im Kontrollfluss
]
.small-font.small-margin-bottom[```kotlin2
if (x is String)
print(x.length) // x is automatically cast to String
```]
.small-font.small-margin-bottom[```kotlin2
if (x !is String) return
print(x.length) // x is automatically cast to String
```]
.small-font.small-margin-bottom[```kotlin2
x as String // unsafe cast: throws an exception if the cast is not possible
print(x.length) // x is automatically cast to String
```]
.small-font.small-margin-bottom[```kotlin2
val x: String? = y as? String // safe cast: x = null if the cast is not possible
```]
---
# .darkblue[Null Safety]
.reference[