2 · Control Flow & Methods · lesson 10 of 41

Methods

Parameters, return types, and overloading.

Java
public static int max(int a, int b) {
    return a > b ? a : b;
}

// Overloading — same name, different parameters
public static double max(double a, double b) { ... }

// Varargs
public static int sum(int... nums) {
    int total = 0;
    for (int n : nums) total += n;
    return total;
}
Key takeaways
  • ✓Overloading picks the best-matching signature at compile time.
  • ✓Varargs (int... nums) is really an array under the hood.