Linked Questions
14 questions linked to/from Is Kotlin "pass-by-value" or "pass-by-reference"?
3 votes
1 answer
9k views
Kotlin value vs. reference in assignment [duplicate]
I would need to have one class reference another. Not create a copy of it but have an actual reference to an already existing instance. I might be able to explain it better with an example. Were we ...
7743 votes
87 answers
2.8m views
Is Java "pass-by-reference" or "pass-by-value"?
I always thought Java uses pass-by-reference. However, I read a blog post which claims that Java uses pass-by-value. I don't think I understand the distinction the author is making. What is the ...
12 votes
4 answers
14k views
Pass an Integer by Reference in Kotlin
I am trying to create a swap function which takes in two parameters as shown below: fun swap(a :Int, b:Int) { } I call it like this: var a = 10 var b = 5 swap(a,b) // a should be ...
4 votes
3 answers
13k views
How to set the value of property in Kotlin
I have tried to set a property value as in the following snippet.This SO question is not answering the question. var person = Person("john", 24) //sample_text.text = person.getName() + ...
5 votes
3 answers
6k views
How to pass a variable from main function to another function and modify it? (Kotlin)
I am aware that an argument passed to a function will be seen as "val", even if the variable was initialized as "var". But this has been a problem for me. In this example code ...
1 vote
3 answers
1k views
Kotlin is not able to change var using a method
Could someone explain why can't I change the value of var in that case ? fun main(args: Array<String>) { var number = 3 changeNumber(number) } fun changeNumber(number: Int) { number ...
0 votes
4 answers
1k views
How to change val to var in function kotlin
I'm new to kotlin. I've got a problem with making a function that changes the value of the boolean. fun main () { var x = false functionWithBoolean(variable = x) } fun functionWithBoolean(...
3 votes
1 answer
1k views
Does Kotlin support string/array slices by reference like Rust and Swift?
In Rust and Swift you can create a slice of either/both arrays and strings which doesn't make a new copy of the elements but gives you a view into the existing elements using a range or iterator and ...
0 votes
1 answer
672 views
In Kotlin, if an object is passed into a new instance of a class and then some properties are changed, will it change in the original object?
The question is in the title: In Kotlin, if an object is passed into a new instance of a class and then some properties are changed, will it change the properties in the original object? Image I've ...
0 votes
1 answer
598 views
Why this Kotlin/Android code throws java.lang.ArrayIndexOutOfBoundsException?
var i = 0 arrayOf<CheckBox>(binding.chkBackup, binding.chkBackupEnc, binding.chkDrive, binding.chkDriveEnc, binding.chkMp3).forEach { it.setOnClickListener { chk -> ...
1 vote
1 answer
306 views
apply function does not work for basic types in kotlin
I am trying to use apply function for assign a new value to a variable. when i try apply() to re-assign a value to a parameter of a data class it works correctly. but when i have not a data class it ...
0 votes
1 answer
103 views
Does Kotlin allow for `optionally` passing by reference like the & in php?
In PHP you can optionally make parameters pass by reference with an & in the function definition: someFunction($byValue, &$byReference) { // $byValue is passed in by value // $...
1 vote
1 answer
91 views
How to pass a variable to a function in Kotlin? [duplicate]
I'm new to Kotlin and I don't know where I went wrong with this. fun swap(a:Int,b:Int):Unit{ a=a xor b b=b xor a a=a xor b } fun main() { var a =10 var b = 1000 // swap(a, b) ...
0 votes
1 answer
99 views
How to put a reference to different variables/class properties as method parameter instead of a value?
I need to create multiple editTexts with logic to handle the format of the input. The different editTexts will bind to different variables and save to different properties of viewModel.home. The ...