Why widening beats both Boxing and var-args in Overloading of a method?

前端 未结 7 2334
不知归路
不知归路 2020-12-06 13:03

I am preparing for a SCJP exam and when studying widening part it\'s given that widening beats both Boxing and Var-args in overloading but there is no clear explanation. Tri

7条回答
  •  忘掉有多难
    2020-12-06 13:17

    class Widen {
        private static widen(long k) {
            System.out.println("Converted to long: " + k);
        }
        private static widen(int ... k) {
            System.out.println("Converted to varargs: " + k);
        }
        private static widen(Integer k) {
            System.out.println("Converted to Integer: " + k);
        }
        public static void main(String ... args) {
            int value = 3;
            widen(value);  // Output: Converted to long: 3
        }
    } 
    

    to solve above mind this:

    widening beats boxing, boxing beats varargs

    the out put will be Converted to long:3

提交回复
热议问题