Loading published curriculum…

01 · Java foundations / LESSON 3 OF 102

Methods, arrays, and debugging

Separate computation from input and output.

Before this lesson: Types, conditions, and loops

Understand the topic

Methods define parameters, a return type, and behavior. Java passes values, including copies of object references. A method can mutate a referenced object but cannot replace the caller variable by reassigning its parameter.

Arrays have fixed length and zero-based indices. Define empty-input behavior explicitly. Set an IDE breakpoint inside the loop, inspect local variables, and step through each addition.

EXAMPLE · JAVA

Main.java

public class Main {
    static int sum(int[] values) {
        int total = 0;
        for (int value : values) total += value;
        return total;
    }
    public static void main(String[] args) {
        System.out.println(sum(new int[]{2, 4, 6}));
        System.out.println(sum(new int[]{}));
    }
}

What to expect

12, then 0.

YOUR TURN

Put it into practice

  1. 01Write max for a nonempty array.
  2. 02Test negative-only and single-element arrays.
  3. 03Inspect each iteration with a debugger.

Pass a quiz or complete a lab to check today’s streak. Each lesson counts once per day, including when you review it later.

UNDER THE HOOD

Build the mental model

Treat a method as a contract: inputs it accepts, output it promises, and side effects it may perform.

  1. 01A copied reference still points at the same object, so changing an array element is visible to the caller. Reassigning the parameter changes only the local reference.
  2. 02The stack holds active call frames. Each call has its own local variables; recursion adds frames until it reaches a base case or exhausts stack space.
  3. 03Return values make computation testable. Printing is a side effect and should usually happen at the outer edge of a small program.

A mistake worth avoiding

Do not use null to mean both no result and invalid input without documenting which is intended.

DEBUGGING ROOM

Something broke. Where do you look?

A method assigns a new array to its parameter but the caller still sees the old array.

Before revealing the diagnosis, write down your first investigation step.

CHECK YOUR UNDERSTANDING

A method changes values[0] through an array parameter. Does the caller see it?

Choose one answer

BUILD IT YOURSELF

Write a small statistics library

  1. 1.Implement sum, average, and max as separate methods.
  2. 2.Document null and empty-input behavior.
  3. 3.Keep console interaction in main.

Acceptance checklist

Lab completion is your self-report. Knowledge checks do not certify project quality.

RETRIEVE IT FROM MEMORY

How is mutation different from reassigning a parameter?

Revisit this lesson in three days after a successful check or lab.

Ready for the next step?

Write a method with a clear contract and boundary behavior.

Reading is separate from passing a knowledge check or completing a lab.

Go deeper · official references