Methods with the same name
You've called one method name with all kinds of values since module 1: System.out.println("Hi"), System.out.println(42), System.out.println(3.5) and System.out.println(). That isn't one method that takes anything: println is several methods with the same name, one for each type of parameter. Like constructors, methods can be overloaded, and the methods of your own classes can be too:
public class Main {
public static void main(String[] args) {
LabelPrinter printer = new LabelPrinter();
printer.print("Flour");
printer.print(42);
printer.print("Sugar", 2);
System.out.println("Labels printed: " + printer.getPrinted());
}
}public class LabelPrinter {
private int printed;
public LabelPrinter() {
this.printed = 0;
}
public void print(String text) {
System.out.println("[ " + text + " ]");
this.printed = this.printed + 1;
}
public void print(int number) {
this.print("No. " + number);
}
public void print(String text, int copies) {
for (int i = 0; i < copies; i++) {
this.print(text);
}
}
public int getPrinted() {
return this.printed;
}
}[ Flour ]
[ No. 42 ]
[ Sugar ]
[ Sugar ]
Labels printed: 4- Three methods are called
print. Java picks one by the values in the call: a String runsprint(String text), an int runsprint(int number), and a String and an int runprint(String text, int copies). - The other two call
print(String text)instead of printing on their own, so how a label looks, and the counting, are written once. It's the same idea asthis(...)in the last step, as an ordinary method call. - Overloading suits methods that do the same thing with different kinds of values. Methods that do different things should get different names.
Which one Java picks
Java compares the values in the call with each method's parameters: how many there are, and their types, in order. What decides is the type of each value, not what it looks like when printed. Static methods can be overloaded too, so here's the idea in Main alone:
public class Main {
public static void main(String[] args) {
show(5);
show(5.0);
show("5");
show(2 + 3);
show(5 / 2.0);
}
public static void show(int value) {
System.out.println("int: " + value);
}
public static void show(double value) {
System.out.println("double: " + value);
}
public static void show(String value) {
System.out.println("String: " + value);
}
}
int: 5
double: 5.0
String: 5
int: 5
double: 2.55.0is a double and"5"is a String, so each goes to its own method.2 + 3is an int, and5 / 2.0a double, so the type of the result decides.- An int also fits a
doubleparameter, just asdouble distance = 5;works (module 3). Withoutshow(int value),show(5)would runshow(double value)and printdouble: 5.0. With both there, Java takes the one that fits exactly.
The return type doesn't count
Methods, like constructors, differ only by their parameter types. Parameter names don't count, and neither does the return type. Say you want a method that returns a label's text instead of printing it, and you call it print too:
public class Main {
public static void main(String[] args) {
LabelPrinter printer = new LabelPrinter();
printer.print("Salt");
}
}public class LabelPrinter {
public void print(String text) {
System.out.println("[ " + text + " ]");
}
public String print(String label) {
return "[ " + label + " ]";
}
}LabelPrinter.java:6: error: method print(String) is already defined in class LabelPrinter
public String print(String label) {
^
1 errorA call such as printer.print("Salt"); doesn't have to use the value a method returns, so nothing in it could tell the two apart. A method that does something different gets a name of its own, such as labelFor(String text).