Verified for the 2025 AP Computer Science A exam•Citation:
In Java programming, methods are fundamental building blocks that perform specific tasks within a program. There are two primary types of methods: void methods and non-void methods. While void methods perform actions without returning any values, non-void methods complete their tasks and then return a value to whatever called them. This study guide focuses on non-void methods: what they are, how they work, and most importantly, how to properly call and use them in your programs. Understanding non-void methods is essential as they allow you to retrieve and use calculated values, access object properties, and build more complex expressions in your code.
int
, double
, boolean
, String
, or any object typeVoid Methods | Non-Void Methods |
---|---|
Declared with void keyword | Declared with a specific return type |
Do not return values | Must return a value of the specified type |
Called as standalone statements | Usually called as part of expressions or with assignment |
Used for performing actions | Used for computing and retrieving values |
Example: System.out.println() | Example: Math.sqrt() |
Public: an access modifier in Java that indicates unrestricted visibility for classes, methods, and variables. Public members can be accessed from any other class or package.
public int calculateTotal(double price, int quantity) { double total = price * quantity; return (int)total; }
Access modifier (public
) - Controls visibility
Return type (int
) - Specifies what type of value will be returned
Method name (calculateTotal
) - Identifies the method
Parameters (double price, int quantity
) - Input values the method needs
Return statement (return (int)total;
) - Sends a value back to the caller
To call a non-void method, you need to:
Store in a variable:
double radius = 5.0; double area = Math.PI * Math.pow(radius, 2);
Use in an expression:
double totalCost = calculateSubtotal() + calculateTax();
Use as a parameter in another method:
System.out.println(student.getName());
Use in a conditional statement:
if (number.isPrime()) { System.out.println("This is a prime number!"); }
length()
- Returns the number of characters in a string
substring(int beginIndex, int endIndex)
- Returns a new string that is a substring
indexOf(String str)
- Returns the index of the first occurrence of a substring
equals(Object obj)
- Returns boolean indicating if two strings have the same value
Math.sqrt(double a)
- Returns the square root of a value
Math.pow(double a, double b)
- Returns a raised to the power of b
Math.random()
- Returns a random value between 0.0 (inclusive) and 1.0 (exclusive)
Math.abs(int a)
- Returns the absolute value of a number
The return value must be used in some way - stored in a variable, used in expression, etc.
The variable type must match (or be compatible with) the method's return type
When a non-void method is called, it temporarily gets replaced by its return value
Consider a Student
class with various non-void methods:
public class Student { private String name; private int[] testScores; // Constructor and other methods omitted for brevity public String getName() { return name; } public double getAverageScore() { int sum = 0; for (int score : testScores) { sum += score; } return (double) sum / testScores.length; } public boolean isPassing() { return getAverageScore() >= 60.0; } }
Here's how to work with these methods:
public class StudentTester { public static void main(String[] args) { Student student1 = new Student("Alex", new int[]{85, 92, 78}); // Store return value in a variable String studentName = student1.getName(); System.out.println("Student name: " + studentName); // Use return value in an expression System.out.println("Average score: " + student1.getAverageScore()); // Use return value in a conditional if (student1.isPassing()) { System.out.println(studentName + " is passing the course!"); } else { System.out.println(studentName + " needs to improve their scores."); } // Combining multiple non-void method calls double curvedAverage = student1.getAverageScore() + 5.0; System.out.println("Curved average: " + curvedAverage); } }
In many cases, you can chain multiple non-void method calls together when each method returns an object:
// Getting the first 3 characters of a student name and converting to uppercase String nameInitials = student1.getName().substring(0, 3).toUpperCase(); // Equivalent to: String name = student1.getName(); String initials = name.substring(0, 3); String upperInitials = initials.toUpperCase();
Non-void methods are essential tools in Java programming that allow you to retrieve values from objects or classes. Unlike void methods which only perform actions, non-void methods return data that can be stored, manipulated, or used in other operations. When calling non-void methods, always remember to capture or use their return values - this is what makes them different from void methods. As you continue with your AP Computer Science A course, you'll find that non-void methods become increasingly important for building complex and efficient programs.