Unit 1 · Lesson 615 minAcademic review pending

One name, several jobs

Java lets several methods share one name if their parameters differ — the compiler picks the right one by matching arguments, the same instinct C used to match printf's format specifiers to its arguments.

Choose explanation

After this lesson

You should be able to

  • Write two overloaded methods and explain how Java picks between them.
  • Explain why Java passes object references by value, not the objects themselves.
  • Trace a recursive Java method the same way you traced one in C.
01

Overloading: the compiler reads your arguments, not just your name

C required every function to have a unique name — you could not have two functions both called area, one for a circle and one for a rectangle. Java allows exactly that, as long as the parameter lists are different. This is method overloading: several methods sharing a name, distinguished only by the number, order, or type of their parameters.

The compiler resolves which version to call at compile time, by matching the arguments in your call against each overload's parameter list. Call area(5) and it matches the version taking one int; call area(5, 10) and it matches the version taking two. Get this wrong — call with arguments that match no overload — and you get a compiler error, not a runtime surprise.

Three methods, one name, matched by argument shape
public class Shapes {
    public static double area(double side) {
        return side * side;              // square
    }

    public static double area(double length, double width) {
        return length * width;           // rectangle
    }

    public static double area(double radius, boolean isCircle) {
        return Math.PI * radius * radius; // circle, kept distinct via a marker param
    }
}
02

Parameter passing: familiar rule, one new kind of variable

For primitives — int, double, char, boolean — Java passes by value, exactly like C: the method receives a copy, and changes inside the method never affect the caller's variable. You already relied on this in CS105ES and it needs no relearning at all.

Objects work differently, but not the way students usually guess. Java still passes by value — but for an object variable, the value being copied is a reference, an address pointing at the object, similar in spirit to a C pointer. The method gets its own copy of that address, so reassigning the parameter inside the method does not change what the caller points at — but calling a method on that reference, like s.setMarks(90), does change the one real object both the caller and the method are pointing at.

Reassigning the reference vs. changing what it points at
static void reassign(Student s) {
    s = new Student(999, "Nobody"); // only the local copy of the reference changes
}

static void modify(Student s) {
    s.setMarks(90);                 // changes the one real object both sides see
}
03

Recursion: the same idea, one line of new syntax

Everything you learned about recursion in CS105ES transfers directly: a Java method can call itself, each call gets its own set of parameters and local variables on its own stack frame, and a base case is required or the recursion never terminates. The only difference is spelling — the method lives inside a class, and you call it the same way, just possibly through an object.

Trace a recursive Java method exactly the way you traced one in C: write down each call with its argument, follow it down to the base case, and follow the return values back up. The factorial you wrote in C and the one below in Java are, line for line, the same algorithm — proof that recursion itself was never a C-specific idea, only the syntax around it was.

The same recursion you already know how to trace
static long factorial(int n) {
    if (n <= 1) {
        return 1;               // base case
    }
    return n * factorial(n - 1); // recursive case
}

Try it yourself

Write two overloaded greet methods: one taking just a name, printing a plain greeting, and one taking a name and a boolean formal flag, printing a more formal greeting when formal is true. Then write a recursive method that sums the digits of a positive integer.

Need a hint?

For digit sum, the base case is a single-digit number, which is its own digit sum. The recursive case peels off the last digit with n % 10 and recurses on the rest with n / 10 — the same % and / combination from CS105ES.

Check the worked solution

The two greet methods are distinguished purely by parameter count, which is enough for the compiler to choose correctly at every call site. digitSum mirrors the structure of factorial exactly: a base case that needs no further recursion, and a recursive case that does a small amount of work (n % 10) and hands off a smaller version of the same problem (n / 10) to itself.

static void greet(String name) {
    System.out.println("Hi " + name);
}

static void greet(String name, boolean formal) {
    if (formal) {
        System.out.println("Good morning, " + name);
    } else {
        greet(name);
    }
}

static int digitSum(int n) {
    if (n < 10) {
        return n;
    }
    return (n % 10) + digitSum(n / 10);
}

Quick check

When a Student object is passed to a method and the method calls s.setMarks(90), why does the change persist after the method returns?

Select an answer to check your thinking.

Why this lesson exists

Syllabus mapping

Overloading methods and constructors, parameter passing, recursion.

Maps to course outcome CO1.