Skip to main content

Section 6.10 Calling Methods that Return Values

If a method is a void method and has void as its return type, like most of the methods we have seen so far, that means that it does not return anything. But some methods return a value back that the program can use.
When you use a method that returns a value, you need to save what it returns in a variable or use the value in some way for example by printing it out. The data type of the variable must match the data type of the return value of the method. You can find out the return type of a method in its documentation. It will be right before the method name, for example int[] getPosition() means getPosition will return an int array (an array of integer numbers).

Note 6.10.1.

A common error is forgetting to do something with the value returned from a method. When you call a method that returns a value, you should do something with that value like saving it into a variable or printing it out.

Subsection 6.10.1 Methods with Arguments and Return Values

Methods that take arguments and return values are like mathematical functions. Given some input, they return a value. For example, a square(x) method would take an argument x and return its square by multiplying it by itself.
Figure 6.10.2. Figure 1: Method that takes arguments and returns a value
You should be able to trace through method calls like below. Notice that the return statement in a method returns the value that is indicated in the return type back to the calling method. The calling method must save or use or print that value.

Subsection 6.10.2 Summary

  • Some methods return values.
  • To use the return value when calling a method, it must be stored in a variable or used as part of an expression. The variable data type must match the return type of the method.

Subsection 6.10.3 Practice

Checkpoint 6.10.3.

What does the following code print out?
public class MethodTrace
{
  public int square(int x)
  {
      return x*x;
  }
  public int divide(int x, int y)
  {
        return x/y;
  }
  public static void main(String[] args) {
      MethodTrace traceObj = new MethodTrace();
      System.out.println( traceObj.square(2) + traceObj.divide(6,2) );
  }
 }
  • 5
  • Make sure you call both methods and compute the square of 2 and then add the results.
  • 7
  • Yes, square(2) returns 4 which is added to divide(6,2) which returns 3. The total of 4 + 3 is 7.
  • 4 3
  • Make sure you add the results before printing it out.
  • 2 3
  • Make sure you square(2) and add the results before printint it out.
  • Does not compile.
  • Try the code in an active code window.
Try this visualization to see this code in action.

Checkpoint 6.10.4.

Consider the following method.
public double calculatePizzaBoxes(int numOfPeople, double slicesPerBox)
{ /*implementation not shown */}
Which of the following lines of code, if located in a method in the same class as calculatePizzaBoxes, will compile without an error?
  • int result = calculatePizzaBoxes(45, 9.0);
  • The method calculatePizzaBoxes returns a double value that cannot be saved into an int variable.
  • double result = calculatePizzaBoxes(45.0, 9.0);
  • The method calculatePizzaBoxes has an int parameter that cannot hold a double value 45.0.
  • int result = calculatePizzaBoxes(45.0, 9);
  • The method calculatePizzaBoxes has an int parameter that cannot hold a double value 45.0. Note that the int 9 can be passed into a double parameter.
  • double result = calculatePizzaBoxes(45, 9.0);
  • The method calculatePizzaBoxes has an int and a double parameter and returns a double result.
  • result = calculatePizzaBoxes(45, 9);
  • The variable result has not been declared (with an appropriate data type).

Checkpoint 6.10.5.

Consider the following class definition.
public class Liquid
{
    private double boilingPoint;
    private double freezingPoint;
    private double currentTemp;

    public Liquid()
    {
        currentTemp = 50;
    }

    public void lowerTemp()
    {
        currentTemp -= 10;
    }

    public double getTemp()
    {
        return currentTemp;
    }
}
Assume that the following code segment appears in a class other than Liquid.
Liquid water = new Liquid();
water.lowerTemp();
System.out.println(water.getTemp());
What is printed as a result of executing the code segment?
  • -10
  • The Liquid() constructor sets the currentTemp instance variable to 50 and the lowerTemp() method subtracts 10 from it.
  • 50
  • The Liquid() constructor sets the currentTemp instance variable to 50 and the lowerTemp() method subtracts 10 from it.
  • water.getTemp()
  • The System.out.println will print the value returned from water.getTemp().
  • The code will not compile.
  • This code should compile.
  • 40.0
  • Correct, the Liquid() constructor sets the currentTemp instance variable to 50 and the lowerTemp() method subtracts 10 from it, and getTemp() returns the currentTemp value as a double.
You have attempted of activities on this page.