Professional Documents
Culture Documents
Koltin Info
Koltin Info
JAVA STARS
KOTLIN
EVERYTHING IS
AN OBJECT
val i: Int = 7
val d: Double = i.toDouble()
val c: Char = 'c'
val i: Int = c.toInt()
PROPERTIES
public class Person {
...
...
get() = field.toUpperCase()
set(value) {
field = "Name: $value"
}
}
;
SEMICOLONS ARE NOT
NECESSARY
VAL VS VAR
MUTABLE (VAR)
Person person = new Person();
person = Person();
IMMUTABLE (VAL)
val s = "Example" // A String
val i = 23 // An Int
VAL
AS MUCH AS POSSIBLE
CLASSES
class MainActivity {
}
class Person(name: String, surname: String) {
init {
... // Default logic in the constructor
}
}
open class Animal(name: String)
class Person(firstName: String, lastName: String) : Animal(firstName)
FUNCTIONS
fun onCreate(savedInstanceState: Bundle?) {
}
THE BILLION-DOLLAR MISTAKE
NULL-SAFETY
Forecast forecast = null;
forecast.toString();
QUESTION MARKS TO IDENTIFY NULLABLE TYPES
val a: Int? = null
val a: Int? = null
a.toLong()
val a: Int? = null
...
if (a != null) {
a.toLong()
}
SAFE CALL OPERATOR (?.)
val a: Int? = null
...
a?.toLong()
ELVIS OPERATOR (?:)
val a: Int? = null
...
val myLong = a?.toLong() ?: 0L
val myLong = a?.toLong() ?: return false