콘텐츠로 이동

Unit 1.9: Method Signatures

Scope: CS Awesome 2, Section 1.9

Learning Goals

By the end of this lesson, you should be able to:

  1. Explain methods as procedural abstraction.
  2. Trace flow of control through method calls.
  3. Distinguish a header, signature, parameter, and argument.
  4. Match calls to compatible signatures.
  5. Explain call by value and overloading.

Methods and Procedural Abstraction

A method is a named block of code that performs a task when called. Methods break a larger problem into smaller operations and reduce repeated code.

Procedural abstraction lets a caller use a method by understanding its contract without needing to know its internal implementation.

public static void separator()
{
    System.out.println("----------------");
}

The method does nothing until it is called:

separator();

Flow of Control

public class Report
{
    public static void title()
    {
        System.out.println("DAILY REPORT");
    }

    public static void main(String[] args)
    {
        System.out.println("Start");
        title();
        System.out.println("End");
    }
}

Execution begins in main. At title();, control moves into title, executes its body, and then returns to the statement immediately after the call.

Output:

Start
DAILY REPORT
End

Method Header and Signature

public static void repeatLabel(String label, int count)
Part Value
access modifier public
class-method keyword static
return type void
method name repeatLabel
parameter list String label, int count

The method header is the complete declaration line. The method signature consists of the method name and ordered parameter types:

repeatLabel(String, int)

The return type and parameter variable names are not part of the Java signature.

Parameters and Arguments

A parameter is a variable declared in a method header. An argument is the actual value supplied by a call.

public static void showGate(int gate) // gate is a parameter
{
    System.out.println(gate);
}

showGate(14); // 14 is an argument

Arguments must be compatible with parameters in number, order, and type.

public static void showReading(String label, double value)

Valid call:

showReading("Pressure", 101.3);

Invalid calls:

showReading(101.3, "Pressure"); // wrong order
showReading("Pressure");        // missing argument

Call by Value

Java passes arguments using call by value: each parameter receives a copy of the argument's value.

public static void addOne(int number)
{
    number++;
}

int count = 5;
addOne(count);
System.out.println(count); // still 5

Changing the primitive parameter changes only the local copy. Reference values are also copied; later lessons examine what that means for objects.

Overloading

Methods are overloaded when the same class has multiple methods with the same name but different signatures.

public static void display(int value) { /* ... */ }
public static void display(double value) { /* ... */ }
public static void display(String value) { /* ... */ }

Java selects a compatible version from the argument types. Changing only the return type does not create a valid overload because the return type is not part of the signature.

Practice Missions

Mission 1: Label the Header

For the following method header, identify the return type, method name, parameter names, parameter types, and method signature.

public static void recordSample(int sensorId, double reading)

Mission 2: Match Calls to Signatures

Given these overloaded signatures, identify which method each call selects or explain why it is invalid.

log(String)
log(int)
log(String, int)
log("open");
log(7);
log("retry", 2);
log(2, "retry");

Mission 3: Trace the Calls

Predict the exact output and draw arrows showing each transfer of control.

public static void alpha()
{
    System.out.print("A");
    beta();
}

public static void beta()
{
    System.out.print("B");
}

public static void main(String[] args)
{
    beta();
    alpha();
    System.out.println("C");
}

Mission 4: Test Call by Value

Write a method triple(int value) that triples its parameter but returns nothing. Call it with a variable from main, then explain why the caller's variable does not change.

Key Summary

Concept Core idea
Method Named code block executed when called.
Procedural abstraction Use behavior without knowing implementation details.
Parameter Variable declared in a method header.
Argument Value supplied by a method call.
Signature Method name plus ordered parameter types.
Call by value Parameters receive copies of argument values.
Overloading Same method name with different signatures.

Source scope: CS Awesome 2, Unit 1.9