Read-only local variables are defined using the keyword val. They can be assigned a value only once. val use to declare final variable.
val in Kotlin
val is same as the final keyword in java. As you should probably know that we cannot reassign value to a final variable but can change its properties. Similarly, val is like Final variable and its known as immutable in kotlin and can be initialized only single time.
Characteristics of val variables in Kotlin:
1. Initialization of val in Kotlin
1.1 Immediate assignment
val a: Int = 1
val b = 2 // `Int` type is inferred
1.2 deferred assignment
val c: Int // Type required when no initializer is provided
c = 3 // deferred assignment
1.3 Must be initialized
Refer more about https://kotlinlang.org/docs/reference/basic-syntax.html#defining-variables
1.4 value can not be changed or reassign
1.5 val is same as the final modifier in java. can not assign to a final variable again but can change its properties.
Uses: val is used for getter (read-only, value won't change).
Extra:
val variables are not necessarily immutable. They are final -- only the reference is immutable -- but if the object stored in the val is mutable, the object is mutable regardless of whether it is assigned via val or var.
Var in Kotlin:
var in kotlin used to declare a variable which is like general variable and its known as a mutable variable in kotlin and
If you use the var keyword to declare Variables that can be reassigned (assigned multiple times)
Characteristics of var variables in Kotlin:
1. variables defined with var are mutable(Read and Write)
2. We can initialize later by using lateinit modifier [lateinit also use for global variable we can not use it for local variable] value can be changed or reassign but not in global scope
var is as a general variable
Uses:
1. use var where value is changing frequently
2. var is used for setter (value will change).
Difference
val is immutable and var is mutable in Kotlin.
Example:
package com.example.espl.val_vs_var_in_kotlin
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
class MainActivity : AppCompatActivity() {
val a: Int = 1 // immediate assignment
val b = 2 // `Int` type is inferred
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val c: Int // Type required when no initializer is provided
// Log.e("IntVal",""+c) // Warning: Variable 'c' must be initialised
c = 3 // deferred assignment
Log.e("val c",":: "+c)
val person=Person("Ram", 50, Address("kaka ni pol","New street", "Ahmedabad"))
Log.e("Value of city of person","- City:: "+person.address.city)
// person.address.city="Rajkot"; // Val cannot be reassigned
person.address.street="Dalal street"; // Var street can be reassigned
person.address.name="Maharshi"; // Val name can be reassigned
Log.e("log of val person","after change of address name,street :: "+person.toString()) //
val person1=Person("Raj", 50, Address("Raj street name","Raj street", "Rajkot"))
// person=person1 // Warning: Val person cannot be reassigned
// person=Person("Lakhan", 52, Address("kaka ni pol","New street", "Ahmedabad")) // Warning: Val person cannot be reassigned
person.address=person1.address // we can reassign the address of a val person as its variable
// This proves that val cannot be reassigned but its properties defined using the val can be changed.
//val is used for getter (read-only, value won't change).
// person.age=person1.age // Warning: Val age cannot be reassigned
// person.age=51 // Warning: Val age cannot be reassigned
//var is used for setter (value will change).
person.name="Tarun"
Log.e("log of val person","after change of var address :: "+person.toString()) //
}
data class Person(var name: String, val age: Int,var address: Address)
data class Address(var name : String, var street : String, val city : String)
// OutPut
//04-24 12:30:36.338 5104-5104/com.example.espl.val_vs_var_in_kotlin E/val c: :: 3
//04-24 12:27:08.054 1661-1661/com.example.espl.val_vs_var_in_kotlin E/Value of city of person: - City:: Ahmedabad
//04-24 12:27:08.054 1661-1661/com.example.espl.val_vs_var_in_kotlin E/log of val person: after change of address name,street :: Person(name=Ram, age=50, address=Address(name=Maharshi, street=Dalal street, city=Ahmedabad))
//04-24 12:27:08.054 1661-1661/com.example.espl.val_vs_var_in_kotlin E/log of val person: after change of var address :: Person(name=Tarun, age=50, address=Address(name=Raj street name, street=Raj street, city=Rajkot))
}