C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: These add() methods are actually separate methods, with no shared code. They just share the method name.
Tip 2: With overloading, we can use simpler names (like "add") that are easier to remember, but that have separate implementations.
Java program that uses overloaded method
public class Program {
static int add(int value1, int value2) {
// This method handles two int arguments.
return value1 + value2;
}
static int add(int value1, int value2, String value3) {
// This overload handles three arguments.
return value1 + value2 + value3.length();
}
public static void main(String[] args) {
// Call overloaded add methods.
int total = add(5, 5);
total += add(10, 10, "carrot");
System.out.println(total);
}
}
Output
36
Branch: The branch (from the if-statement) must be evaluated at runtime. This imposes a performance penalty.
IfThus: This program has fewer methods than the previous one, but will likely execute slower.
Java program that uses no overloaded methods
public class Program {
static int add(int value, int value2, String value3) {
// This method handles a null String with a branch.
// ... It could be overloaded.
if (value3 == null) {
return value + value2;
} else {
return value + value2 + value3.length();
}
}
public static void main(String[] args) {
// Call add() method twice.
int total = add(5, 5, null);
total += add(10, 10, "carrot");
System.out.println(total);
}
}
Output
36
Tip: As with methods, these overloaded constructors are separate. But we could call into a unified "initialize" method.
Java program that uses overloaded constructor
class Element {
int size;
String color;
public Element(int size, String color) {
this.size = size;
this.color = color;
}
public Element(int size) {
this.size = size;
this.color = "blank";
}
public Element(String color) {
this.size = 0;
this.color = color;
}
public String toString() {
// This puts field values into a String.
return "size = " + this.size + ", color = " + this.color;
}
}
public class Program {
public static void main(String[] args) {
// Create with two arguments.
Element e = new Element(10, "blue");
System.out.println(e);
// Use just one argument (size or color).
e = new Element(50);
System.out.println(e);
e = new Element("red");
System.out.println(e);
}
}
Output
size = 10, color = blue
size = 50, color = blank
size = 0, color = red
Version 1: With the add() overloads, we know what arguments are being used—we can act directly on them.
Version 2: We call the addNoOverload method, which uses an if-statement branch to determine the proper return expression.
Result: Calling the overloaded add methods is faster than using addNoOverload. Fewer branches are executed.
Java program that benchmarks overloaded methods
public class Program {
static int add(int value1, int value2) {
return value1 + value2;
}
static int add(int value1, int value2, String value3) {
return value1 + value2 + value3.length();
}
static int addNoOverload(int value, int value2, String value3) {
if (value3 == null) {
return value + value2;
} else {
return value + value2 + value3.length();
}
}
public static void main(String[] args) {
long t1 = System.currentTimeMillis();
// Version 1: use overloaded methods.
for (int i = 0; i < 1000000000; i++) {
int result = add(10, i) + add(5, i, "carrot");
if (result == 0) {
return;
}
}
long t2 = System.currentTimeMillis();
// Version 2: use method with if-statement.
for (int i = 0; i < 1000000000; i++) {
int result = addNoOverload(10, i, null)
+ addNoOverload(5, i, "carrot");
if (result == 0) {
return;
}
}
long t3 = System.currentTimeMillis();
// ... Times.
System.out.println(t2 - t1);
System.out.println(t3 - t2);
}
}
Output
289 ms, add
329 ms, addNoOverload