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?
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With