You want print() to work with strings, integers, and doubles. Instead of printString(), printInt(), printDouble(), overloading lets you use one name with multiple parameter types.

Overload for different types

Create methods with the same name but different parameter types.

example
AddOverload.java
Replay: real traced execution (multi-file project)
public class AddOverload {
    public static void main(String[] args) {
        System.out.println("=== Method Overloading: add() ===\n");

        // Integer addition
        int left = 5;
        int right = 3;
        int intResult = add(left, right);
        System.out.println("add(" + left + ", " + right + ") = " + intResult);

        // Double addition
        double doubleResult = add(5.5, 3.2);
        System.out.println("add(5.5, 3.2) = " + doubleResult);

        // Three integers
        int tripleResult = add(10, 20, 30);
        System.out.println("add(10, 20, 30) = " + tripleResult);

        // More types
        long longResult = add(1000000000L, 2000000000L);
        System.out.println("add(1000000000L, 2000000000L) = " + longResult);

        // Compiler chooses the right method!
        System.out.println("\n=== Compiler Decision ===");
        System.out.println("add(1, 2)     → int version");
        System.out.println("add(1.0, 2.0) → double version");
        System.out.println("add(1, 2, 3)  → three-param version");
    }

    // Overload 1: Two integers
    static int add(int a, int b) {
        System.out.println("  [Called: add(int, int)]");
        return a + b;
    }

    // Overload 2: Two doubles
    static double add(double a, double b) {
        System.out.println("  [Called: add(double, double)]");
        return a + b;
    }

    // Overload 3: Three integers
    static int add(int a, int b, int c) {
        System.out.println("  [Called: add(int, int, int)]");
        return a + b + c;
    }

    // Overload 4: Two longs
    static long add(long a, long b) {
        System.out.println("  [Called: add(long, long)]");
        return a + b;
    }
}
public class AddOverload {
    public static void main(String[] args) {
        System.out.println("=== Method Overloading: add() ===\n");

        // Integer addition
        int left = 9;
        int right = 3;
        int intResult = add(left, right);
        System.out.println("add(" + left + ", " + right + ") = " + intResult);

        // Double addition
        double doubleResult = add(5.5, 3.2);
        System.out.println("add(5.5, 3.2) = " + doubleResult);

        // Three integers
        int tripleResult = add(10, 20, 30);
        System.out.println("add(10, 20, 30) = " + tripleResult);

        // More types
        long longResult = add(1000000000L, 2000000000L);
        System.out.println("add(1000000000L, 2000000000L) = " + longResult);

        // Compiler chooses the right method!
        System.out.println("\n=== Compiler Decision ===");
        System.out.println("add(1, 2)     → int version");
        System.out.println("add(1.0, 2.0) → double version");
        System.out.println("add(1, 2, 3)  → three-param version");
    }

    // Overload 1: Two integers
    static int add(int a, int b) {
        System.out.println("  [Called: add(int, int)]");
        return a + b;
    }

    // Overload 2: Two doubles
    static double add(double a, double b) {
        System.out.println("  [Called: add(double, double)]");
        return a + b;
    }

    // Overload 3: Three integers
    static int add(int a, int b, int c) {
        System.out.println("  [Called: add(int, int, int)]");
        return a + b + c;
    }

    // Overload 4: Two longs
    static long add(long a, long b) {
        System.out.println("  [Called: add(long, long)]");
        return a + b;
    }
}
public class AddOverload {
    public static void main(String[] args) {
        System.out.println("=== Method Overloading: add() ===\n");

        // Integer addition
        int left = 12;
        int right = 3;
        int intResult = add(left, right);
        System.out.println("add(" + left + ", " + right + ") = " + intResult);

        // Double addition
        double doubleResult = add(5.5, 3.2);
        System.out.println("add(5.5, 3.2) = " + doubleResult);

        // Three integers
        int tripleResult = add(10, 20, 30);
        System.out.println("add(10, 20, 30) = " + tripleResult);

        // More types
        long longResult = add(1000000000L, 2000000000L);
        System.out.println("add(1000000000L, 2000000000L) = " + longResult);

        // Compiler chooses the right method!
        System.out.println("\n=== Compiler Decision ===");
        System.out.println("add(1, 2)     → int version");
        System.out.println("add(1.0, 2.0) → double version");
        System.out.println("add(1, 2, 3)  → three-param version");
    }

    // Overload 1: Two integers
    static int add(int a, int b) {
        System.out.println("  [Called: add(int, int)]");
        return a + b;
    }

    // Overload 2: Two doubles
    static double add(double a, double b) {
        System.out.println("  [Called: add(double, double)]");
        return a + b;
    }

    // Overload 3: Three integers
    static int add(int a, int b, int c) {
        System.out.println("  [Called: add(int, int, int)]");
        return a + b + c;
    }

    // Overload 4: Two longs
    static long add(long a, long b) {
        System.out.println("  [Called: add(long, long)]");
        return a + b;
    }
}
public class AddOverload {
    public static void main(String[] args) {
        System.out.println("=== Method Overloading: add() ===\n");

        // Integer addition
        int left = 5;
        int right = 4;
        int intResult = add(left, right);
        System.out.println("add(" + left + ", " + right + ") = " + intResult);

        // Double addition
        double doubleResult = add(5.5, 3.2);
        System.out.println("add(5.5, 3.2) = " + doubleResult);

        // Three integers
        int tripleResult = add(10, 20, 30);
        System.out.println("add(10, 20, 30) = " + tripleResult);

        // More types
        long longResult = add(1000000000L, 2000000000L);
        System.out.println("add(1000000000L, 2000000000L) = " + longResult);

        // Compiler chooses the right method!
        System.out.println("\n=== Compiler Decision ===");
        System.out.println("add(1, 2)     → int version");
        System.out.println("add(1.0, 2.0) → double version");
        System.out.println("add(1, 2, 3)  → three-param version");
    }

    // Overload 1: Two integers
    static int add(int a, int b) {
        System.out.println("  [Called: add(int, int)]");
        return a + b;
    }

    // Overload 2: Two doubles
    static double add(double a, double b) {
        System.out.println("  [Called: add(double, double)]");
        return a + b;
    }

    // Overload 3: Three integers
    static int add(int a, int b, int c) {
        System.out.println("  [Called: add(int, int, int)]");
        return a + b + c;
    }

    // Overload 4: Two longs
    static long add(long a, long b) {
        System.out.println("  [Called: add(long, long)]");
        return a + b;
    }
}
public class AddOverload {
    public static void main(String[] args) {
        System.out.println("=== Method Overloading: add() ===\n");

        // Integer addition
        int left = 5;
        int right = 10;
        int intResult = add(left, right);
        System.out.println("add(" + left + ", " + right + ") = " + intResult);

        // Double addition
        double doubleResult = add(5.5, 3.2);
        System.out.println("add(5.5, 3.2) = " + doubleResult);

        // Three integers
        int tripleResult = add(10, 20, 30);
        System.out.println("add(10, 20, 30) = " + tripleResult);

        // More types
        long longResult = add(1000000000L, 2000000000L);
        System.out.println("add(1000000000L, 2000000000L) = " + longResult);

        // Compiler chooses the right method!
        System.out.println("\n=== Compiler Decision ===");
        System.out.println("add(1, 2)     → int version");
        System.out.println("add(1.0, 2.0) → double version");
        System.out.println("add(1, 2, 3)  → three-param version");
    }

    // Overload 1: Two integers
    static int add(int a, int b) {
        System.out.println("  [Called: add(int, int)]");
        return a + b;
    }

