Calling a Non-Void Method

Emily Wilson
7 min read
Study Guide Overview
This study guide covers non-void methods in Java. It explains the core concept of returning values, method header structure (including data types, method names, and parameters), and returning various data types (integers, doubles, booleans, strings, and objects). It also highlights common mistakes, provides quick facts and exam tips, and includes practice multiple-choice and free-response questions covering topics like string manipulation and palindromes.
#AP Computer Science A: Non-Void Methods - The Night Before
Hey there! Let's get you feeling confident about non-void methods. Think of this as your quick-reference guide, designed to make everything click right before the exam. We're going to make sure you know exactly what to expect and how to tackle it.
#Understanding Non-Void Methods: The Core Concept
Non-void methods are all about getting a value back. Unlike void
methods that just do something, non-void methods calculate or retrieve a value and then return it for use elsewhere in your program. This returned value can be a primitive type (like int
, double
, boolean
) or a reference type (like String
or other objects).
#Method Header Structure
java
public (static) dataType methodName(parameterListOptional)
public
: Access modifier (we'll dive deeper into these later).static
: Optional keyword; indicates the method belongs to the class itself, not an object (more on this in Unit 5).dataType
: Crucial! This is the type of value the method will return. It must match the type of the returned expression.methodName
: How you'll call the method.parameterListOptional
: Input values the method needs to do its job (optional).
Remember: The return
type in the method header must match the type of the value being returned by the method.
#Returning Different Data Types
#Returning Integers/Doubles
These methods are your math wizards. They perform calculations and return the result. The return
statement is key here. It stops the method's execution and sends the calculated value back to the caller.
java
public static double rectanglePerimeter(double length, double width) {
return 2 * (length + width); // Returns a double
// Any code after this return statement will be unreachable!
}
Always double-check that your return type matches the type of the expression you're returning. A mismatch will cause a compile error!
#Returning Booleans
Boolean methods are your condition checkers. They evaluate a boolean expression and return true
or false
. The naming convention isCondition()
...

How are we doing?
Give us your feedback and let us know how we can improve