What is the difference between typing long x = 43 and long x = 43L in Java?

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?

Jon Skeet
people
quotationmark

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.)

people

See more on this question at Stackoverflow