Why does EnumSet have many overloaded “of” methods?

后端 未结 4 1976
[愿得一人]
[愿得一人] 2021-01-01 10:55

While going through the EnumSet of method, I have seen multiple overloaded implementations of of method:

publ         


        
4条回答
  •  有刺的猬
    2021-01-01 11:06

    varags creates an array, that is when we call

    void x(int...x) {...}
    ..
    x(1);
    

    Compiler replaces last line with this:

    x(new int[] {1});
    

    It will not happen if we have an overloaded method with 1 arg:

    void x(int...x) {...}
    void x(int x) {...}
    

    Then compiler will choose the second method.

提交回复
热议问题