    // Overload 2: Two doubles
    static double add(double a, double b) {
        System.out.println("  [Called: add(double, double)]");
        return a + b;
    }

    // Overload 3: Three integers
    static int add(int a, int b, int c) {
        System.out.println("  [Called: add(int, int, int)]");
        return a + b + c;
    }

    // Overload 4: Two longs
    static long add(long a, long b) {
        System.out.println("  [Called: add(long, long)]");
        return a + b;
    }
}
  1. left ← 5, right ← 3

    2public class AddOverload {3    public static void main(String[] args) {4        System.out.println("=== Method Overloading: add() ===\n");5        6        // Integer addition7        int left→ 5 = 5;  //@left=5, 9, 128        int right→ 3 = 3;  //@right=3, 4, 109        int intResult = add(left5, right3);  //?call10        System.out.println("add(" + left + ", " + right + ") = " + intResult);
    output=== Method Overloading: add() ===
  2. static int add(int a, int b)

    31// Overload 1: Two integers  //?overload32static int add(int a5, int b3) {33    System.out.println("  [Called: add(int, int)]");34    return a5 + b3;35}
    output  [Called: add(int, int)]
  3. intResult ← 8

    8int right = 3;  //@right=3, 4, 109int intResult→ 8 = add(left5, right3);  //?call10System.out.println("add(" + left5 + ", " + right3 + ") = " + intResult8);1112// Double addition13double doubleResult = add(5.5, 3.2);14System.out.println("add(5.5, 3.2) = " + doubleResult);
    outputadd(5, 3) = 8
  4. static double add(double a, double b)

    37// Overload 2: Two doubles38static double add(double a5.5, double b3.2) {39    System.out.println("  [Called: add(double, double)]");40    return a5.5 + b3.2;41}
    output  [Called: add(double, double)]
  5. doubleResult ← 8.7

    12// Double addition13double doubleResult→ 8.7 = add(5.5, 3.2);14System.out.println("add(5.5, 3.2) = " + doubleResult8.7);1516// Three integers17int tripleResult = add(10, 20, 30);18System.out.println("add(10, 20, 30) = " + tripleResult);
    outputadd(5.5, 3.2) = 8.7
  6. static int add(int a, int b, int c)

    43// Overload 3: Three integers44static int add(int a10, int b20, int c30) {45    System.out.println("  [Called: add(int, int, int)]");46    return a10 + b20 + c30;47}
    output  [Called: add(int, int, int)]
  7. tripleResult ← 60

    16// Three integers17int tripleResult→ 60 = add(10, 20, 30);18System.out.println("add(10, 20, 30) = " + tripleResult60);1920// More types  //@var=_,!21long longResult = add(1000000000L, 2000000000L);22System.out.println("add(1000000000L, 2000000000L) = " + longResult);
    outputadd(10, 20, 30) = 60
  8. static long add(long a, long b)

    49// Overload 4: Two longs  //@var=_,!50static long add(long a1000000000, long b2000000000) {  //@var=_,!51    System.out.println("  [Called: add(long, long)]");  //@var=_,!52    return a1000000000 + b2000000000;  //@var=_,!53}  //@var=_,!
    output  [Called: add(long, long)]
  9. longResult ← 3000000000

    20    // More types  //@var=_,!21    long longResult→ 3000000000 = add(1000000000L, 2000000000L);22    System.out.println("add(1000000000L, 2000000000L) = " + longResult3000000000);23    24    // Compiler chooses the right method!25    System.out.println("\n=== Compiler Decision ===");26    System.out.println("add(1, 2)     → int version");27    System.out.println("add(1.0, 2.0) → double version");28    System.out.println("add(1, 2, 3)  → three-param version");29}
    outputadd(1000000000L, 2000000000L) = 3000000000
    
    === Compiler Decision ===
    add(1, 2)     → int version
    add(1.0, 2.0) → double version
    add(1, 2, 3)  → three-param version
  1. left ← 9, right ← 3

    1public class AddOverload {2    public static void main(String[] args) {3        System.out.println("=== Method Overloading: add() ===\n");4        5        // Integer addition6        int left→ 9 = 9;7        int right→ 3 = 3;8        int intResult = add(left9, right3);9        System.out.println("add(" + left + ", " + right + ") = " + intResult);
    output=== Method Overloading: add() ===
  2. static int add(int a, int b)

    30// Overload 1: Two integers31static int add(int a9, int b3) {32    System.out.println("  [Called: add(int, int)]");33    return a9 + b3;34}
    output  [Called: add(int, int)]
  3. intResult ← 12

    7int right = 3;8int intResult→ 12 = add(left9, right3);9System.out.println("add(" + left9 + ", " + right3 + ") = " + intResult12);1011// Double addition12double doubleResult = add(5.5, 3.2);13System.out.println("add(5.5, 3.2) = " + doubleResult);
    outputadd(9, 3) = 12
  4. static double add(double a, double b)

    36// Overload 2: Two doubles37static double add(double a5.5, double b3.2) {38    System.out.println("  [Called: add(double, double)]");39    return a5.5 + b3.2;40}
    output  [Called: add(double, double)]
  5. doubleResult ← 8.7

    11// Double addition12double doubleResult→ 8.7 = add(5.5, 3.2);13System.out.println("add(5.5, 3.2) = " + doubleResult8.7);1415// Three integers16int tripleResult = add(10, 20, 30);17System.out.println("add(10, 20, 30) = " + tripleResult);
    outputadd(5.5, 3.2) = 8.7
  6. static int add(int a, int b, int c)

    42// Overload 3: Three integers43static int add(int a10, int b20, int c30) {44    System.out.println("  [Called: add(int, int, int)]");45    return a10 + b20 + c30;46}
    output  [Called: add(int, int, int)]
  7. tripleResult ← 60

    15// Three integers16int tripleResult→ 60 = add(10, 20, 30);17System.out.println("add(10, 20, 30) = " + tripleResult60);1819// More types20long longResult = add(1000000000L, 2000000000L);21System.out.println("add(1000000000L, 2000000000L) = " + longResult);
    outputadd(10, 20, 30) = 60
  8. static long add(long a, long b)

    48// Overload 4: Two longs49static long add(long a1000000000, long b2000000000) {50    System.out.println("  [Called: add(long, long)]");51    return a1000000000 + b2000000000;52}
    output  [Called: add(long, long)]
  9. longResult ← 3000000000

    19    // More types20    long longResult→ 3000000000 = add(1000000000L, 2000000000L);21    System.out.println("add(1000000000L, 2000000000L) = " + longResult3000000000);22    23    // Compiler chooses the right method!24    System.out.println("\n=== Compiler Decision ===");25    System.out.println("add(1, 2)     → int version");26    System.out.println("add(1.0, 2.0) → double version");27    System.out.println("add(1, 2, 3)  → three-param version");28}
    outputadd(1000000000L, 2000000000L) = 3000000000
    
    === Compiler Decision ===
    add(1, 2)     → int version
    add(1.0, 2.0) → double version
    add(1, 2, 3)  → three-param version
  1. left ← 12, right ← 3

    1public class AddOverload {2    public static void main(String[] args) {3        System.out.println("=== Method Overloading: add() ===\n");4        5        // Integer addition6        int left→ 12 = 12;7        int right→ 3 = 3;8        int intResult = add(left12, right3);9        System.out.println("add(" + left + ", " + right + ") = " + intResult);
    output=== Method Overloading: add() ===
  2. static int add(int a, int b)

    30// Overload 1: Two integers31static int add(int a12, int b3) {32    System.out.println("  [Called: add(int, int)]");33    return a12 + b3;34}
    output  [Called: add(int, int)]
  3. intResult ← 15

    7int right = 3;8int intResult→ 15 = add(left12, right3);9System.out.println("add(" + left12 + ", " + right3 + ") = " + intResult15);1011// Double addition12double doubleResult = add(5.5, 3.2);13System.out.println("add(5.5, 3.2) = " + doubleResult);
    outputadd(12, 3) = 15
  4. static double add(double a, double b)

    36// Overload 2: Two doubles37static double add(double a5.5, double b3.2) {38    System.out.println("  [Called: add(double, double)]");39    return a5.5 + b3.2;40}
    output  [Called: add(double, double)]
  5. doubleResult ← 8.7

    11// Double addition12double doubleResult→ 8.7 = add(5.5, 3.2);13System.out.println("add(5.5, 3.2) = " + doubleResult8.7);1415// Three integers16int tripleResult = add(10, 20, 30);17System.out.println("add(10, 20, 30) = " + tripleResult);
    outputadd(5.5, 3.2) = 8.7
  6. static int add(int a, int b, int c)

    42// Overload 3: Three integers43static int add(int a10, int b20, int c30) {44    System.out.println("  [Called: add(int, int, int)]");45    return a10 + b20 + c30;46}
    output  [Called: add(int, int, int)]
  7. tripleResult ← 60

    15// Three integers16int tripleResult→ 60 = add(10, 20, 30);17System.out.println("add(10, 20, 30) = " + tripleResult60);1819// More types20long longResult = add(1000000000L, 2000000000L);21System.out.println("add(1000000000L, 2000000000L) = " + longResult);
    outputadd(10, 20, 30) = 60
  8. static long add(long a, long b)

    48// Overload 4: Two longs49static long add(long a1000000000, long b2000000000) {50    System.out.println("  [Called: add(long, long)]");51    return a1000000000 + b2000000000;52}
    output  [Called: add(long, long)]
  9. longResult ← 3000000000

    19    // More types20    long longResult→ 3000000000 = add(1000000000L, 2000000000L);21    System.out.println("add(1000000000L, 2000000000L) = " + longResult3000000000);22    23    // Compiler chooses the right method!24    System.out.println("\n=== Compiler Decision ===");25    System.out.println("add(1, 2)     → int version");26    System.out.println("add(1.0, 2.0) → double version");27    System.out.println("add(1, 2, 3)  → three-param version");28}
    outputadd(1000000000L, 2000000000L) = 3000000000
    
    === Compiler Decision ===
    add(1, 2)     → int version
    add(1.0, 2.0) → double version
    add(1, 2, 3)  → three-param version
  1. left ← 5, right ← 4

    1public class AddOverload {2    public static void main(String[] args) {3        System.out.println("=== Method Overloading: add() ===\n");4        5        // Integer addition6        int left→ 5 = 5;7        int right→ 4 = 4;8        int intResult = add(left5, right4);9        System.out.println("add(" + left + ", " + right + ") = " + intResult);
    output=== Method Overloading: add() ===
  2. static int add(int a, int b)

    30// Overload 1: Two integers31static int add(int a5, int b4) {32    System.out.println("  [Called: add(int, int)]");33    return a5 + b4;34}
    output  [Called: add(int, int)]
  3. intResult ← 9

    7int right = 4;8int intResult→ 9 = add(left5, right4);9System.out.println("add(" + left5 + ", " + right4 + ") = " + intResult9);1011// Double addition12double doubleResult = add(5.5, 3.2);13System.out.println("add(5.5, 3.2) = " + doubleResult);
    outputadd(5, 4) = 9
  4. static double add(double a, double b)

    36// Overload 2: Two doubles37static double add(double a5.5, double b3.2) {38    System.out.println("  [Called: add(double, double)]");39    return a5.5 + b3.2;40}
    output  [Called: add(double, double)]
  5. doubleResult ← 8.7

    11// Double addition12double doubleResult→ 8.7 = add(5.5, 3.2);13System.out.println("add(5.5, 3.2) = " + doubleResult8.7);1415// Three integers16int tripleResult = add(10, 20, 30);17System.out.println("add(10, 20, 30) = " + tripleResult);
    outputadd(5.5, 3.2) = 8.7
  6. static int add(int a, int b, int c)

    42// Overload 3: Three integers43static int add(int a10, int b20, int c30) {44    System.out.println("  [Called: add(int, int, int)]");45    return a10 + b20 + c30;46}
    output  [Called: add(int, int, int)]
  7. tripleResult ← 60

    15// Three integers16int tripleResult→ 60 = add(10, 20, 30);17System.out.println("add(10, 20, 30) = " + tripleResult60);1819// More types20long longResult = add(1000000000L, 2000000000L);21System.out.println("add(1000000000L, 2000000000L) = " + longResult);
    outputadd(10, 20, 30) = 60
  8. static long add(long a, long b)

    48// Overload 4: Two longs49static long add(long a1000000000, long b2000000000) {50    System.out.println("  [Called: add(long, long)]");51    return a1000000000 + b2000000000;52}
    output  [Called: add(long, long)]
  9. longResult ← 3000000000

    19    // More types20    long longResult→ 3000000000 = add(1000000000L, 2000000000L);21    System.out.println("add(1000000000L, 2000000000L) = " + longResult3000000000);22    23    // Compiler chooses the right method!24    System.out.println("\n=== Compiler Decision ===");25    System.out.println("add(1, 2)     → int version");26    System.out.println("add(1.0, 2.0) → double version");27    System.out.println("add(1, 2, 3)  → three-param version");28}
    outputadd(1000000000L, 2000000000L) = 3000000000
    
    === Compiler Decision ===
    add(1, 2)     → int version
    add(1.0, 2.0) → double version
    add(1, 2, 3)  → three-param version
  1. left ← 5, right ← 10

    1public class AddOverload {2    public static void main(String[] args) {3        System.out.println("=== Method Overloading: add() ===\n");4        5        // Integer addition6        int left→ 5 = 5;7        int right→ 10 = 10;8        int intResult = add(left5, right10);9        System.out.println("add(" + left + ", " + right + ") = " + intResult);
    output=== Method Overloading: add() ===
  2. static int add(int a, int b)

    30// Overload 1: Two integers31static int add(int a5, int b10) {32    System.out.println("  [Called: add(int, int)]");33    return a5 + b10;34}
    output  [Called: add(int, int)]
  3. intResult ← 15

    7int right = 10;8int intResult→ 15 = add(left5, right10);9System.out.println("add(" + left5 + ", " + right10 + ") = " + intResult15);1011// Double addition12double doubleResult = add(5.5, 3.2);13System.out.println("add(5.5, 3.2) = " + doubleResult);
    outputadd(5, 10) = 15
  4. static double add(double a, double b)

    36// Overload 2: Two doubles37static double add(double a5.5, double b3.2) {38    System.out.println("  [Called: add(double, double)]");39    return a5.5 + b3.2;40}
    output  [Called: add(double, double)]
  5. doubleResult ← 8.7

    11// Double addition12double doubleResult→ 8.7 = add(5.5, 3.2);13System.out.println("add(5.5, 3.2) = " + doubleResult8.7);1415// Three integers16int tripleResult = add(10, 20, 30);17System.out.println("add(10, 20, 30) = " + tripleResult);
    outputadd(5.5, 3.2) = 8.7
  6. static int add(int a, int b, int c)

    42// Overload 3: Three integers43static int add(int a10, int b20, int c30) {44    System.out.println("  [Called: add(int, int, int)]");45    return a10 + b20 + c30;46}
    output  [Called: add(int, int, int)]
  7. tripleResult ← 60

    15// Three integers16int tripleResult→ 60 = add(10, 20, 30);17System.out.println("add(10, 20, 30) = " + tripleResult60);1819// More types20long longResult = add(1000000000L, 2000000000L);21System.out.println("add(1000000000L, 2000000000L) = " + longResult);
    outputadd(10, 20, 30) = 60
  8. static long add(long a, long b)

    48// Overload 4: Two longs49static long add(long a1000000000, long b2000000000) {50    System.out.println("  [Called: add(long, long)]");51    return a1000000000 + b2000000000;52}
    output  [Called: add(long, long)]
  9. longResult ← 3000000000

    19    // More types20    long longResult→ 3000000000 = add(1000000000L, 2000000000L);21    System.out.println("add(1000000000L, 2000000000L) = " + longResult3000000000);22    23    // Compiler chooses the right method!24    System.out.println("\n=== Compiler Decision ===");25    System.out.println("add(1, 2)     → int version");26    System.out.println("add(1.0, 2.0) → double version");27    System.out.println("add(1, 2, 3)  → three-param version");28}
    outputadd(1000000000L, 2000000000L) = 3000000000
    
    === Compiler Decision ===
    add(1, 2)     → int version
    add(1.0, 2.0) → double version
    add(1, 2, 3)  → three-param version

Same name, different types: add(int, int) vs add(double, double).

method overloading Multiple methods with same name but different parameter lists.

Overload for multiple data types

Handle different input types with the same method name.

PrintOverload.java
Replay: real traced execution (multi-file project)
public class PrintOverload {
    public static void main(String[] args) {
        System.out.println("=== Overloaded print() ===\n");

        // Different types, same method name
        print(42);                          // int
        print(3.14159);                     // double
        print("Hello World");               // String
        print(true);                        // boolean

        // Arrays
        print(new int[]{1, 2, 3, 4, 5});    // int array
        print(new String[]{"a", "b", "c"}); // String array

        System.out.println("\n=== How Java's System.out Works ===");
        System.out.println("System.out has 10+ overloaded println() methods!");
        System.out.println("println(int), println(double), println(String), etc.");
    }

    // Overload for int
    static void print(int value) {
        System.out.println("Integer: " + value);
    }

    // Overload for double
    static void print(double value) {
        System.out.println("Double: " + value);
    }

    // Overload for String
    static void print(String value) {
        System.out.println("String: \"" + value + "\"");
    }

    // Overload for boolean
    static void print(boolean value) {
        System.out.println("Boolean: " + value);
    }

    // Overload for int array
    static void print(int[] arr) {
        System.out.print("Int Array: [");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);
            if (i < arr.length - 1) System.out.print(", ");
        }
        System.out.println("]");
    }

    // Overload for String array
    static void print(String[] arr) {
        System.out.print("String Array: [");
        for (int i = 0; i < arr.length; i++) {
            System.out.print("\"" + arr[i] + "\"");
            if (i < arr.length - 1) System.out.print(", ");
        }
        System.out.println("]");
    }
}
  1. public static void main(String[] args)

    2public class PrintOverload {3    public static void main(String[] args) {4        System.out.println("=== Overloaded print() ===\n");5        6        // Different types, same method name7        print(42);                          // int8        print(3.14159);                     // double
    output=== Overloaded print() ===
  2. static void print(int value)

    6    // Different types, same method name7    print(42);                          // int8    print(3.14159);                     // double9    print("Hello World");               // String10    print(true);                        // boolean11    12    // Arrays  //@var=_,!13    print(new int[]{1, 2, 3, 4, 5});    // int array14    print(new String[]{"a", "b", "c"}); // String array15    16    System.out.println("\n=== How Java's System.out Works ===");17    System.out.println("System.out has 10+ overloaded println() methods!");18    System.out.println("println(int), println(double), println(String), etc.");19}2021// Overload for int  //?print22static void print(int value42) {23    System.out.println("Integer: " + value42);24}
    outputInteger: 42
  3. static void print(double value)

    7    print(42);                          // int8    print(3.14159);                     // double9    print("Hello World");               // String10    print(true);                        // boolean11    12    // Arrays  //@var=_,!13    print(new int[]{1, 2, 3, 4, 5});    // int array14    print(new String[]{"a", "b", "c"}); // String array15    16    System.out.println("\n=== How Java's System.out Works ===");17    System.out.println("System.out has 10+ overloaded println() methods!");18    System.out.println("println(int), println(double), println(String), etc.");19}2021// Overload for int  //?print22static void print(int value) {23    System.out.println("Integer: " + value);24}2526// Overload for double27static void print(double value3.14159) {28    System.out.println("Double: " + value3.14159);29}
    outputDouble: 3.14159
  4. static void print(String value)

    8    print(3.14159);                     // double9    print("Hello World");               // String10    print(true);                        // boolean11    12    // Arrays  //@var=_,!13    print(new int[]{1, 2, 3, 4, 5});    // int array14    print(new String[]{"a", "b", "c"}); // String array15    16    System.out.println("\n=== How Java's System.out Works ===");17    System.out.println("System.out has 10+ overloaded println() methods!");18    System.out.println("println(int), println(double), println(String), etc.");19}2021// Overload for int  //?print22static void print(int value) {23    System.out.println("Integer: " + value);24}2526// Overload for double27static void print(double value) {28    System.out.println("Double: " + value);29}3031// Overload for String32static void print(String valueHello World) {33    System.out.println("String: \"" + valueHello World + "\"");34}
    outputString: "Hello World"
  5. static void print(boolean value)

    9    print("Hello World");               // String10    print(true);                        // boolean11    12    // Arrays  //@var=_,!13    print(new int[]{1, 2, 3, 4, 5});    // int array14    print(new String[]{"a", "b", "c"}); // String array15    16    System.out.println("\n=== How Java's System.out Works ===");17    System.out.println("System.out has 10+ overloaded println() methods!");18    System.out.println("println(int), println(double), println(String), etc.");19}2021// Overload for int  //?print22static void print(int value) {23    System.out.println("Integer: " + value);24}2526// Overload for double27static void print(double value) {28    System.out.println("Double: " + value);29}3031// Overload for String32static void print(String value) {33    System.out.println("String: \"" + value + "\"");34}3536// Overload for boolean37static void print(boolean valuetrue) {38    System.out.println("Boolean: " + valuetrue);39}
    outputBoolean: true
  6. static void print(int[] arr)

    41// Overload for int array  //@var=_,!42static void print(int[] arr) {  //@var=_,!43    System.out.print("Int Array: [");  //@var=_,!44    for (int i = 0; i < arr.length; i++) {  //@var=_,!
    outputInt Array: [
  7. for (int i = 0; i < arr.length; i++)

    pass 1 of 5
    43System.out.print("Int Array: [");  //@var=_,!44for (int i0 = 0; i < arr.length5; i++) {  //@var=_,!45    System.out.print(arr[i]1);  //@var=_,!46    if (i < arr.length - 1) System.out.print(", ");  //@var=_,!
    output1
    All 5 passes — pass 1 is the card above
    passiarr[i]
    101
    212
    323
    434
    545
  8. if (i < arr.length - 1)

    pass 1 of 4
    45    System.out.print(arr[i]);  //@var=_,!46    if (i0 < arr.length5 - 1) System.out.print(", ");  //@var=_,!47}  //@var=_,!
    output, 
    All 4 passes — pass 1 is the card above
    passi
    10
    21
    32
    43
  9. System.out.println("]"); //@var=_,!

    12    // Arrays  //@var=_,!13    print(new int[]{1, 2, 3, 4, 5});    // int array14    print(new String[]{"a", "b", "c"}); // String array15    16    System.out.println("\n=== How Java's System.out Works ===");17    System.out.println("System.out has 10+ overloaded println() methods!");18    System.out.println("println(int), println(double), println(String), etc.");19}2021// Overload for int  //?print22static void print(int value) {23    System.out.println("Integer: " + value);24}2526// Overload for double27static void print(double value) {28    System.out.println("Double: " + value);29}3031// Overload for String32static void print(String value) {33    System.out.println("String: \"" + value + "\"");34}3536// Overload for boolean37static void print(boolean value) {38    System.out.println("Boolean: " + value);39}4041// Overload for int array  //@var=_,!42static void print(int[] arr) {  //@var=_,!43    System.out.print("Int Array: [");  //@var=_,!44    for (int i = 0; i < arr.length; i++) {  //@var=_,!45        System.out.print(arr[i]);  //@var=_,!46        if (i < arr.length - 1) System.out.print(", ");  //@var=_,!47    }  //@var=_,!48    System.out.println("]");  //@var=_,!49}  //@var=_,!
    output]
  10. static void print(String[] arr)

    51// Overload for String array  //@var=_,!52static void print(String[] arr) {  //@var=_,!53    System.out.print("String Array: [");  //@var=_,!54    for (int i = 0; i < arr.length; i++) {  //@var=_,!
    outputString Array: [
  11. for (int i = 0; i < arr.length; i++)

    pass 1 of 3
    53System.out.print("String Array: [");  //@var=_,!54for (int i0 = 0; i < arr.length3; i++) {  //@var=_,!55    System.out.print("\"" + arr[i]a + "\"");  //@var=_,!56    if (i < arr.length - 1) System.out.print(", ");  //@var=_,!
    output"a"
    All 3 passes — pass 1 is the card above
    passiarr[i]
    10a
    21b
    32c
  12. if (i < arr.length - 1)

    pass 1 of 2
    55    System.out.print("\"" + arr[i] + "\"");  //@var=_,!56    if (i0 < arr.length3 - 1) System.out.print(", ");  //@var=_,!57}  //@var=_,!
    output, 
  13. if (i < arr.length - 1)

    pass 2 of 2
    55    System.out.print("\"" + arr[i] + "\"");  //@var=_,!56    if (i1 < arr.length3 - 1) System.out.print(", ");  //@var=_,!57}  //@var=_,!
    output, 
  14. System.out.println("]"); //@var=_,!

    13    print(new int[]{1, 2, 3, 4, 5});    // int array14    print(new String[]{"a", "b", "c"}); // String array15    16    System.out.println("\n=== How Java's System.out Works ===");17    System.out.println("System.out has 10+ overloaded println() methods!");18    System.out.println("println(int), println(double), println(String), etc.");19}2021// Overload for int  //?print22static void print(int value) {23    System.out.println("Integer: " + value);24}2526// Overload for double27static void print(double value) {28    System.out.println("Double: " + value);29}3031// Overload for String32static void print(String value) {33    System.out.println("String: \"" + value + "\"");34}3536// Overload for boolean37static void print(boolean value) {38    System.out.println("Boolean: " + value);39}4041// Overload for int array  //@var=_,!42static void print(int[] arr) {  //@var=_,!43    System.out.print("Int Array: [");  //@var=_,!44    for (int i = 0; i < arr.length; i++) {  //@var=_,!45        System.out.print(arr[i]);  //@var=_,!46        if (i < arr.length - 1) System.out.print(", ");  //@var=_,!47    }  //@var=_,!48    System.out.println("]");  //@var=_,!49}  //@var=_,!5051// Overload for String array  //@var=_,!52static void print(String[] arr) {  //@var=_,!53    System.out.print("String Array: [");  //@var=_,!54    for (int i = 0; i < arr.length; i++) {  //@var=_,!55        System.out.print("\"" + arr[i] + "\"");  //@var=_,!56        if (i < arr.length - 1) System.out.print(", ");  //@var=_,!57    }  //@var=_,!58    System.out.println("]");  //@var=_,!59}  //@var=_,!
    output]
    
    === How Java's System.out Works ===
    System.out has 10+ overloaded println() methods!
    println(int), println(double), println(String), etc.

The compiler picks the right version based on argument types.

Overload for different argument counts

Provide flexible APIs with varying parameter counts.

MaxOverload.java
Replay: real traced execution (multi-file project)
public class MaxOverload {
    public static void main(String[] args) {
        System.out.println("=== Overloaded max() ===\n");

        // Two arguments
        System.out.println("max(10, 20) = " + max(10, 20));
        System.out.println("max(3.5, 2.8) = " + max(3.5, 2.8));

        // Three arguments
        System.out.println("max(5, 12, 8) = " + max(5, 12, 8));
        System.out.println("max(1.1, 2.2, 3.3) = " + max(1.1, 2.2, 3.3));

        // Four arguments
        System.out.println("max(4, 7, 2, 9) = " + max(4, 7, 2, 9));

        // Compare with Java's Math.max
        System.out.println("\n=== Java's Math.max ===");
        System.out.println("Math.max(10, 20) = " + Math.max(10, 20));
        System.out.println("Math.max only takes 2 args - chain for more:");
        System.out.println("Math.max(Math.max(5, 12), 8) = " +
            Math.max(Math.max(5, 12), 8));

        // Min versions
        System.out.println("\n=== Overloaded min() ===");
        System.out.println("min(10, 20) = " + min(10, 20));
        System.out.println("min(5, 12, 8) = " + min(5, 12, 8));
    }

    // Two ints
    static int max(int a, int b) {
        return (a > b) ? a : b;
    }

    // Two doubles
    static double max(double a, double b) {
        return (a > b) ? a : b;
    }

    // Three ints
    static int max(int a, int b, int c) {
        return max(max(a, b), c);  // Reuse two-arg version!
    }

    // Three doubles
    static double max(double a, double b, double c) {
        return max(max(a, b), c);
    }

    // Four ints
    static int max(int a, int b, int c, int d) {
        return max(max(a, b), max(c, d));
    }

    // Min versions
    static int min(int a, int b) {
        return (a < b) ? a : b;
    }

    static int min(int a, int b, int c) {
        return min(min(a, b), c);
    }
}
  1. public static void main(String[] args)

    2public class MaxOverload {3    public static void main(String[] args) {4        System.out.println("=== Overloaded max() ===\n");5        6        // Two arguments7        System.out.println("max(10, 20) = " + max(10, 20));8        System.out.println("max(3.5, 2.8) = " + max(3.5, 2.8));
    output=== Overloaded max() ===
  2. static int max(int a, int b)

    pass 1 of 6
    30// Two ints31static int max(int a10, int b20) {32    return (a10 > b20) ? a : b;33}
    All 6 passes — pass 1 is the card above
    passab
    11020
    2512
    3128
    447
    529
    679
  3. System.out.println("max(10, 20) = " + max(10, 20));

    6// Two arguments7System.out.println("max(10, 20) = " + max(10, 20));8System.out.println("max(3.5, 2.8) = " + max(3.5, 2.8));
    outputmax(10, 20) = 20
  4. static double max(double a, double b)

    pass 1 of 3
    35// Two doubles36static double max(double a3.5, double b2.8) {37    return (a3.5 > b2.8) ? a : b;38}
    All 3 passes — pass 1 is the card above
    passab
    13.52.8
    21.12.2
    32.23.3
  5. System.out.println("max(3.5, 2.8) = " + max(3.5, 2.8));

    7System.out.println("max(10, 20) = " + max(10, 20));8System.out.println("max(3.5, 2.8) = " + max(3.5, 2.8));910// Three arguments  //?three11System.out.println("max(5, 12, 8) = " + max(5, 12, 8));12System.out.println("max(1.1, 2.2, 3.3) = " + max(1.1, 2.2, 3.3));
    outputmax(3.5, 2.8) = 3.5
  6. static int max(int a, int b, int c)

    40// Three ints  //?chain41static int max(int a5, int b12, int c8) {42    return max(max(a5, b12), c8);  // Reuse two-arg version!43}
  7. System.out.println("max(5, 12, 8) = " + max(5, 12, 8));

    10// Three arguments  //?three11System.out.println("max(5, 12, 8) = " + max(5, 12, 8));12System.out.println("max(1.1, 2.2, 3.3) = " + max(1.1, 2.2, 3.3));
    outputmax(5, 12, 8) = 12
  8. static double max(double a, double b, double c)

    45// Three doubles46static double max(double a1.1, double b2.2, double c3.3) {47    return max(max(a1.1, b2.2), c3.3);48}
  9. System.out.println("max(1.1, 2.2, 3.3) = " + max(1.1, 2.2, 3.3));

    11System.out.println("max(5, 12, 8) = " + max(5, 12, 8));12System.out.println("max(1.1, 2.2, 3.3) = " + max(1.1, 2.2, 3.3));1314// Four arguments15System.out.println("max(4, 7, 2, 9) = " + max(4, 7, 2, 9));
    outputmax(1.1, 2.2, 3.3) = 3.3
  10. static int max(int a, int b, int c, int d)

    50// Four ints51static int max(int a4, int b7, int c2, int d9) {52    return max(max(a4, b7), max(c2, d9));53}
  11. System.out.println("max(4, 7, 2, 9) = " + max(4, 7, 2, 9));

    14// Four arguments15System.out.println("max(4, 7, 2, 9) = " + max(4, 7, 2, 9));1617// Compare with Java's Math.max  //@var=_,!18System.out.println("\n=== Java's Math.max ===");19System.out.println("Math.max(10, 20) = " + Math.max(10, 20));20System.out.println("Math.max only takes 2 args - chain for more:");21System.out.println("Math.max(Math.max(5, 12), 8) = " + 22    Math.max(Math.max(5, 12), 8));2324// Min versions  //@var=!,_25System.out.println("\n=== Overloaded min() ===");26System.out.println("min(10, 20) = " + min(10, 20));27System.out.println("min(5, 12, 8) = " + min(5, 12, 8));
    outputmax(4, 7, 2, 9) = 9
    
    === Java's Math.max ===
    Math.max(10, 20) = 20
    Math.max only takes 2 args - chain for more:
    Math.max(Math.max(5, 12), 8) = 12
    
    === Overloaded min() ===
  12. static int min(int a, int b)

    pass 1 of 3
    55// Min versions  //@var=!,_56static int min(int a10, int b20) {  //@var=!,_57    return (a10 < b20) ? a : b;  //@var=!,_58}  //@var=!,_
    All 3 passes — pass 1 is the card above
    passab
    11020
    2512
    358
  13. System.out.println("min(10, 20) = " + min(10, 20));

    25    System.out.println("\n=== Overloaded min() ===");26    System.out.println("min(10, 20) = " + min(10, 20));27    System.out.println("min(5, 12, 8) = " + min(5, 12, 8));28}
    outputmin(10, 20) = 10
  14. static int min(int a, int b, int c)

    60static int min(int a5, int b12, int c8) {  //@var=!,_61    return min(min(a5, b12), c8);  //@var=!,_62}  //@var=!,_
  15. System.out.println("min(5, 12, 8) = " + min(5, 12, 8));

    26    System.out.println("min(10, 20) = " + min(10, 20));27    System.out.println("min(5, 12, 8) = " + min(5, 12, 8));28}
    outputmin(5, 12, 8) = 5

max(a, b) and max(a, b, c) - same concept, different arity.

Constructor overloading

Create objects in multiple ways using overloaded constructors.

ConstructorOverload.java
Replay: real traced execution (multi-file project)
public class ConstructorOverload {
    public static void main(String[] args) {
        System.out.println("=== Constructor Overloading ===\n");

        // Different ways to create a Book
        Book book1 = new Book();
        System.out.println("Default: " + book1);

        Book book2 = new Book("1984");
        System.out.println("Title only: " + book2);

        Book book3 = new Book("1984", "George Orwell");
        System.out.println("Title + Author: " + book3);

        Book book4 = new Book("1984", "George Orwell", 328);
        System.out.println("All params: " + book4);

        // Copy constructor
        Book book5 = new Book(book4);
        System.out.println("Copy: " + book5);

        System.out.println("\n=== Flexibility of Overloading ===");
        System.out.println("Same class, 5 ways to create objects!");
    }
}

class Book {
    String title;
    String author;
    int pages;

    // Constructor 1: No arguments (defaults)
    Book() {
        this("Unknown Title", "Unknown Author", 0);  // Calls another constructor
    }

    // Constructor 2: Title only
    Book(String title) {
        this(title, "Unknown Author", 0);
    }

    // Constructor 3: Title and author
    Book(String title, String author) {
        this(title, author, 0);
    }

    // Constructor 4: All parameters (primary)
    Book(String title, String author, int pages) {
        this.title = title;
        this.author = author;
        this.pages = pages;
    }

    // Constructor 5: Copy constructor
    Book(Book other) {
        this(other.title, other.author, other.pages);
    }

    @Override
    public String toString() {
        return "\"" + title + "\" by " + author +
               (pages > 0 ? " (" + pages + " pages)" : "");
    }
}
  1. public static void main(String[] args)

    1public class ConstructorOverload {2    public static void main(String[] args) {3        System.out.println("=== Constructor Overloading ===\n");4        5        // Different ways to create a Book  //?constructors6        Book book1 = new Book();7        System.out.println("Default: " + book1);
    output=== Constructor Overloading ===
  2. this.title ← Unknown Title, this.author ← Unknown Author, this.pages ← 0

    pass 1 of 5
    47// Constructor 4: All parameters (primary)  //?primary48Book(String titleUnknown Title, String authorUnknown Author, int pages0) {49    this.title→ Unknown Title = titleUnknown Title;50    this.author→ Unknown Author = authorUnknown Author;51    this.pages→ 0 = pages0;52}
    All 5 passes — pass 1 is the card above
    passtitleauthorpagesotherthis.titlethis.authorthis.pagesbook1book2book3book4book5
    1Unknown TitleUnknown Author0Unknown TitleUnknown Author0"Unknown Title" by Unknown Author
    21984Unknown Author01984Unknown Author0"1984" by Unknown Author
    31984George Orwell01984George Orwell0"1984" by George Orwell
    41984George Orwell3281984George Orwell328"1984" by George Orwell (328 pages)
    51984George Orwell328"1984" by George Orwell (328 pages)1984George Orwell328"1984" by George Orwell (328 pages)
  3. book1 ← "Unknown Title" by Unknown Author

    5        // Different ways to create a Book  //?constructors6        Book book1→ "Unknown Title" by Unknown Author = new Book();7        System.out.println("Default: " + book1"Unknown Title" by Unknown Author);8        9        Book book2 = new Book("1984");10        System.out.println("Title only: " + book2);11        12        Book book3 = new Book("1984", "George Orwell");13        System.out.println("Title + Author: " + book3);14        15        Book book4 = new Book("1984", "George Orwell", 328);16        System.out.println("All params: " + book4);17        18        // Copy constructor19        Book book5 = new Book(book4);  //?copy20        System.out.println("Copy: " + book5);21        22        System.out.println("\n=== Flexibility of Overloading ===");23        System.out.println("Same class, 5 ways to create objects!");24    }25}2627class Book {28    String title;29    String author;30    int pages;31    32    // Constructor 1: No arguments (defaults)  //?default33    Book() {34        this("Unknown Title", "Unknown Author", 0);  // Calls another constructor
    outputDefault: "Unknown Title" by Unknown Author
  4. book2 ← "1984" by Unknown Author

    9        Book book2→ "1984" by Unknown Author = new Book("1984");10        System.out.println("Title only: " + book2"1984" by Unknown Author);11        12        Book book3 = new Book("1984", "George Orwell");13        System.out.println("Title + Author: " + book3);14        15        Book book4 = new Book("1984", "George Orwell", 328);16        System.out.println("All params: " + book4);17        18        // Copy constructor19        Book book5 = new Book(book4);  //?copy20        System.out.println("Copy: " + book5);21        22        System.out.println("\n=== Flexibility of Overloading ===");23        System.out.println("Same class, 5 ways to create objects!");24    }25}2627class Book {28    String title;29    String author;30    int pages;31    32    // Constructor 1: No arguments (defaults)  //?default33    Book() {34        this("Unknown Title", "Unknown Author", 0);  // Calls another constructor35    }36    37    // Constructor 2: Title only38    Book(String title1984) {39        this(title, "Unknown Author", 0);
    outputTitle only: "1984" by Unknown Author
  5. book3 ← "1984" by George Orwell

    12        Book book3→ "1984" by George Orwell = new Book("1984", "George Orwell");13        System.out.println("Title + Author: " + book3"1984" by George Orwell);14        15        Book book4 = new Book("1984", "George Orwell", 328);16        System.out.println("All params: " + book4);17        18        // Copy constructor19        Book book5 = new Book(book4);  //?copy20        System.out.println("Copy: " + book5);21        22        System.out.println("\n=== Flexibility of Overloading ===");23        System.out.println("Same class, 5 ways to create objects!");24    }25}2627class Book {28    String title;29    String author;30    int pages;31    32    // Constructor 1: No arguments (defaults)  //?default33    Book() {34        this("Unknown Title", "Unknown Author", 0);  // Calls another constructor35    }36    37    // Constructor 2: Title only38    Book(String title) {39        this(title, "Unknown Author", 0);40    }41    42    // Constructor 3: Title and author43    Book(String title1984, String authorGeorge Orwell) {44        this(title, author, 0);
    outputTitle + Author: "1984" by George Orwell
  6. book5 ← "1984" by George Orwell (328 pages)

    18        // Copy constructor19        Book book5→ "1984" by George Orwell (328 pages) = new Book(book4);  //?copy20        System.out.println("Copy: " + book5"1984" by George Orwell (328 pages));21        22        System.out.println("\n=== Flexibility of Overloading ===");23        System.out.println("Same class, 5 ways to create objects!");24    }25}2627class Book {28    String title;29    String author;30    int pages;31    32    // Constructor 1: No arguments (defaults)  //?default33    Book() {34        this("Unknown Title", "Unknown Author", 0);  // Calls another constructor35    }36    37    // Constructor 2: Title only38    Book(String title) {39        this(title, "Unknown Author", 0);40    }41    42    // Constructor 3: Title and author43    Book(String title, String author) {44        this(title, author, 0);45    }46    47    // Constructor 4: All parameters (primary)  //?primary48    Book(String title, String author, int pages) {49        this.title = title;50        this.author = author;51        this.pages = pages;52    }53    54    // Constructor 5: Copy constructor  //?copyctr55    Book(Book other"1984" by George Orwell (328 pages)) {56        this(other.title, other.author, other.pages);
    outputCopy: "1984" by George Orwell (328 pages)
    
    === Flexibility of Overloading ===
    Same class, 5 ways to create objects!

Constructors can be overloaded just like regular methods.

constructor overloading Multiple constructors with different parameters for flexible object creation.

Avoid ambiguity

Understand when overloading becomes confusing.

Ambiguity.java
Replay: real traced execution (multi-file project)
public class Ambiguity {
    public static void main(String[] args) {
        System.out.println("=== Overloading Ambiguity ===\n");

        // Clear cases - no ambiguity
        System.out.println("=== Clear Cases ===");
        process(10);           // Clearly int
        process("Hello");      // Clearly String
        process(10, 20);       // Clearly two ints

        // Ambiguous case with null
        System.out.println("\n=== Null Ambiguity ===");
        // process(null);      // Error! Both String and int[] match null

        // Solution: explicit cast
        process((String) null);    // Explicit: String version
        process((int[]) null);     // Explicit: int[] version

        // Type promotion ambiguity
        System.out.println("\n=== Type Promotion ===");
        demo(10);         // int → could go to long or double?
        demo(10L);        // Explicitly long
        demo(10.0);       // Explicitly double

        System.out.println("\n=== Rules ===");
        System.out.println("1. Exact match is preferred");
        System.out.println("2. Widening (int→long→double) preferred over boxing");
        System.out.println("3. null can match any reference type - be explicit!");
    }

    // Overload 1: int
    static void process(int x) {
        System.out.println("process(int): " + x);
    }

    // Overload 2: String
    static void process(String s) {
        System.out.println("process(String): " + s);
    }

    // Overload 3: two ints
    static void process(int x, int y) {
        System.out.println("process(int, int): " + x + ", " + y);
    }

    // Overload 4: int array
    static void process(int[] arr) {
        System.out.println("process(int[]): array");
    }

    // Demo type promotion
    static void demo(int x) {
        System.out.println("demo(int): " + x);
    }

    static void demo(long x) {
        System.out.println("demo(long): " + x);
    }

    static void demo(double x) {
        System.out.println("demo(double): " + x);
    }
}
  1. public static void main(String[] args)

    1public class Ambiguity {2    public static void main(String[] args) {3        System.out.println("=== Overloading Ambiguity ===\n");4        5        // Clear cases - no ambiguity6        System.out.println("=== Clear Cases ===");7        process(10);           // Clearly int8        process("Hello");      // Clearly String
    output=== Overloading Ambiguity ===
    === Clear Cases ===
  2. static void process(int x)

    6    System.out.println("=== Clear Cases ===");7    process(10);           // Clearly int8    process("Hello");      // Clearly String9    process(10, 20);       // Clearly two ints10    11    // Ambiguous case with null  //?nullcase12    System.out.println("\n=== Null Ambiguity ===");13    // process(null);      // Error! Both String and int[] match null14    15    // Solution: explicit cast16    process((String) null);    // Explicit: String version17    process((int[]) null);     // Explicit: int[] version18    19    // Type promotion ambiguity  //?promotion20    System.out.println("\n=== Type Promotion ===");21    demo(10);         // int → could go to long or double?22    demo(10L);        // Explicitly long23    demo(10.0);       // Explicitly double24    25    System.out.println("\n=== Rules ===");26    System.out.println("1. Exact match is preferred");27    System.out.println("2. Widening (int→long→double) preferred over boxing");28    System.out.println("3. null can match any reference type - be explicit!");29}3031// Overload 1: int32static void process(int x10) {33    System.out.println("process(int): " + x10);34}
    outputprocess(int): 10
  3. static void process(String s)

    pass 1 of 2
    7    process(10);           // Clearly int8    process("Hello");      // Clearly String9    process(10, 20);       // Clearly two ints10    11    // Ambiguous case with null  //?nullcase12    System.out.println("\n=== Null Ambiguity ===");13    // process(null);      // Error! Both String and int[] match null14    15    // Solution: explicit cast16    process((String) null);    // Explicit: String version17    process((int[]) null);     // Explicit: int[] version18    19    // Type promotion ambiguity  //?promotion20    System.out.println("\n=== Type Promotion ===");21    demo(10);         // int → could go to long or double?22    demo(10L);        // Explicitly long23    demo(10.0);       // Explicitly double24    25    System.out.println("\n=== Rules ===");26    System.out.println("1. Exact match is preferred");27    System.out.println("2. Widening (int→long→double) preferred over boxing");28    System.out.println("3. null can match any reference type - be explicit!");29}3031// Overload 1: int32static void process(int x) {33    System.out.println("process(int): " + x);34}3536// Overload 2: String  37static void process(String sHello) {38    System.out.println("process(String): " + sHello);39}
    outputprocess(String): Hello
  4. static void process(int x, int y)

    8    process("Hello");      // Clearly String9    process(10, 20);       // Clearly two ints10    11    // Ambiguous case with null  //?nullcase12    System.out.println("\n=== Null Ambiguity ===");13    // process(null);      // Error! Both String and int[] match null14    15    // Solution: explicit cast16    process((String) null);    // Explicit: String version17    process((int[]) null);     // Explicit: int[] version18    19    // Type promotion ambiguity  //?promotion20    System.out.println("\n=== Type Promotion ===");21    demo(10);         // int → could go to long or double?22    demo(10L);        // Explicitly long23    demo(10.0);       // Explicitly double24    25    System.out.println("\n=== Rules ===");26    System.out.println("1. Exact match is preferred");27    System.out.println("2. Widening (int→long→double) preferred over boxing");28    System.out.println("3. null can match any reference type - be explicit!");29}3031// Overload 1: int32static void process(int x) {33    System.out.println("process(int): " + x);34}3536// Overload 2: String  37static void process(String s) {38    System.out.println("process(String): " + s);39}4041// Overload 3: two ints42static void process(int x10, int y20) {43    System.out.println("process(int, int): " + x10 + ", " + y20);44}
    outputprocess(int, int): 10, 20
    
    === Null Ambiguity ===
  5. static void process(String s)

    pass 2 of 2
    15    // Solution: explicit cast16    process((String) null);    // Explicit: String version17    process((int[]) null);     // Explicit: int[] version18    19    // Type promotion ambiguity  //?promotion20    System.out.println("\n=== Type Promotion ===");21    demo(10);         // int → could go to long or double?22    demo(10L);        // Explicitly long23    demo(10.0);       // Explicitly double24    25    System.out.println("\n=== Rules ===");26    System.out.println("1. Exact match is preferred");27    System.out.println("2. Widening (int→long→double) preferred over boxing");28    System.out.println("3. null can match any reference type - be explicit!");29}3031// Overload 1: int32static void process(int x) {33    System.out.println("process(int): " + x);34}3536// Overload 2: String  37static void process(String snull) {38    System.out.println("process(String): " + snull);39}
    outputprocess(String): null
  6. static void process(int[] arr)

    16    process((String) null);    // Explicit: String version17    process((int[]) null);     // Explicit: int[] version18    19    // Type promotion ambiguity  //?promotion20    System.out.println("\n=== Type Promotion ===");21    demo(10);         // int → could go to long or double?22    demo(10L);        // Explicitly long23    demo(10.0);       // Explicitly double24    25    System.out.println("\n=== Rules ===");26    System.out.println("1. Exact match is preferred");27    System.out.println("2. Widening (int→long→double) preferred over boxing");28    System.out.println("3. null can match any reference type - be explicit!");29}3031// Overload 1: int32static void process(int x) {33    System.out.println("process(int): " + x);34}3536// Overload 2: String  37static void process(String s) {38    System.out.println("process(String): " + s);39}4041// Overload 3: two ints42static void process(int x, int y) {43    System.out.println("process(int, int): " + x + ", " + y);44}4546// Overload 4: int array47static void process(int[] arrnull) {48    System.out.println("process(int[]): array");49}
    outputprocess(int[]): array
    
    === Type Promotion ===
  7. static void demo(int x)

    20    System.out.println("\n=== Type Promotion ===");21    demo(10);         // int → could go to long or double?22    demo(10L);        // Explicitly long23    demo(10.0);       // Explicitly double24    25    System.out.println("\n=== Rules ===");26    System.out.println("1. Exact match is preferred");27    System.out.println("2. Widening (int→long→double) preferred over boxing");28    System.out.println("3. null can match any reference type - be explicit!");29}3031// Overload 1: int32static void process(int x) {33    System.out.println("process(int): " + x);34}3536// Overload 2: String  37static void process(String s) {38    System.out.println("process(String): " + s);39}4041// Overload 3: two ints42static void process(int x, int y) {43    System.out.println("process(int, int): " + x + ", " + y);44}4546// Overload 4: int array47static void process(int[] arr) {48    System.out.println("process(int[]): array");49}5051// Demo type promotion52static void demo(int x10) {53    System.out.println("demo(int): " + x10);54}
    outputdemo(int): 10
  8. static void demo(long x)

    21    demo(10);         // int → could go to long or double?22    demo(10L);        // Explicitly long23    demo(10.0);       // Explicitly double24    25    System.out.println("\n=== Rules ===");26    System.out.println("1. Exact match is preferred");27    System.out.println("2. Widening (int→long→double) preferred over boxing");28    System.out.println("3. null can match any reference type - be explicit!");29}3031// Overload 1: int32static void process(int x) {33    System.out.println("process(int): " + x);34}3536// Overload 2: String  37static void process(String s) {38    System.out.println("process(String): " + s);39}4041// Overload 3: two ints42static void process(int x, int y) {43    System.out.println("process(int, int): " + x + ", " + y);44}4546// Overload 4: int array47static void process(int[] arr) {48    System.out.println("process(int[]): array");49}5051// Demo type promotion52static void demo(int x) {53    System.out.println("demo(int): " + x);54}5556static void demo(long x10) {  //?prefer57    System.out.println("demo(long): " + x10);58}
    outputdemo(long): 10
  9. static void demo(double x)

    22    demo(10L);        // Explicitly long23    demo(10.0);       // Explicitly double24    25    System.out.println("\n=== Rules ===");26    System.out.println("1. Exact match is preferred");27    System.out.println("2. Widening (int→long→double) preferred over boxing");28    System.out.println("3. null can match any reference type - be explicit!");29}3031// Overload 1: int32static void process(int x) {33    System.out.println("process(int): " + x);34}3536// Overload 2: String  37static void process(String s) {38    System.out.println("process(String): " + s);39}4041// Overload 3: two ints42static void process(int x, int y) {43    System.out.println("process(int, int): " + x + ", " + y);44}4546// Overload 4: int array47static void process(int[] arr) {48    System.out.println("process(int[]): array");49}5051// Demo type promotion52static void demo(int x) {53    System.out.println("demo(int): " + x);54}5556static void demo(long x) {  //?prefer57    System.out.println("demo(long): " + x);58}5960static void demo(double x10.0) {61    System.out.println("demo(double): " + x10.0);62}
    outputdemo(double): 10.0
    
    === Rules ===
    1. Exact match is preferred
    2. Widening (int→long→double) preferred over boxing
    3. null can match any reference type - be explicit!

Some combinations create ambiguous calls. Know the rules to avoid compiler errors.

Exercise: TypePromotion.java

Explore how Java's type promotion affects overload resolution