Chapter 4 of 16Foundation

Methods and Parameter Passing

Declare methods with parameters and return values, understand pass-by-value, and use recursion only when it clarifies the problem.

25 minJava 21
01

Why it matters

Start from the concept, not the syntax

Meet a concrete situation where this idea earns its place, then state the boundary it guards.

Methods are where behavior lives. Naming a block of logic, giving it inputs and a return value, and calling it from many places is how a program stays readable—and how overloading and recursion become natural tools instead of tricks.

By the end of this chapter

Confusion about pass-by-value produces methods that mutate the caller unexpectedly or lose their updates entirely.

  • Parameters and returns
  • Overloading
  • Recursion base case
02

Mechanism

How Java expresses the concept

Walk the actual types, rules, and runtime behavior involved—the same behavior described in the Java Language Specification.

The mental model

A method signature is its name plus its parameter types. Java passes primitives by value (a copy of the bits) and references by value (a copy of the reference, so the object can still be mutated but reassignment is invisible to the caller).

Method overloading lets you define several methods with the same name but different parameter lists; the compiler picks the most specific match. Varargs (`String... parts`) let a method accept zero or more arguments as an array; only one varargs parameter is allowed and it must be last.

Recursion solves a problem by calling the same method on a smaller input. Every recursive method needs a base case that stops the recursion; without it the stack overflows. Prefer iteration when the problem is naturally sequential, and recursion when the data is naturally tree-like.

Return type is part of the method but not the signature, so two methods cannot differ only by return type. A method that declares a return type must return on every path.

03

Concept map

Inspect the building blocks as a whole

Use the map as a structural reference; it shows the pieces and their relationships, not an execution sequence.

Core Java

Methods, parameters, and recursion

Methods group behavior, accept inputs through parameters, and return a single result. Overloading and varargs let one name serve several shapes.

  1. 01Pass by value
  2. 02Overloading
  3. 03Varargs
  4. 04Base case
  5. 05Recursion vs iteration
  6. 06Single return type, many paths
04

Implementation

Encode the smallest runnable example

Write and run the code, then observe the behavior you just predicted with a real Java program.

Runnable snippet path

Predict, run, and change each example

2 focused examples
01

Overload and use varargs

sum(int...) accepts any number of ints; sum(int, int) is a more specific overload.

Sum.java
public class Sum {
    static int sum(int... numbers) {
        int total = 0;
        for (int n : numbers) total += n;
        return total;
    }
    static int sum(int a, int b) {
        return a + b;
    }
}

Remember

The compiler binds the most specific overload, so sum(1, 2) calls the two-arg version.

Change one input, predict the result, then compile or test it before continuing.

02

Recursion with a base case

Factorial stops at n == 0; each step reduces the problem.

Factorial.java
public class Factorial {
    static long fact(int n) {
        if (n <= 1) return 1; // base case
        return n * fact(n - 1);
    }
}

Remember

Every recursive call must move toward the base case or the stack will overflow.

Change one input, predict the result, then compile or test it before continuing.

05

Watch out

Break it on purpose, then repair the right layer

Predict the symptom before running the failure case, and close with the completion checklist.

Deliberate failure

  1. Break

    Reassign a parameter inside a method and expect the caller to see the change

  2. Observe

    The caller keeps the original value and the update is lost

  3. Repair

    Remember that Java passes primitives and references by value; mutate the object contents or return a new result

Why it matters

Remember that Java passes primitives and references by value; mutate the object contents or return a new result

Completion checklist

  • Explain pass-by-value for primitives and references
  • Overload a method by parameter list
  • Write recursion with a base case
06

Go deeper

Official references

The primary sources behind this chapter. Every claim above stays consistent with them.