Java long Data Type: Size, Range, Examples, Casting & Overflow

0

The long data type is used when an application needs to store whole numbers larger than the range supported by int. It is a signed 64-bit integer type and can represent extremely large values.

For everyday counting and calculations, int is usually sufficient. But when values can grow beyond approximately 2.1 billion, long becomes the safer choice. Common examples include timestamps, large counters, file sizes, database identifiers, and values representing large quantities.

What Is the long Data Type?

The long data type stores signed whole numbers using 64 bits. Its range extends from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

long population = 1400000000L;
long distance = 9876543210L;
long fileSize = 5000000000L;

The L suffix tells Java that the numeric literal should be treated as a long.

Key Point: Java's long is a signed 64-bit integer with a range of approximately -9.22 quintillion to 9.22 quintillion.

Why Do We Need long?

An int can store values only up to 2,147,483,647. That may sound huge, but modern applications can easily encounter values beyond this limit.

Imagine an application that records billions of events, measures time in milliseconds, processes very large files, or generates identifiers across a massive system. An int may eventually become too small.

long totalViews = 50000000000L;
long totalBytes = 9876543210L;

Using long gives these values enough room to grow without overflowing the 32-bit int range.

Syntax of long

long variableName = valueL;

For example:

long population = 1400000000L;
long distance = 25000000000L;
long salary = 5000000000L;

The L suffix is especially important when the literal is too large to be represented as an int.

Range of long

A long uses 64 bits and is signed. Its range is mathematically expressed as -263 to 263 - 1.

Property Value
Data Type long
Category Integer
Size 64 bits
Minimum Value -9,223,372,036,854,775,808
Maximum Value 9,223,372,036,854,775,807
Default Value for a Field 0L
Signed Yes

long Literals

A whole-number literal without a suffix is normally treated as an int if it fits within the int range.

long number = 1000;

This works because the constant value 1000 can be represented as an int and then assigned to the larger long type.

For a large literal outside the int range, use the L suffix.

long number = 3000000000L;

Without the suffix, Java attempts to interpret the literal as an int, which cannot represent that value.

Remember: Use L for large integer literals that need to be treated as long. Using uppercase L is generally clearer than lowercase l.

long Arithmetic

Arithmetic operations involving long values generally produce a long result.

long a = 5000000000L;
long b = 2000000000L;

long sum = a + b;
long difference = a - b;
long product = a * b;

The result remains a long as long as the operation is performed using long operands.

Integer Division with long

Just like int, division between whole-number types produces an integer result. The fractional part is discarded.

long result = 10L / 3L;

System.out.println(result);

The output is 3, not 3.333....

If you need a decimal result, use a floating-point type in the expression.

double result = 10.0 / 3;

System.out.println(result);

long Overflow

Although long has an enormous range, it is still a fixed-size integer. If a calculation exceeds its maximum or minimum value, overflow can occur.

long number = 9223372036854775807L;
number++;

System.out.println(number);

The result wraps around to -9223372036854775808.

This is a useful reminder that even a 64-bit integer is not unlimited. Applications dealing with continuously increasing counters should always consider the maximum possible value.

int to long Conversion

Converting an int to long is a widening conversion and happens automatically.

int number = 500;

long largeNumber = number;

No cast is required because every possible int value can be represented by long.

long to int Conversion

The reverse conversion is potentially unsafe because a long can contain values that an int cannot represent.

long number = 1000L;
int value = (int) number;

An explicit cast is required. If the long value is outside the int range, information can be lost.

long and System.currentTimeMillis()

A classic real-world example of long is time measurement. Java's System.currentTimeMillis() returns the current time measured in milliseconds from the Unix epoch as a long.

long currentTime = System.currentTimeMillis();

System.out.println(currentTime);

A millisecond timestamp is already much larger than what an int can safely represent, which makes long a natural choice.

Common Real-World Uses

  • Large counters and event counts.
  • File sizes and byte counts.
  • Timestamps measured in milliseconds.
  • Large database identifiers.
  • Large population or statistical values.
  • Applications that process very large quantities.
  • Systems where integer values can exceed the int range.

Common Beginner Mistakes

  • Forgetting the L suffix for large long literals.
  • Assuming long can store unlimited values.
  • Converting a large long to int without considering overflow.
  • Expecting long / long to produce a decimal result.
  • Using int for values that are known to exceed its range.
  • Using lowercase l in large numeric literals, which can be visually confused with the number 1.

Best Practices

  • Use long when an integer value may exceed the int range.
  • Use uppercase L for explicit long literals.
  • Check possible overflow when working with very large calculations.
  • Avoid narrowing a long to int unless the range is known to be safe.
  • Choose int for ordinary integer values and reserve long for values that genuinely need its larger range.

Interview Insights

Question: What is the size and range of long in Java?

Answer: long is a signed 64-bit integer type with a range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Question: Why is L used with long values?

Answer: The L suffix tells the compiler that the numeric literal should be treated as a long literal.

Question: Can a long be automatically converted to int?

Answer: No. It is a narrowing conversion and requires an explicit cast because a long may contain values outside the int range.

long Data Type: Quick Revision

Property Details
Type long
Category Integer
Size 64 bits
Minimum -9,223,372,036,854,775,808
Maximum 9,223,372,036,854,775,807
Default Field Value 0L
Signed Yes
Literal Suffix L
Example long population = 1400000000L;

The long data type gives Java applications a huge range for whole-number values while keeping the simplicity of integer arithmetic. The key lesson is simple: use int for normal integer values, but choose long when the data can grow beyond the int limit. Always pay attention to literal suffixes, conversions, and overflow when working with large numbers.

Post a Comment

0Comments
Post a Comment (0)