Same method name, different parameters — Java picks the right one based on what you pass in.
A class can have multiple methods with the same name as long as their parameter lists are different. Java uses the arguments you pass to decide which version to call.
Here is a Calculator class with four overloaded add methods:
intdoubleintString
Java determines which overload to call by matching the method signature =
name + parameter types. Click a method call to see which method it resolves to.
Pick argument types and values, then call add(). Watch which overload Java selects.
Methods must share the same name but differ in their parameter list — number of parameters, types, or order of types.
Two methods that differ only by return type will NOT compile. Java cannot tell them apart from the call site.
add(int, String) is a different signature from add(String, int) because the types appear in a different order.
int add(int a, int b) and double add(int a, int b) will not compile —
the parameter lists are identical, so Java sees them as the same method.
System.out.println() accepts
int, double, String, boolean, and more —
that's method overloading in action. One name, many parameter types.
Click through the examples in Section 2 and experiment in Section 3 to build intuition.