I have been learning java basics for quite a while now and I am still learning it. I am learning ambiguity with varArgs in function overloading. But when I tried to write a program to see it happen, I first made a float and int overload, but this does not give me ambiguity error and executes the int function!! And then just to test, I changed one functions argument to String and it totally gives me the error!!
class VarArgsOverlodingAndIssues {
public static void main(String[] com) {
Compiler a = new Compiler();
Compiler b = new Compiler();
a.setYearlyRatings(1,1,1,1,1);
b.setYearlyRatings(1f,1f,1f,1f);
b.setYearlyRatings(); /* this is not giving me the error in int and float*/
}
}
class Compiler {
void setYearlyRatings(int ... data) { // Changed this to boolean and it gave me the error
if(data.length < 1)
System.err.println("The data list passed is emptyf");
for( int i = 0; i < data.length; i++)
System.out.println("Ratings of year " + (i + 1) + ": " + data[i]);
}
void setYearlyRatings(float ... data) {
if(data.length < 1)
System.err.println("The data list passed is emptyint");
for( int i = 0; i < data.length; i++)
System.out.println("Ratings of yearf " + (i + 1) + ": " + data[i]);
}
}
why does it not give me an error in float and int overload?? I have absolutely no idea!! and I am mostly not confident with my questions, I may be asking a wrong question. But i am very very confused .... plzz help!!