What will be printed by the following code?

public class Main {
    public static void var(Integer x, int y) {
        System.out.println("Integer int");
    }

    public static void var(Object... x) {
        System.out.println("Object");
    }

    public static void var(int... x) {
        System.out.println("int... x");
    }

    public static void var(Integer... x) {
        System.out.println("Integer...");
    }

    public static void main(String... args) { 
        int i = 0;
        Integer i2 = 127;
        var(i, i2);
    }
}
Explanation
When you call the overloaded (overloaded) compiler methods:
1) first it looks for a method parameter types that are exactly the same with arguments;
If this method is not found
2) then looks for a method with parameters that can be obtained via a call admissible transformations (widening, boxing + widening, unboxing + widening);
if this method is not found
3) then methods are analyzed with a variable number of arguments.

In this example, the compilation stops at the second step (conversions trigger int → Integer, Integer → int).

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Продолжайте изучать
тесты по Java
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты