class Test{
static void testCase_1(long l){System.out.println("Long");}
static void testCase_2(Long l){System.out.println("Long");}
public static void main(String args[]){
int a = 30;
testCase_1(a); // Working fine
testCase_2(a); // Compilation time error
//Exception - The method testCase_2(Long) in the type Test is not applicable for the arguments (int)
}
}
testCase - 1 : int - long working fine
testCase - 2 : int to Long throwing an exception
为什么 testCase_2() 方法抛出编译异常?
最佳答案
当你这样做的时候
testCase_1(a);
您传递的是 int
而不是 long
,widening primitive conversion
正在发生。
第二种情况
testCase_2(a);
您不能将基元转换为对象。自动装箱/拆箱不起作用,因为 Long
不是 int
的包装器。
关于java - int to Long 解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34587834/