Long Literals and the L Suffix
What you'll learn: When and why to append L or l to numeric literals so Java treats them as long instead of int.
The Problem: Java's Default Assumption
By default, when you write a whole number literal like 42 or 1000000, Java assumes it's an int. This works fine for values within int range (about -2 billion to +2 billion), but what if you need a larger number?
If you try to assign a literal beyond int range directly to a long variable, the compiler gets confused—it tries to treat the literal as an int first, which overflows before assignment happens.
The Solution: The L Suffix
To tell Java "this literal is a long," append an L or l to the end:
long bigNumber = 3000000000L; // Must use L suffix
long small = 42L; // Optional but valid
Without the L, 3000000000 would cause a compilation error because it's too large for an int.
Lowercase vs Uppercase
Both l and L work, but always use uppercase L. The lowercase l looks too similar to the digit 1, making code hard to read:
long bad = 1000000l; // Looks like "10000001"
long good = 1000000L; // Clearly "1000000 Long"
When Do You Need It?
- Required: When the literal exceeds
intrange (larger than 2,147,483,647 or smaller than -2,147,483,648) - Optional: For smaller values assigned to
longvariables—Java auto-convertsinttolongsafely
Key Takeaway: Use the L suffix on literals larger than int range or when you explicitly want a long type; always prefer uppercase L for readability.