Data Types
Programming
Java
Integer
Long

Long vs Integer, long vs int, what to use and when?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Choosing between int, long, Integer, and Long is not just a syntax preference. The right type affects range safety, memory usage, null handling, and performance under high load. Most production bugs come from overflow assumptions or accidental boxing, not from forgetting type names.

Core Sections

Primitive versus boxed types

In Java:

  • int is 32-bit primitive
  • long is 64-bit primitive
  • Integer wraps int as object
  • Long wraps long as object

Primitive types are faster and smaller in memory for arithmetic-heavy paths. Boxed types are needed for generics and nullability.

Range and overflow decisions

Numeric range:

  • int approx plus or minus 2.1 billion
  • long approx plus or minus 9.22 quintillion

If counters can exceed int, choose long early. Overflow in Java primitives is silent by default and can corrupt logic.

java
1int a = Integer.MAX_VALUE;
2int b = a + 1;
3System.out.println(b); // overflow: negative value
4
5long c = (long) Integer.MAX_VALUE + 1;
6System.out.println(c); // correct long value

When to use primitives

Prefer primitives for:

  • arithmetic loops
  • numerical arrays
  • tight performance paths
  • values that are always present
java
1long total = 0L;
2for (int i = 0; i < 1_000_000; i++) {
3    total += i;
4}
5System.out.println(total);

This avoids boxing overhead and reduces GC pressure.

When to use boxed types

Use Integer or Long when:

  • storing in generic collections
  • representing missing value with null
  • using APIs requiring objects
java
List<Long> ids = new ArrayList<>();
ids.add(100L);
ids.add(null); // possible only with boxed type

Boxed types are convenient but introduce nullable-state complexity.

Autoboxing and unboxing traps

Java automatically boxes and unboxes values, which can hide costs and null risks.

java
1Integer x = null;
2// int y = x; // NullPointerException during unboxing
3
4Long m = 100L;
5Long n = 200L;
6Long sum = m + n; // unbox, add primitive, box result

In hot loops, repeated boxing can create unnecessary allocations.

Equality semantics differences

Primitive equality compares values directly. Boxed equality can be tricky with object identity.

java
1Integer i1 = 128;
2Integer i2 = 128;
3System.out.println(i1 == i2);      // false in many cases
4System.out.println(i1.equals(i2)); // true

Use .equals for boxed numeric value comparison.

Practical decision guide

Use int when:

  • range is safely bounded
  • value never null
  • high-volume arithmetic is required

Use long when:

  • ids, timestamps, counters may exceed int
  • range uncertainty exists

Use boxed types when:

  • null is meaningful state
  • collection or framework APIs require objects

Persistence and API boundaries

Database and JSON contracts often drive type choice. If backend id column is 64-bit, mapping to int in application code is risky. Keep type consistency across storage, API schema, and domain model.

For external APIs, prefer explicit schema docs that state integer size assumptions.

Performance perspective

Modern JVMs optimize many cases well, but primitive-heavy numeric code still benefits from reduced allocations. If performance matters, profile before and after type changes with realistic workload. Type choice should be measurable, not speculative. Document these choices in coding standards so teams avoid inconsistent type decisions across services. Also review serialization layers, because a numeric type change in one service can silently break downstream consumers if contracts are loosely validated.

Common Pitfalls

  • Choosing int for counters that can exceed 2.1 billion over time.
  • Using boxed types in tight loops and creating avoidable allocation pressure.
  • Forgetting null checks before unboxing Integer or Long.
  • Comparing boxed values with == instead of .equals.
  • Mismatching application numeric types with database or API integer width.

Summary

  • Use primitives for speed and memory efficiency when null is not needed.
  • Use long proactively when range can exceed int.
  • Use Integer and Long where generics or nullable semantics are required.
  • Watch for autoboxing costs and unboxing null exceptions.
  • Align numeric types across code, storage, and API contracts to avoid hidden bugs.

Course illustration
Course illustration

All Rights Reserved.