Kotlin Variable Declaration
Variable Declaration
Basically, variables are declared as follows.
val(or var) variableName: Type = value
There are two types of variable declarations: immutable variables and mutable variables.
Mutable Variables (var)
When declared with var, a variable can be reassigned. In other words, it becomes a mutable variable. var means variable.
var a: Int = 1
var b = 2
var c: Int
c = 3
If an initial value is specified when declaring the variable, Kotlin also provides type inference, which lets you omit the variable type.
Immutable Variables (val)
When declared with val, a variable cannot be reassigned. In other words, it becomes an immutable variable. val means value.
val MAX_COUNT: Int = 1024
val MAX_SIZE = 1024
A variable using the val keyword can be initialized only once in context. It is similar to Java’s final.
Compiling var and val
var can be assigned anytime and anywhere, but if a variable is declared with val, reassignment causes a compile error.
var foo = "a"
hoge = "b" // OK
val foo = "a"
fuga = "b" // Compile error
Repeatedly assigning values multiple times is not recommended, so use val by default.
Of course, there are cases where var must be used.
Constant Declaration (const)
When declared with const, you can declare a more efficient constant.
const val FOO = "Foo"
Variable Data Types
VariableTutorial.kt
fun main(args: Array<String>) {
val a: Int
var b: String
val c = 3;
var d = "dString"
a = 34
b = "bString"
println("a = $a is Int? ${a is Int}")
println("b = $b is String? ${b is String}")
println("c = $c is Int? ${c is Int}")
println("d = $d is String ${d is String}")
}
a = 34 is Int? true
b = bString is String? true
c = 3 is Int? true
d = dString is String true
Unlike Java, you do not need to write data types such as int or String; it is enough to specify var or val.
One point to note is that when only declaring a variable, you must specify the data type, such as var a: Int, val b: String. However, when declaring a variable and defining its value, as in var a = 34, the data type is assigned automatically and can be omitted. This is called type inference.
Non-Null Variables and Nullable Variables
null means that no value exists. Normal variables cannot be assigned null. By adding a question mark (?) to the type name, you can define a variable that allows null, also called a nullable parameter.
var a: String = null // null cannot be assigned
var b: String? = null // null can be assigned
Nullable variables cannot call properties and functions as-is.
fun main() {
var a: String? = "ABC"
var b: String? = "ABC"
var c: String? = "ABC"
var d: String? = "ABC"
// Treated as non-nullable inside an if statement that checks for non-null (smart cast)
if (a != null) { println(a.length) }
// Using ?. makes the whole expression null if b is null (safe call)
println(b?.length)
// Using !! forces access (dangerous because it can cause a runtime error)
println(c!!.length)
// Use the Elvis operator (?:) to prevent the value from becoming null.
d = d ?: "(Unknown)"
println(d.length)
}