Java Tutorial
Java Method Overloading
Overloading means the same method name with different parameter lists. The compiler picks the match from the arguments you pass.
Same name, different parameters
You can declare several methods named print or add as long as their parameter lists differ in number or type. At the call site you write one name; the compiler chooses the right version.
Example
public class Main {
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)); // int version
System.out.println(add(2.5, 3.1)); // double version
}
}Different arity counts too
Changing how many parameters you take is also overloading. That is how you offer a short form and a longer form without inventing awkward names like addTwo and addThree.
Example
public class Main {
static void greet(String name) {
System.out.println("Hi, " + name);
}
static void greet(String name, int times) {
for (int i = 0; i < times; i++) {
System.out.println("Hi, " + name);
}
}
public static void main(String[] args) {
greet("Ada");
greet("Ada", 3);
}
}Return type alone is not enough
Two methods that differ only in return type are not overloads — they are a compile error. The signature that matters is the name plus the parameter types. The return type is not part of that choice.
Example — this does not compile
static int value() { return 1; }
static double value() { return 1.0; } // error: same signatureOverloading is chosen at compile time from the argument types. Overriding (inheritance) is chosen atruntime from the object's real class. Do not mix the two ideas.
Try It Yourself
Exercise: Overload max so it works for two ints and for threeints. Print both results from main.
Show solution
public class Main {
static int max(int a, int b) {
return a > b ? a : b;
}
static int max(int a, int b, int c) {
return max(max(a, b), c);
}
public static void main(String[] args) {
System.out.println(max(3, 7)); // 7
System.out.println(max(3, 7, 5)); // 7
}
}The three-argument version reuses the two-argument one — a common overload pattern.
Key Takeaways
- Overloads share a name but differ in parameter number or types.
- The compiler picks the match from the arguments at the call site.
- Return type alone cannot distinguish two methods.
- Overloading is compile-time; overriding (later) is runtime.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Maths
Area for different shapes
Same method name, different parameter lists. A square needs one side; a rectangle needs two. Overloading keeps both under area.
A = s² or A = w × h
Example
public class Main {
static int area(int side) { return side * side; }
static int area(int w, int h) { return w * h; }
public static void main(String[] args) {
System.out.println(area(4));
System.out.println(area(3, 5));
}
}