Fisier kt -> .class file
Rulare:
> java -cp ...\kotlin-runtime.jar MainKt
Alta optiune: export jar
> java -jar MainKt.jar
Mai putina "ceremonie":
- poti defini clase oriunde, nu trebuie sa fie in fisier separat
STRINGS
- string interpolation:
println("Display $name")
println("Display $(obj.name)")
- string comparison: if (str1 == str2)
- nullable vs non-nullable
var answer: String; // non-nullable
var answer: String?; // nullable
- string comparison with null check: if (str1?.prop == str2?.prop) // compara doar daca nu sunt nule
IF este o expresie care returneaza o valoare:
var message = if (str1 == str2) { "egal" } else { "gresit" }
WHEN - asemanator cu switch
when (str1) {
"25" -> print("Corect")
else -> print("Gresit")
}
TRY - CATCH
val num: Int = try {
Integer.parseInt("20s")
} catch (e: NumberFormatException) {
-1
} finally {
}
Iteratii - WHILE, FOR
var range = 1..10 // inclusive
// poate fi si range = 'a'..'z' --- orice care implementeaza interfata Comparable
for (i in range step 3) {
println(i)
}
for (i in 10 downTo 1 step 4) {
println(i)
}
for (i in 1 until 10) { // non-inclusive
println(i)
}
LISTE
var nums = listOf(11,22,33)
for (i in nums) {
println(i)
}
for ((index, element) in nums.withIndex()) {
println("$element at index $index")
}
MAPS
var ages = TreeMap<String, Int>()
ages["Geo"] = 33
ages["Petri"] = 7
for ((name, age) in ages) {
println("$name's age is $age")
}
Niciun comentariu:
Trimiteți un comentariu