What would be the difference between long x = 43
and long x = 43L
in Java?
Do both of them initialize x to be have the long data type?
Do both of them initialize x to be have the long data type?
Absolutely. The type of the variable is entirely determined by the declaration part, not the initialization.
Your first form is logically equivalent to:
long x = (long) 43;
... but there's an implicit conversion from int
to long
, so you don't need to put the cast there.
(In reality the compiler performs the conversion to long
so it's a long
constant in the bytecode, but that's an implementation detail to some extent.)
See more on this question at Stackoverflow