Method Overloading means writing multiple methods with the same name but different parameter counts or types. Java looks at the arguments you pass and figures out which method to call. It's handy when you want the same name to handle different situations.
java
public class Main {
static int add(int a, int b) {
return a + b;
}
static double add(double a, double b) {
return a + b;
}
static int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
System.out.println(add(5, 3));
System.out.println(add(2.5, 4.2));
System.out.println(add(1, 2, 3));
}
}The method name add is the same, but the parameter signatures differ. When the Java compiler sees add(5, 3) it picks the int version, and for add(2.5, 4.2) it picks the double version.
You should see
8 6.7 6Real-World Use
You'll see overloading all over libraries that want one operation — like print, calculate, create, or format — to work with different kinds of input.