Many programmers think that objects are passed by reference. This isn't the case, and this video helps you understand why this is significant.
- [Instructor] Passing variables into methods by value can sometimes however be confusing. And that's because when you call a method and you pass in an object as the parameter rather than a primitive data type like we see here then the variable that contains the reference to the object is what is passed. To state this more fully, when an object is passed into a method as a parameter, a copy of the variable on the stack containing the reference to the object is passed.
And it's this variable on the stack that is passed by value. It means that a copy of the pointer to the object is created, the object itself is not copied and in fact it's not the object that's passed into a method but rather a pointer to the object. For example, if we pass a copy of a list into a method, then the parameter value will be a copy of the myList variable which is a pointer to the object on the heap.
So I hope that's clear, many programmers misinterpret this as objects are passed by reference. But they're not, and let's look at an example of why this is significant. Here's some code which creates a customer and then calls a method which changes the customer's name and prints out the customer's name at the end. Assume that the customer class has been well written and it has get and set methods as you'd expect. What would the outputs of this be? I suggest you pause the video and work through this example using the stack and heap pictures that I've been showing you to work out what would happen.
When you've had a think about it press resume on the video and I'll talk it through. So let's talk through this example. First of all, we have a line that creates the new customer object. The constructor of the customer takes a string so the first thing that will happen is a string will be created on the heap, then the customer will be created with a property that references the string object and a variable will be created on the stack pointing to this customer object.
When we call rename customer because we passed the value of the customer by value, a new variable will be created on the stack called cust which will be given a copy of the pointer to the customer on the heap. Now we change the name of the customer. I hope you remember that strings are immutable in Java, that is you can't change the value of a string if you do this you actually create a new string object. So what happens when this line runs is that a new string object is created on the heap and Java changes the pointer from the name in the customer object to this new string.
The original string object is no longer referenced from anywhere and so it can be garbage collected at some point in the future. Finally, we exit the rename customer method and go to the print line. And now our C variable references the amended object customer which has a name pointing to the new string Diane so the output should be the name Diane. I hope you were able to predict this outcome, if you were then well done as this means that you now do understand the mechanism of how variables are passed between methods.
- How memory works in Java
- Passing variables by value
- How objects are passed
- What are escaping references?
- How to avoid escaping references with collections and custom objects
- Garbage collection and generation sizes
- Detecting soft leaks
- Choosing a garbage collector
- Tuning a virtual machine
- Fixing a memory leak