Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Variables
https://kotlinlang.org/docs/reference/basic-syntax.html#defining-local-variables
Read-only variables
Kotlin let you declare variables as immutable by using the val
keyword :
val a: Int = 42 // Type is declared after name
val b = 5 // Int is inferred
val c: Int // Initialization is deferred so we need to specify type
c = 3
Warning : As in Java (final
) or JavaScript (const
), val
makes only the reference be immutable, not the object being referenced itself :
val languages = mutableListOf("Java", "JavaScript")
languages.add("Kotlin") // [Java, JavaScript, Kotlin]
Mutable variables
Using the var
keyword you can declare a mutable variable =
var d = 41
d += 1 // What is the answer ?
Exercice
Constants
You can also declare compile-time constants with the const
keyword :
const val CONST = 42
Only usable for top-level or object
member and String
"primitive" types
Strings
Type
https://kotlinlang.org/docs/reference/basic-types.html#strings
As in Java String
are immutables. They are also iterables.
Kotlin has two types of strings :
- Literals, using
"
- Raw, using
"""
Raw strings can contain newlines and other characters :
Tell me and I forget.
Teach me and I remember.
Involve me and I learn.
(Benjamin Franklin)
Templates
https://kotlinlang.org/docs/reference/basic-types.html#string-templates
Using $
or ${...}
in a string allows you to reference variables and call functions.