Skip to main content
United StatesComputer ScienceSyllabus dot point

How does a non-void method return a value, and how do you use that returned value?

Topic 2.5 Calling a Non-void Method: call a method that returns a value, and use the returned value by storing it, printing it, or including it in an expression.

A focused answer to AP CSA Topic 2.5, covering methods that return a value, the return type, using a returned value in an assignment, expression or print, the difference from a void method, and chaining method calls, with a worked trace.

Generated by Claude Opus 4.89 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. Methods that return a value
  3. Using the returned value
  4. Method calls as expressions
  5. Try this

What this topic is asking

The College Board (Topic 2.5) wants you to call a method that returns a value (a non-void method) and to use the returned value: store it in a variable, print it, or include it in a larger expression. The method's return type tells you what kind of value comes back, and you must use it in a context that expects that type.

Methods that return a value

Because the call evaluates to a value, you write it wherever a value of that type is expected. For a method double getArea(), the call s.getArea() produces a double that you can use immediately.

Using the returned value

This is the most important difference from a void method (Topic 2.3). A void call is a statement; a non-void call is an expression that has a value. Ignoring a non-void return value is legal but usually wasteful - the whole point of the method is the value it gives you.

Method calls as expressions

Because a non-void call evaluates to its returned value, Java first runs the method, then substitutes the result where the call appeared, and continues evaluating. So in:

double total = box.getWidth() * box.getHeight();

Java calls getWidth(), gets (say) 4.0, calls getHeight(), gets (say) 9.0, then computes 4.0 * 9.0 = 36.0 and stores it. You can also chain calls: if a method returns an object, you can call a method on that returned object, as in s.substring(1).length() (the substring returns a String, on which length is then called).

Try this

Q1. A method header is boolean isEmpty(). Write a statement that stores the returned value in a boolean variable empty for an object list. [1 point]

  • Cue. boolean empty = list.isEmpty();

Q2. Explain the difference between a void method and a non-void method in terms of how their calls are used. [2 points]

  • Cue. A void method's call is a statement and returns no value to use; a non-void method's call evaluates to its returned value, so it can be stored, printed or used in an expression.

Exam-style practice questions

Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AP 2021 (style)1 marksMultiple choice. A class `Square` has a method `double getArea()` that returns the area. Given `Square s = new Square();`, which use of the returned value is NOT valid? (A) `double a = s.getArea();` (B) `System.out.println(s.getArea());` (C) `double total = s.getArea() + 5.0;` (D) `s.getArea();` (E) `getArea() = 10.0;`
Show worked answer →

The answer is (E).

A returned value can be stored (A), printed (B), or used in an expression (C). It is even legal to call a non-void method and ignore its return value (D), though that is usually pointless. (E) is invalid: a method call is not a variable, so you cannot assign to it, and it omits the object reference. The question asks which is NOT valid, so (E) is the answer.

Markers reward knowing the valid uses of a returned value and that a method call cannot be the target of an assignment.

AP 2020 (style)4 marksFree response (code writing). A class `Thermometer` has a method `double getCelsius()` that returns the temperature in degrees Celsius. Write a code segment that creates a `Thermometer` object `t`, gets its Celsius reading, converts it to Fahrenheit using the formula Fahrenheit equals Celsius times 9.0 divided by 5.0 plus 32, stores the result in a `double` variable `fahrenheit`, and prints it.
Show worked answer →

A 4-point question testing use of a returned value in a calculation.

Thermometer t = new Thermometer();
double celsius = t.getCelsius();
double fahrenheit = celsius * 9.0 / 5.0 + 32;
System.out.println(fahrenheit);

Point 1: creating the object. Point 2: calling t.getCelsius() and storing the returned double. Point 3: the conversion using double arithmetic (the 9.0 and 5.0 keep it floating-point). Point 4: storing and printing the result. Using 9 / 5 (integer division giving 1) instead of 9.0 / 5.0 would be a common error.

Related dot points

Sources & how we know this