Core Java Fundamentals ยท Methods
Several methods can share a name if their parameters differ. Java picks the right one from the arguments you pass.
public class OverloadingDemo {
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) {
int two = add(1, 1);
double half = add(0.25, 0.25);
int three = add(1, 1, 1);
System.out.println(two + " " + half + " " + three);
}
}add() has finished, so its variables are gone.two.add() has finished, so its variables are gone.half.add() has finished, so its variables are gone.three.2 0.5 3.