Calling Methods: Parameters and Return Values

AP Computer Science A · Unit 1 — Using Objects and Methods

Arguments are copied in; results come back only through the return value

A method call has three moving parts: the object (or class) you call it on, the arguments you pass, and the value it returns. A method whose header says void returns nothing, so writing double r = box.print(); is a compile-time error, not a run-time surprise.

public static int doubled(int n) {
    n = n * 2;
    return n;
}

Java passes every argument by value. For a primitive that means the method receives a copy of the number, so changing n inside doubled leaves the caller's variable untouched. For an object the copied thing is the reference: the method cannot make your variable point somewhere else, but it can call mutator methods on the object you both share, and those changes are visible to the caller.

The other half of the rule is that a returned value is thrown away unless you store it or use it. doubled(score); on a line by itself compiles and runs, computes the answer, and discards it. You must write score = doubled(score);. Static methods are called on the class (Math.abs(-4)), instance methods on an object (word.length()); mixing the two up is a compile-time error.

Caller: int score = 5; 5 chép doubled: n = n * 2; 10 score is still 5 — the two number cells are different cells Counter c → object bump(c) copies the ARROW c.increment() CAN change it
Read the picture in two layers. The top layer is the primitive type: the two number cells are separate, so no matter what a method does to its parameter, it cannot reach the original variable — to get a result you must return and then assign it back. The bottom layer is the object type: the arrow is copied, but there is only one rectangle at the end of the arrow, so when a method calls a mutator on it, the caller sees it immediately. It is the same “pass by value” rule, differing only in whether what gets copied is a number or an arrow.

Worked example: same rule, two different results

What does the following code print? Assume Counter is the class from Lesson 1.

public static int grow(int n) { n = n + 10; return n; }
public static void tick(Counter c) { c.increment(); }

int score = 5;
grow(score);
score = grow(score);
Counter k = new Counter();
tick(k);
tick(k);
System.out.println(score + " " + k.getCount());

Solution

The line grow(score); — the method runs and computes 15, then the return value is not stored anywhere and is thrown away. score is still 5. This is a line the question puts in deliberately to separate careful readers from skimmers.

The line score = grow(score); — this time the result 15 is assigned back, so score becomes 15.

The two lines tick(k); — k is a class type, so the method receives a copy of the arrow, and that arrow still points to the caller's own object. Each call increases count by 1, making it 2 after two calls.

Printed result: 15 2.

The easy place to slip is to think both cases must behave the same. They differ because grow reassigns the parameter itself (which cannot reach outside), while tick calls a mutator on the shared object (which can reach outside).

Common mistake

Calling a value-returning method and forgetting to capture the result — word.toUpperCase();, grow(score);, Math.abs(x); standing alone. The compiler does not say a word, the program runs normally, it is just that nothing changes. How to prevent it: whenever the method name is not void, the line calling it must have an = at the start, or must sit inside another expression (printed, added, used as an argument).

Key takeaway

Primitive types: the number is copied, and a method cannot change the original variable. Object types: the arrow is copied, and a method that calls a mutator can change the object. If a method is not void, you must capture the return value, otherwise the result is thrown away.

← Objects, References, and Constructors · All AP Computer Science A lessons · Practice AP Computer Science A questions (free, instant scoring) · The String Class and Immutability →