ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

Java Tutorial• 6 min read

Methods, Parameters & Method Overloading

Pass-by-value semantics, return types, static methods, and overloading signatures

Methods encapsulate reusable logic. Method Overloading allows multiple methods in the same class to share the same name with different parameter signatures.

Method Signatures & Pass-by-Value

Pass-by-Value: Java is strictly 100% pass-by-value. For primitives, copies of values are passed. For objects, copies of the object reference pointers are passed.

Method Overloading: Multiple methods share the same name but differ in number, type, or order of parameters. Return type alone does not differentiate overloaded methods.

Method Overloading Example
public class Calculator {
    // Overload 1: Two integers
    public static int add(int a, int b) {
        return a + b;
    }

    // Overload 2: Three integers
    public static int add(int a, int b, int c) {
        return a + b + c;
    }

    // Overload 3: Double precision
    public static double add(double a, double b) {
        return a + b;
    }

    public static void main(String[] args) {
        System.out.println(add(5, 10));       // calls int overload
        System.out.println(add(2.5, 3.5));   // calls double overload
    }
}
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Can two Java methods be overloaded if they have identical parameter lists but different return types?