Var str:String is mutable or immutable?



Is var str:String mutable or immutable?

As per document var is mutable…


And String is immutable…

Please clarify contradiction.


Val states that the reference is immutable, the value can not be reasigned:

val a : String = "hi" 
a = "b"  //not valid because a is reassigned

The variable which is referenced can change, depending on its implementation:

val a = mutableListOf<String>() //can change
val  b = listOf<String>()  //cannot change

The implementation of String is that it can’t change, so String itself is immutable.


And to be quite precise val does not mean immutable, it means read-only. See