1

can someone explain to me the difference between a reference and a referenced object?

Notice that the type of the variable reference (var, val) relates to the reference itself, not the properties of the referenced object. This means that when using a read-only reference (val), we will not be able to change the reference that is pointing to a particular object instance (we will not be able to reassign variable values), but we will still be able to modify properties of referenced objects. Let's see it in action using an array:

val list = mutableListOf("a","b","c") //1
list = mutableListOf("d", "e") //2 Error
list.remove("a") //3
  1. Initialize mutable list
  2. The compiler will throw an error because value reference cannot be changed (reassigned)
  3. The compiler will allow to modify content of the list The keyword val cannot guarantee that the underlying object is immutable.
  • 1
    The explanation you gave is good. Q: What exactly is your question? Q: Does this help: https://stackoverflow.com/questions/44200075/val-and-var-in-kotlin? Specifically [this](https://stackoverflow.com/a/44361695/3135317) response: **var** is like a **general variable** ("mutable"); **val** is like **Final** variable ("constant"; "immutable") – FoggyDay Jan 31 '20 at 17:34
  • `val` is like `final` in kotlin and this is a concept of changing the object state but not the object itself, follow https://stackoverflow.com/questions/2435163/why-can-final-object-be-modified – Pavneet_Singh Jan 31 '20 at 17:34
  • 1
    I can't understand what is it value reference. can you explain me? – Rezo Joglidze Jan 31 '20 at 17:47
  • I think there are 2 underlying questions to the question you are asking. First, val and var, you seem to have the proper understanding. The second part of this, is the type that is assigned may allow internal property manipulation exposed by methods. The reference is not being changed, but the object itself is, while maintaining the same reference. Var would just allow a different reference to be assigned to the variable. There is no restriction by the variable declaration on the internal manipulation of values, this is only done by the type itself. – Benjamin Charais Jan 31 '20 at 18:22
  • 1
    Q: What is "value reference"? A: Item 2. is poorly worded. What they're trying to say is a) "list" has references to objects with values "a", "b" and "c". b) That list is immutable. c) Consequently, you *CANNOT* change it to now contain the *DIFFERENT* vaues "d" and "e". That would be an "error". "list" is the "value reference" in this example. Q: Does that help? – FoggyDay Jan 31 '20 at 19:34

1 Answers1

0

Consider do following code:

data class ReferencedObject(
    val valAttribute: String, 
    var varAttribute: String
)

fun main() {
    val reference = ReferencedObject(
        valAttribute = "cant change",
        varAttribute = "can change"
    )

//    it doesn't compile because reference can't be reassigned
//    val reference = ReferencedObject(
//        valAttribute = "cant change",
//        varAttribute = "can change"
//    )

//    it doesn't compile because valAttribute of referenced object can't be reassigned
//    reference.valAttribute = "change"

    // it works because the var attribute of referenced object can be reassigned
    reference.varAttribute = "change"
}

In this example, reference is a final "variable" that points to an object of type ReferencedObject. The referenced object has two attributes: a valAttribute which is imutable and varAttribute which is not.

So, when you run reference.varAttribute = "change" you are not changing the reference variable, you are changing the referenced object.

This is what is happening in your example, when you run list.remove("a") you are changing the state of the MutableList object that is referenced by the list reference "variable".

Diego Magdaleno
  • 831
  • 8
  • 20