Programming
Data Conversion
Java
Integer
Long Data Type

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:

java
long value = 123L;
int result = (int) value;
System.out.println(result);

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.

java
long value = 3_000_000_000L;
int result = (int) value;
System.out.println(result);  // overflowed value

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.

java
long value = 123L;
int result = Math.toIntExact(value);
System.out.println(result);

If the value is out of range, Java throws ArithmeticException instead of silently corrupting the value.

java
long value = 3_000_000_000L;
int result = Math.toIntExact(value);  // throws ArithmeticException

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:

java
Long boxed = 42L;
Integer converted = Math.toIntExact(boxed);
System.out.println(converted);

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:

java
Long boxed = null;
Integer converted = Math.toIntExact(boxed);  // NullPointerException

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:

java
1public final class NumberUtils {
2    private NumberUtils() {}
3
4    public static Integer toInteger(Long value) {
5        if (value == null) {
6            return null;
7        }
8        return Math.toIntExact(value);
9    }
10
11    public static void main(String[] args) {
12        System.out.println(toInteger(100L));
13        System.out.println(toInteger(null));
14    }
15}

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 int earlier and was widened to long
  • your schema or protocol guarantees the range
  • performance matters and you already validated the bounds elsewhere

In such situations, this is normal:

java
long count = 25L;
int exactCount = (int) count;

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 Long wrappers

For example:

java
Long rows = 500L;
int pageSize = Math.toIntExact(rows);

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) someLong works, but it silently overflows if the value is out of range.
  • 'Math.toIntExact is the safest standard way to convert long to int in Java.'
  • For Long wrappers, handle null explicitly before conversion.
  • Use direct casts only when the range is already guaranteed.
  • In application code, prefer visible failure over silent truncation.

Course illustration
Course illustration

All Rights Reserved.