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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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
- Topic 2.3 Calling a Void Method: call a non-static void method on an object using dot notation, and explain that a void method performs an action but returns no value.
A focused answer to AP CSA Topic 2.3, covering dot notation for calling methods on objects, what void means, why a void call cannot be used in an expression, the difference between a method signature and a call, and method side effects, with a worked example.
- Topic 2.4 Calling a Void Method with Parameters: call a void method that takes parameters, passing arguments that match the parameter list in number, order and type, and explain how arguments are copied to parameters.
A focused answer to AP CSA Topic 2.4, covering parameters versus arguments, matching the argument list in number, order and type, pass-by-value for primitives, overloaded methods, and how the call supplies data to the method, with a worked trace.
- Topic 2.7 String Methods: call the String methods in the AP Java subset (length, substring, indexOf, equals, compareTo), respecting zero-based indexing and the immutability of String objects.
A focused answer to AP CSA Topic 2.7, covering the required String methods length, substring (both forms), indexOf, equals and compareTo, zero-based indexing, the half-open range of substring, why == differs from equals, and String immutability, with a worked trace.
- Topic 2.9 Using the Math Class: call the static Math methods in the AP Java subset (abs, pow, sqrt, random) and generate a random integer or double in a specified range.
A focused answer to AP CSA Topic 2.9, covering the required static Math methods abs, pow, sqrt and random, why Math methods are called on the class, the return types, and the standard formula for generating a random int in a range, with a worked trace.
- Topic 1.3 Expressions and Assignment Statements: evaluate arithmetic expressions using operator precedence, integer division and the modulo operator, and assign results to variables.
A focused answer to AP CSA Topic 1.3, covering arithmetic operators, operator precedence, integer division truncation, the modulo operator, and how assignment statements store results, with a fully traced worked evaluation.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)