为什么main方法没有调用第一个方法?
public class Test {
public static void printValue(byte...b) {
System.out.println("long");
}
public static void printValue(int i, int j, int k) {
System.out.println("int");
}
public static void main(String... args) {
byte b = 9;
printValue(b,b,b);
}
}
为什么输出是int
?
最佳答案
在选择最合适的方法时,编译器会首先选择加宽,然后是装箱,最后,如果都失败了,Var-args 。在您的情况下,它会将所有 bytes
加宽到 ints
以获取匹配方法,其中包含 3 个 int
类型参数。
有关这方面的更多详细信息,请参阅 JLS §15.12.2 .
关于java - 为什么输出是int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22435930/