Objects passed by parameter | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Objects passed by parameter

If a object is passed by parameter, does the method work with the real object or with a copy of the object. I have a data-object where data is added every 100ms. I like to save the data of this every 5 seconds. If I pass the dataobject like this. SaveData(dataobject) Do values added in the main thread affect the object in the SaveData method ?

19th Nov 2019, 8:14 AM
sneeze
sneeze - avatar
3 Answers
+ 1
Variables are just addresses in your memory. Every thread - when they want to use this variable - refers to this address. So it doesn't matter what thread you're on, data will be changed.
20th Nov 2019, 9:30 AM
Jakub Stasiak
Jakub Stasiak - avatar
+ 3
If you pass complex type to method you use original value. If you pass primitive type you pass copy of it.
19th Nov 2019, 8:17 AM
Jakub Stasiak
Jakub Stasiak - avatar
0
Yes, I understand that. But I like to know. If data is added to a object in the main-thread. Does this affect the object that is passed as parameter. My measuring points are added 10 every second. I like to make a back-up every 5 seconds. After the save method is called, there are easily 10 measuring points added to the object before it is finally saved to a file. Are those data-points saved or Are those only in the main thread ?
20th Nov 2019, 9:02 AM
sneeze
sneeze - avatar