If i have this:
class MyClass {
double calcAverage(double marks1, int marks2) {
return (marks1 + marks2)/2.0;
}
double calcAverage(int marks1, double marks2) {
return (marks1 + marks2)/2.0;
}
public static void main(String args[]) {
MyClass myClass = new MyClass();
myClass.calcAverage(2, 3);
}
}
Compiler can't determinate which overloaded method "calcAverage" should be called. I understand this. So I was exploring some examples to see if I understand fully this idea. I came to this example and I am confused:
public class MyClass {
double metoda(int marks1, double marks2){
System.out.println("int + double");
return (marks1 + marks2)/2.;
}
double metoda(char marks1, double marks2){
System.out.println("char + double");
return (marks1 + marks2)/2.;
}
double metoda(char marks1, int marks2){
System.out.println("char + int");
return (marks1 + marks2)/2.;
}
public static void main(String args[]) {
MyClass ss = new MyClass();
char a = 2;
char b = 3;
double var = ss.metoda(a, b);
}
}
All 3 of this methods can accept "char, char" arguments, but still, there is NO compilation error. If I run this program, metoda(char .., int..) is executed. If i delete this method and execute program again, then metoda(char .., double..) is called. I don't understand why this is not compilation error and how does compiler knows which method to call.