Java Academy · Lesson

Return Values & Overloading

Return statements, early returns, and method overloading by parameter count and type. Includes varargs and overload selection demos.

Lesson 2 of 38 steps

Return Values & Overloading is a free Java Academy lesson on CoddyKit. This is lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, and your progress syncs across the web and the CoddyKit app. The Java Academy course includes 3 lessons in total.

Overview

Goal: Master return statements and method overloading. You will see early returns (guard clauses), overloads by count and by type, and a short varargs demo.

Return & guard

Return sends a value back to the caller and ends the method. Guard clauses (early return) keep code simple by handling special cases first.

public class Main {
  // Double an integer and return the result
  static int doubleIt(int x) {
    return x * 2;
  }

  // Return a label using a simple early-return pattern (guard clause)
  static String parity(int n) {
    if (n % 2 == 0) {
      return "even";   // early return ends the method here
    }
    return "odd";       // otherwise this path executes
  }

  public static void main(String[] args) {
    System.out.println("doubleIt(7) = " + doubleIt(7));
    System.out.println("parity(10) = " + parity(10));
    System.out.println("parity(11) = " + parity(11));
  }
}

Multiple returns

Returning early for invalid input avoids deep nesting. Here, division by zero returns 0 as a simple policy (later you will learn exceptions).

public class Main {
  // Safe integer division with simple validation
  static int safeDiv(int num, int den) {
    if (den == 0) {
      return 0; // simple policy: return 0 if denominator is zero
    }
    return num / den; // normal path
  }

  public static void main(String[] args) {
    System.out.println("safeDiv(8, 2) = " + safeDiv(8, 2));
    System.out.println("safeDiv(5, 0) = " + safeDiv(5, 0));
  }
}

Overload: count

Overloading means same method name, different parameter list. Changing the number of parameters is a valid overload.

public class Main {
  // Overload 1: max of two numbers
  static int max(int a, int b) {
    return (a > b) ? a : b;
  }
  // Overload 2: max of three numbers (different parameter COUNT)
  static int max(int a, int b, int c) {
    int m = (a > b) ? a : b;
    return (m > c) ? m : c;
  }
  public static void main(String[] args) {
    System.out.println("max(3, 9) = " + max(3, 9));
    System.out.println("max(3, 9, 5) = " + max(3, 9, 5));
  }
}

Overload: types

Overloading by type lets you reuse a name for related operations. Mixed numeric arguments promote to the most specific matching signature.

public class Main {
  // Overload for integers
  static int add(int a, int b) {
    return a + b;
  }
  // Overload for doubles (different parameter TYPES)
  static double add(double a, double b) {
    return a + b;
  }
  public static void main(String[] args) {
    System.out.println("add(2, 3) = " + add(2, 3));           // calls int version
    System.out.println("add(2.5, 3.5) = " + add(2.5, 3.5));   // calls double version
    System.out.println("add(2, 3.5) = " + add(2, 3.5));       // numeric promotion chooses double version
  }
}

Overload: varargs

Varargs (int...) collects zero or more arguments. Overload resolution prefers an exact non-varargs match when available.

public class Main {
  // Specific overload for two ints
  static int sum(int a, int b) {
    return a + b;
  }
  // Varargs overload for many ints
  static int sum(int... nums) {
    int s = 0;
    for (int i = 0; i < nums.length; i = i + 1) s = s + nums[i];
    return s;
  }
  public static void main(String[] args) {
    // Exact match beats varargs
    System.out.println("sum(4, 5) = " + sum(4, 5));           // picks (int,int)
    System.out.println("sum(1,2,3,4) = " + sum(1, 2, 3, 4));  // uses varargs
  }
}

Overload check

Quick check: Which pair can be valid overloads in one class?

Recap & next

Recap: You used return and early returns, created valid overloads by count and type, and practiced varargs. Next: Scope & Lifetime of variables and methods.

Free to start

Learn Java with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
104
Lessons
374

Frequently Asked Questions

Is the “Return Values & Overloading” lesson free?

Yes — the full text of “Return Values & Overloading” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO. The Java Academy course includes 3 lessons in total.

What will I learn in “Return Values & Overloading”?

Return statements, early returns, and method overloading by parameter count and type. Includes varargs and overload selection demos. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.

How long does the “Return Values & Overloading” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining Methods & Parameters
  2. Return Values & Overloading
  3. Scope & Lifetime
← Back to Java Academy