Convert Long into Integer
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Java, converting a Long or long into an Integer or int is easy syntactically but risky semantically. The main issue is not how to write the cast. The main issue is whether the 64-bit value actually fits inside the 32-bit int range.
Primitive Conversion with a Cast
For primitive values, the direct syntax is:
This compiles and runs, but it is only safe when the long value is between Integer.MIN_VALUE and Integer.MAX_VALUE.
If the value is outside that range, Java truncates the higher bits and you get a numerically different result.
That is why a plain cast is acceptable only when you already know the value fits.
The Safer Option: Math.toIntExact
If you want correctness rather than silent truncation, use Math.toIntExact.
If the value is out of range, Java throws ArithmeticException instead of silently corrupting the value.
For most business code, this is the better default because failures are visible.
Wrapper Conversion: Long to Integer
If you are working with wrapper types rather than primitives, there are two issues:
- range overflow
- possible
null
Example:
This works because Java unboxes the Long to long, applies Math.toIntExact, and then boxes the int result into Integer.
But this fails if boxed is null:
So when wrappers are involved, null handling matters just as much as numeric range.
Defensive Conversion Pattern
Here is a small utility method that handles both concerns clearly:
This preserves nulls and still rejects overflow.
When a Cast Is Still Reasonable
There are cases where a cast is perfectly fine:
- the value came from an
intearlier and was widened tolong - your schema or protocol guarantees the range
- performance matters and you already validated the bounds elsewhere
In such situations, this is normal:
The key is not avoiding casts entirely. It is avoiding unverified casts on values that may exceed the int range.
Conversion in Collections and APIs
This issue often appears around:
- database IDs
- JSON deserialization
- counters returned as
long - framework APIs that use
Longwrappers
For example:
If those values come from untrusted input or external systems, Math.toIntExact is usually the better guardrail.
Why Java Does Not Do This Automatically
Java allows widening conversions automatically, such as int to long, because no information is lost. Narrowing conversions like long to int are different because data can be lost, so Java requires explicit syntax or an explicit helper method.
That language rule is a feature. It forces you to acknowledge that the conversion may not be safe.
Common Pitfalls
Using (int) value on a large long silently overflows and produces a wrong answer without warning.
Treating Long and long as interchangeable can lead to NullPointerException when unboxing happens implicitly.
Converting external IDs or counters without validating their range creates bugs that are hard to detect later, because the code still compiles and runs.
Using Integer.valueOf((int) value) does not make the conversion safer. It only boxes the potentially overflowed primitive result.
Summary
- A cast like
(int) someLongworks, but it silently overflows if the value is out of range. - '
Math.toIntExactis the safest standard way to convertlongtointin Java.' - For
Longwrappers, handlenullexplicitly before conversion. - Use direct casts only when the range is already guaranteed.
- In application code, prefer visible failure over silent truncation.

