Ben Edwards
08/12/2022, 7:44 PM=
sign does not copy the object itself, it only copies a reference to it.". This only seems the case for immutable objects. if I made the variable a var this is not the case. This seems very important. I'm getting very disillusioned with the JetBrains Academy course. It's very slow going but still manages to seem to oversimplify things. Can anyone recommend a good beginners course with lots of excursuses? So far my favourite one is https://www.udemy.com/course/100-days-of-code/ (bit its a Python course). It covers a lot and the docent is not too bogged down with the fiddly, not very useful stuff. I really appreciate the effort they put in but I want something that is a bit more fun. I don't mind paying a bit.Landry Norris
08/12/2022, 7:47 PMclass MyClass(var x: Int)
and I write
val foo = MyClass(10)
val bar = foo
foo.x = 15
println("bar.x is ${bar.x}")
I’d expect to get “bar.x = 15”Landry Norris
08/12/2022, 7:49 PMbar = foo
, it copies the contents of foo into bar. Kotlin does not. If you’re familiar with pointers, you can think of it more like bar = &foo
where bar is actually a MyClass*
ephemient
08/12/2022, 7:50 PMBen Edwards
08/12/2022, 7:54 PMval msg1 = "Hi"
var msg2 = msg1
msg2 = "Ben"
println(msg1)
println(msg2)
Outputs
Hi
Ben
So does not seem to of just copied the reference to itBen Edwards
08/12/2022, 7:55 PMBen Edwards
08/12/2022, 7:56 PMJacob
08/12/2022, 7:56 PMmsg2 = "Ben"
has no effect on msg1. msg1 is still a reference to "Hi"Landry Norris
08/12/2022, 7:57 PMephemient
08/12/2022, 7:58 PMmsg2
is not a reference to `msg1`; it is first a reference to the object referred to by msg1
("Hi"
) and then a reference to "Ben"
.Ben Edwards
08/12/2022, 7:59 PMJacob
08/12/2022, 8:08 PMx=foo
then x is a reference to the same object as foo. if I mutate the object referenced by foo, then the object referenced by x will also be mutated.
Note: writing foo=somethingElse
is not a mutation of the object referenced by foo. You're simply making foo reference something new. to mutate the object referenced by foo you'll usually need to change a property of it ie foo.bar =2
now in some languages when I say x=foo
I actually make copy of foo so that if I write x.bar=2
it will have no effect on foo. Kotlin is not such a languageBen Edwards
08/12/2022, 8:14 PMBen Edwards
08/12/2022, 8:15 PMval msg1 = "Hi"
var msg2 = msg1
msg2 = "Ben"
println(msg1)
println(msg2)
This is not happening.Ben Edwards
08/12/2022, 8:19 PMephemient
08/12/2022, 8:23 PM.toList()
, .toMutableList()
, .toSet()
, .toMutableSet()
, .toMap()
, .toMutableMap()
, yes they all create a copyJacob
08/12/2022, 8:26 PMJacob
08/12/2022, 8:26 PMJacob
08/12/2022, 8:29 PMephemient
08/12/2022, 8:29 PMString
is immutable, a reference to a string is mutable if it's a var
(can be replaced by a reference to a different string). a StringBuilder
is mutable, a reference to a string builder is immutable if it's a val
(the builder can still be mutated)ephemient
08/12/2022, 8:31 PMList
being "immutable" just means you can't mutate it through this reference (without downcasts); it's possible that a different reference can mutate it. (I think it would be better called unmodifiable) e.g.
val x: MutableList<Int> = mutableListOf(1, 2, 3)
val y: List<Int> = x
y == listOf(1, 2, 3)
y.add(4) // not allowed
x.add(5) // allowed
y == listOf(1, 2, 3, 5)
Joffrey
08/12/2022, 8:33 PMJoffrey
08/12/2022, 8:39 PMJoffrey
08/12/2022, 8:40 PMBen Edwards
08/12/2022, 8:44 PMBen Edwards
08/12/2022, 8:48 PMLandry Norris
08/12/2022, 8:50 PMLandry Norris
08/12/2022, 8:53 PMVampire
08/12/2022, 9:14 PMval msg1 = "Hi"
var msg2 = msg1
msg2 = "Ben"
println(msg1)
println(msg2)
has nothing to do with the statement your quoted.
I think you still didn't quite understand it.
• "Hi"
is an immutable instance of String
with value Hi
• "Ben"
is an immutable instance of String
with value Ben
• msg1
is an immutable variable that can point to an instance of String
• msg2
is a mutable variable that can point to an instance of String
So after
val msg1 = "Hi"
var msg2 = msg1
msg1
and msg2
both point to the instance "Hi"
And after
msg2 = "Ben"
msg1
still points at the instance "Hi"
but msg2
now points at the instance "Ben"
.
You nowhere try to modify the properties of the instance either variable is pointing to, but you just modify what independent instances the variables are pointing at.
So your example has nothing to do with the statement you quoted.
If you have
data class Foo(var bar: String)
val msg1 = Foo("Hi")
val msg2 = msg1
msg2.bar = "Ben"
println(msg1.bar)
println(msg2.bar)
then you see what the sentence is meaning, that msg1
and msg2
point to the same instance that you modified with the msg2.bar = ...
line.ephemient
08/12/2022, 9:21 PMval a = "Hi"
val b = "H" + "i"
a == b
a === b // … maybe, but it shouldn't matter
ephemient
08/12/2022, 9:25 PMLandry Norris
08/12/2022, 9:26 PMVampire
08/12/2022, 9:33 PMBen Edwards
08/12/2022, 10:14 PMJacob
08/12/2022, 10:38 PMVampire
08/12/2022, 10:42 PMmsg1
and msg2
are never the same.
They are two separate things that can point to an instance.
They can point to the same instance or they can point to different instances.
And if they point to the same instance, then modifying the instance the one points to, of course also modifies the instance the other points to as they point to the same instance.
But msg1
and msg2
, the variables - not the instances they point to -, are always separate things and modifying what one points to has no influence on what the other points to.
Maybe you should start with a general course about object oriented programming paradigm to learn some basics.Joffrey
08/13/2022, 5:04 AMmsg1
and msg2
are the same, I think he meant "when they are set to the same reference"Vampire
08/13/2022, 11:52 AMJoffrey
08/14/2022, 8:54 AM