0

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!!

mitesh
  • 235
  • 3
  • 11
  • Have you check [this post about ambiguity](http://stackoverflow.com/questions/12879910/varargs-in-method-overloading-in-java) ? Not complitly a duplicate but it is a good start to understand. – AxelH Apr 10 '17 at 13:38
  • Where do you see an ambiguity here? See also http://stackoverflow.com/questions/27874380/ambiguous-varargs-methods – Henry Apr 10 '17 at 13:39
  • Word parts in names like `VarArgsOverlodingAndIssues` should be spelled correctly. Misspellings like that make code more difficult to maintain. – Lew Bloch Apr 10 '17 at 17:09

0 Answers0