Join Peggy Fisher for an in-depth discussion in this video Using call by value and call by reference, part of Learning Java 8.
- Most methods include parameters as a way to pass values from the calling method. There are two types of parameters: call by reference and call by value. The biggest difference is that the call by reference allows the method and the calling program to both point to the same address in memory that contains the value of the variable. So, changes made in method are reflected back in the calling program. The call by value makes a copy of the variable contents so any changes made in the method are not reflected back in the calling program.
Let's look at a program that includes both, call by value and call by reference. This program computes the total meal plan cost for a semester. The main method calls four methods. getTotalMealWeeks, getDailyMealCost, compute the MealPlanCost, and finally print the TotalMealPlan cost. Let's take a look at each method. I'm gonna scroll down you'll see the first method, getTotalMealWeeks does not take anything in the parentheses.
There's no parameters. But it is going to return the value we see from the user when we ask them "How many weeks are in your semester?" We'll read it in the total weeks and return that value. That's why I have int here after the word static. The next method is a void method, getDailyMealCost. In the parentheses, we have a parameter double dailyCost). This is an array. Arrays are always a pass by reference or a call by reference.
That means what I'm doing is I'm giving this method the address of where the dailyCost array exists in memory. In my method, I run through and ask the user for the estimated cost for each day and I store that value in the dailyCost array. I don't have to return the dailyCost array because when I update it it's automatically reflected back in the main program because it was call by reference. The computeMealPlanCost method has both parameters and a return type.
The return type is a double. We're gonna return the total cost for the semester. The parameters include both a call by value, int numWeeks, which makes a copy of the value numWeeks so that if I change it in my method it does not get changed back in the calling program and it also takes a call by reference, in which is our array, double  dailyCost. In this method, I'm not changing either one. I'm just calculating the total cost for the semester and then I return the total cost.
One other quick note, in the for loop I don't know if you had noticed but I have for(int) i = 0, the control portion of the for loop says i < dailyCost.length. This makes my program much more flexible because it calculates the size of the array without having a preset value there. Okay, the last method is printTotalMealPlanCost. It is a void method so it does not return anything but it does accept one call by value parameter: double total.
It uses that value to print out the total estimated meal plan for the semester. Okay, let's run this and test it out. Usually semesters run about fifteen weeks. You can see I printed out the array before calling the method and everything is 0. Now, I'm gonna enter the estimated cost for day 1 just to keep it simple. I'll do 10 dollars for day one. 20 dollars for day 2. 30, 40, 50, 60, and day 7 I'll make 70 dollars.
Now, after calling the method you can see the array now has the values that I entered for days 1 through 7. It also printed out my estimated total meal plan cost for the semester of $4,200. An easy rule of thumb is to remember that primitive data types are considered call by value and therefore are a copy of the variable and variables such as arrays and objects which we will talk about shortly are call by reference.
- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs