Using call by value and call by reference

From the course: Learning Java 8

  • Course details

    Java is one of the top-five programming languages, and is used for websites, embedded controllers, and Android app development. This is an introduction to get you started programming with Java. Peggy Fisher introduces the basics: data types, strings, arrays, expressions, loops, and functions. She'll help you control the flow and logic of your code, and create classes using the principles of object-oriented design. Then go a bit beyond the basics and learn advanced techniques for working with arrays, manipulating files, and building graphical user interfaces (GUIs) that respond to user input. This three-hour capsule course is perfect for developers who need to get up to speed with Java fast, as well as for beginning programmers who want their first taste of this popular language.

    Instructor

    • Click here to view Peggy Fisher’s instructor page

      Peggy Fisher

      Content Strategist, Software Development Languages at Linkedin Learning Solutions with Lynda.com content

      Peggy Fisher is a programmer and content manager at LinkedIn Learning.

      Peggy's main focus is application programming in Java, Arduino, and C++. She has also worked on courses in COBOL and discrete ,athematics. Previously she was a faculty member at Penn State University's College of Information Sciences and Technology. She started out as a programmer working for a large insurance company, but after 18 years she left her job as a director of information technology to pursue her true passion teaching. She earned a master's degree in math education, and went on to teach high school math and computer science in Pennsylvania. In 2012, Peggy accepted a position as an instructional designer at Penn State, and shortly thereafter began teaching Intro to Application Programming with Java. She is a strong supporter of women in STEM. As one of the few female programming teachers, she serves as a mentor to incoming female freshmen who are considering a career in programming. She was also the K–12 outreach coordinator for the college, where she scheduled, ran, and taught summer camps for middle school and high school students. In a PBS NewsHour interview, she expressed that all students should take at least one programming class either in high school or college. Peggy enjoys constantly learning and finding new and exciting ways to bring technology to life in and outside of the classroom, such as using Arduino microcontrollers or Lego Mindstorms, to help make learning hands-on and fun.

    Skills covered in this course

  • Welcome

    - 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.

  • Practice while you learn with exercise files

    Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.

    Download the exercise files for this course. Get started with a free trial today.

  • Download courses and learn on the go

    Watch courses on your mobile device without an internet connection. Download courses using your iOS or Android LinkedIn Learning app.

    Watch this course anytime, anywhere. Get started with a free trial today.

Contents