Coding Friction

kotlin var vs val

var – Variable delcare with var keywordare are Mutable Variables, value of variable can be changed

fun main() {
    var color = "Green"
    color = "Red"

val – Variable declare with val keyword are Immutable Variables, this is read only variables. it’s not constant value. It is similar to final variable in Java.

val birthYear = 23
    birthYear = 25
    println(birthYear) //get an error val cannot be reassigned 

Leave a Comment

Your email address will not be published. Required fields are marked *