The long Type: Extended Range Integers
What you'll learn: You'll discover Java's long type, which stores much larger whole numbers than int can handle.
Why long Exists
Remember how int works great for everyday whole numbers? Sometimes you need to store values that are absolutely enormous—think astronomical distances, milliseconds since 1970, or national debts. That's where long comes in.
What is long?
The long type is a 64-bit integer. While int uses 32 bits, long doubles that storage space, giving you a massively expanded range:
- Minimum value: -9,223,372,036,854,775,808
- Maximum value: 9,223,372,036,854,775,807
That's over 9 quintillion in either direction!
Declaring long Variables
Just like you declare int variables, you can declare long variables:
long worldPopulation;
long distanceToMoon = 384400000L;
Important detail: Notice the L at the end of 384400000L? When you write a large number literal that you want Java to treat as a long, you add an uppercase L (or lowercase l, but uppercase is clearer). Without it, Java assumes you mean int, which can cause errors if the number is too large.
Think of It This Way
If int is a standard filing cabinet drawer that holds typical documents, long is a warehouse storage unit. You don't always need the warehouse, but when you're dealing with massive data, nothing else will do.
Key Takeaway: The long type stores 64-bit integers ranging from about -9.2 quintillion to +9.2 quintillion, perfect for values that exceed int's limits. Use the L suffix for long literals.