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 finetestCase - 2: int to L ong throws an exception
testCase - 1: int - long working fine
testCase - 2: int to L ong throws an exception
Why does the testCase_2 () method throw an exception for compilation?
When you do
testCase_1(a);
you pass intinstead long, widening primitive conversion.
int
long
widening primitive conversion
In the second case
testCase_2(a);
you cannot convert a primitive to an object. Autoboxing / unboxing does not work because it is longnot a wrapper int.
When called testCase_1(a), it is aautomatically converted from intto long.
testCase_1(a)
a
int long (, ), int long. .
testCase_2(a), , (cast autobox) int long. , , .
testCase_2(a)
:
testCase_2(Long.valueOf(a));
.
Source: https://habr.com/ru/post/1622703/More articles:Javascript: how to make sure window.open returns the same window if it is already open - javascriptHow to increase SQL query execution time in SSMS? - sqlКак я могу создать границу речевого пузыря для пользовательской иконки Google Marker с помощью Picasso? - androidThe free Nhibernate programming dictionary creates an extra table - dictionaryconflict strings.xml in android manifest - androidAnnotation check does not work in ASP.net MVC - c #Regular expression for matching account numbers - regexHow to get applicationId of a Spark application deployed to YARN in Scala? - scalaspark Direct mode, how to get applicationId from spark-submit - hadoopHTTP_COOKIE IIS Server Variable по неизвестной причине - asp.netAll Articles