when a Kotllin class has a (non-primitive) property, for example a
List<Foo>
, is it always passed by copy? if so, is there a way to make it hold a reference?
g
gildor
01/26/2023, 6:13 AM
In Java and Kotlin it’s never passed by copy for objects (to be pedantic Java always pass-by-value, but in case of objects it’s always passes pointer to the object, not a copy of the object)
gildor
01/26/2023, 6:17 AM
So on practice it means that if you pass an object, it will copy pointer on it, and as result:
1. Mutation of the object changes original object (because there is only 1 instance of it inside and outside of the function)
2. Re-assignment of original variable (so setting different pointer to it) will not change pointer passed to the function, it will continue referencing original object
gildor
01/26/2023, 6:19 AM
And there are no operators to change it, there is no pass-by-reference in Java and in Kotlin (even in Native it represented as a wrapper on top of pointer, not as real passing by reference)