Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
Join author David Gassner as he explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more. This course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts.
One of the questions you have to answer when you learn a new programming language is whether arguments passed into function are being passed by copy or by reference? Let's define these terms, when you pass arguments by copy that means that you're passing a variable in as an argument to the function or method. And within the function there is a new copy of that value, that is the value outside the function and the value inside the function are separated and any changes you make inside the function, won't be reflected in the original value.
When you pass by reference, the variable outside the function and the variable inside the function are the same variable, and any changes that you make inside the function would be reflected in the original value, once the function is completed. In Java you always are passing arguments by copy; that is you're always creating a new instance of the value inside the function. But there are certain behaviors that can make you think you're passing by reference. I'm going to show you three scenarios and explain how each of them proves the point that arguments are passed by copy.
Let's take the simplest scenario, passing primitive values. When you pass a primitive value into a function, a copy of the original value is made and any changes inside the function won't be reflected in the original. Here is a bit of code, a function named incrementValue, it receives an argument data typed as an int, within the function it increments the value by 1 and outputs the new value. Now when I call the function I am starting off with an original value, an int of 10, and outputting that value and that will clearly output the value of 10.
Then I call increment value and that will obviously output a value of 11, but what will be the final line of code output? Will it be 10 or 11? Well, because we are making a copy inside the function and not referencing the original, you'll see a value of 10 in the final output. That is to say the original value did not change. With primitives it's very easy to see that you're passing parameters by copy. Now you'll hear some Java developers say that when you pass primitives, you're making copies, but when you pass complex objects, you're actually passing by reference.
That's not the case. You're still passing by copy, but references to values inside the complex object are parts of both versions of the variable, both the original and the version inside the function. Let's take look at an example using an array. In this version of the increment value function, I'm receiving an array of integer values. Within the function I'm incrementing the first item in the array by 1 and then I'm outputting that value. In the calling code I am starting off with an array of 3 values 10, 20 and 30, then I am outputting the value of the first item in the array, once again outputting 10.
I'm calling the function and then outputting the original after. This time the behavior will be different than in the first scenario with a simple primitive, the original value is 10, inside the function it's 11, but the original value after is 11. Well, if we were passing by copy, what's going on? Here is a good way to visualize it. The variables inside a complex object are references, but when you pass the complex object to a function, you're making a copy of the original object. But both the original object and the new object are pointing to the same values internally.
A good way to say it is that a reference variable points to a location in memory and when you pass a variable to the function, a new reference is always created, but the original value is still in the same place. Let's once again take this integer array named original. It has three values of 10, 20 and 30. Let's visualize these values as locations in memory. In the original array, item 1 pointed to this location in memory, when we take that array and we pass it to a function, we're making a copy of the array, but the internal references are pointing to the same place.
So our copy of the array inside the function is still pointing to those locations in memory, those values of 10, 20 and 30. So this is why it can look like complex objects are being passed by reference. The complex objects themselves are being copied, but the internal references are being retained. Finally, let's look at a scenario with strings. I've said many times that strings are complex objects, that they contain an array of char or character values. But I've also said that strings are immutable, once you create a string, you can't change its value.
It may appear that you're changing its value, but you are actually creating a new string. Let's see what happens when you pass a string as a parameter. I've created a method named changeString, which accepts a string argument. Within the function I set its value to a string of new, and output that value. When I call the code, I start off with an original string, I output its value, I call the method and I output the original value again. With strings you'll see that you're always passing by copy, and the new copy can't reference the internal characters, because strings are immutable, they can't change once they're declared.
And so you can't reach back and change that original value, the result with a string will look like a primitive. The original will say whatever it did, inside the function it will have that value, but the original value will remain untouched. As long as you remember that in Java you're always passing parameters by copy. But with complex objects other than strings, the internal references can be retained; you'll see that the behavior is completely consistent across the entire language.
There are currently no FAQs about Java Essential Training.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.