int to Long解析错误

Shi*_*rty 4 java

  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)
      }   
    } 
Run Code Online (Sandbox Code Playgroud)

testCase - 1:int - 长期正常工作

testCase - 2:int to L ong抛出异常

为什么testCase_2()方法抛出编译异常?

Sur*_*tta 10

当你这样做

  testCase_1(a); 
Run Code Online (Sandbox Code Playgroud)

你传递的是一个int而不是一个long,widening primitive conversion正在发生.

在第二种情况下

testCase_2(a);  
Run Code Online (Sandbox Code Playgroud)

您无法将基元转换为对象.自动装箱/拆箱不起作用,因为Long它不是包装机int.