Method Overloading in Java
Method overloading lets several methods share the same name as long as their parameter lists differ — in count, in type, or both — with the compiler picking which one to actually call based on the arguments supplied at each call site. add(int, int) and add(double, double) can coexist as two entirely separate methods that happen to share a name, and Java resolves which one a given call means at compile time, not at runtime.
The return type alone is never enough to distinguish overloads — two methods with identical parameter lists but different return types are a compile error, since the compiler has no way to choose between them based on the call site alone, before it even knows what the caller intends to do with the result.
Same name, different parameters
Overloaded methods share a name but differ in their parameter list's count, types, or both.
Resolved at compile time
The compiler picks which overload a given call means based on the argument types supplied, before the program ever runs.
Return type alone isn't enough
Two methods with identical parameters but different return types cannot coexist — that's a compile error, not a valid overload.
Widening applies during resolution
An int argument can match a method expecting a double parameter through automatic widening if no exact int overload exists.
A common, everyday example
System.out.println itself is overloaded dozens of times, once for each type it can print directly.
public class OverloadDemo { static int add(int a, int b) { return a + b; } static double add(double a, double b) { return a + b; } public static void main(String[] args) { System.out.println(add(2, 3)); System.out.println(add(2.5, 3.5)); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.