I'm starting to play with Scala, and one of the first things I read is that vals are:
variables that are assigned once and never change, and vars, variables that may change over their lifetime
But I'm curious why I can do this:
val foo = Array(1, 3 ,2)
scala.util.Sorting.quickSort(foo)
If I check the foo variable now is ordered, which means it has changed... also if I do print(foo), both have the same, so the variable is pointing to the same object (I could have thought that the variable just pointed to a new object)
Could anyone clarify?
The Array pointed to by the foo
variable is changing, but the fact that foo
points at that Array
doesn't change. Try re-assigning foo
and you will see what you are looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With