java - int to Long 解析错误

标签 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)
      }   
    } 

testCase - 1 : int - long working fine

testCase - 2 : int to Long throwing an exception

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

最佳答案

当你这样做的时候

  testCase_1(a); 

您传递的是 int 而不是 longwidening primitive conversion正在发生。

第二种情况

testCase_2(a);  

您不能将基元转换为对象。自动装箱/拆箱不起作用,因为 Long 不是 int 的包装器。

关于java - int to Long 解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34587834/

相关文章:

java - Spring RestTemplate 连接超时不起作用

java - HtmlUnit 无法自动登录网站

java - Thread.sleep() 方法如何工作?

java - 如何调用我在某人链接到的 wsdl 文件中看到的方法?

java - 使用带有 java 的 Selenium WebDriver 断言 WebElement 不存在

java - 不要将文本显示与设置的文本连接起来,而是使用 android 资源?

java - 如何在 t :selectOneListbox? 中添加水平滚动条

java - Thymeleaf Spring 环 n 项目

java - 为多个 JUnit 测试类添加一个设置步骤

java - 如何在 JVM 上解析时区字符串 "Pacific Time (US & Canada)"?