Why is there a compilation error with Long l = 3
and not with Long l = 3L
?
The primitive data type long
accepts both 3
and 3l
. I understand that 3
is an int
literal - but it can't be assigned to a Long
wrapper object? int
is only 32 bits shouldn't it fit in a 64 bit integer type?
Because there isn't an
int
toLong
widening and autoboxing conversion, autoboxing converts fromlong
toLong
(but first the value must be widened from anint
to along
). You could do3L
as you have, oror
For additional answer:
3L
is equals to(long)3
--> parse it to 3L as it is a long literal3 is a integer literal
3L is a long literal
in a nutshell, they are different from each other that's why you need to parse int to long or vice versa.