Long vs Integer, long vs int, what to use and when?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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:
intis 32-bit primitivelongis 64-bit primitiveIntegerwrapsintas objectLongwrapslongas 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:
intapprox plus or minus 2.1 billionlongapprox 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.
When to use primitives
Prefer primitives for:
- arithmetic loops
- numerical arrays
- tight performance paths
- values that are always present
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
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.
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.
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
intfor 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
IntegerorLong. - 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
longproactively when range can exceedint. - Use
IntegerandLongwhere 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.
Related reading
- Loop doesn't see value changed by other thread without a print statement
- Lossless JPEG Rotate 90/180/270 degrees in Java?
- Mac OS X and multiple Java versions
- Machine learning challenge diagnosing program in java/groovy datamining, machine learning
- Main differences between SOAP and RESTful web services in Java
- make arrayList.toArray return more specific types
- Make Hibernate ignore instance variables that are not mapped
- Make Maven to copy dependencies into target/lib

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.