Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

int to Long parsing error

Tags:

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

Why testCase_2() method throwing an compilation exception?

like image 550
Shiladittya Chakraborty Avatar asked Dec 11 '22 19:12

Shiladittya Chakraborty


1 Answers

When you do

  testCase_1(a); 

you are passing an int instead of a long, widening primitive conversion is happening.

In the second case

testCase_2(a);  

you cannot convert a primitive to an object. Autoboxing/unboxing doesn't work because Long is not a wrapper of int.

like image 140
Suresh Atta Avatar answered Dec 23 '22 23:12

Suresh Atta