Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the default type of Java integer literals int instead of long? [closed]

Tags:

I'm confused why Java integer literals default to int instead of long. This seems to cause unnecessary confusion.

First, it requires the programmer to adopt a special syntax (append "L" to literals) when assigning a value to a long that exceeds the maximum int size (2147483647).

long x = 2147483647; // Compiles
long y = 2147483648; // Does not compile
long z = 2147483648L; // Compiles

Second, when using the Long wrapper class, the programmer must always use the long literal notation as explained in this SO question.

Long x = 250; // Does not compile
Long y = 250L; // Compiles

Third, considering that implicit conversion from int literals to the "narrower" data types (short and byte) works just fine in all situations (that I know of), it seems that simply making all integer literals type long would have been the obvious solution... right? Wouldn't this completely remove the need for this odd system of appending "L" to integer literals in special cases?