Can an int be null in Java?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Java, whether a value can be null depends on whether it is a primitive or an object reference. That distinction matters because int is a primitive type, while Integer is a reference type. If you confuse them, you usually end up with compile-time errors or a surprise NullPointerException during unboxing.
Primitive int Cannot Be null
A primitive int always stores an actual numeric value. It does not represent “no value,” and Java does not allow assigning null to it.
That is the simplest rule: int is a value, not a nullable reference.
Primitives also have default values for fields. If an instance field of type int is not assigned explicitly, it defaults to 0. Local variables are different: they must be initialized before use.
Integer Can Be null
If you need an integer-like value that may be absent, use Integer instead.
Integer is a wrapper class, so it behaves like any other object reference. That means it can point to an Integer instance or it can be null.
This is why frameworks, databases, collections, and JSON libraries often use Integer instead of int when missing values are possible.
Autoboxing Makes the Difference Easier To Miss
Java automatically converts between int and Integer in many situations. That feature is convenient, but it can hide where null becomes dangerous.
The risky case is unboxing a null reference:
This compiles, but it fails at runtime because Java tries to extract an int value from a null Integer.
When To Use int vs Integer
Use int when:
- the value is always required
- '
0is a meaningful default' - you want less overhead and simpler logic
Use Integer when:
- the value may be missing
- you need to store it in collections with nullable semantics
- a framework expects an object reference
- you need to distinguish between “unset” and an actual numeric value
A common example is a database column that may contain SQL NULL. Mapping that to int loses the distinction between NULL and 0, while Integer preserves it.
Safer Patterns for Optional Numeric Values
If absence is part of the domain, be explicit. One option is to check for null before unboxing.
Another option is to model absence directly with OptionalInt or Optional<Integer> depending on the API shape you need.
That makes the “maybe present” behavior more obvious than passing null around.
Common Pitfalls
- Treating
intandIntegeras interchangeable when nullability matters. - Unboxing a
nullIntegerand getting a runtimeNullPointerException. - Using
intfor data that can legitimately be absent, such as nullable database columns. - Using
Integereverywhere even when the value is required andnullshould never appear.
Summary
- Primitive
intcannot benullin Java. - Wrapper
Integercan benullbecause it is an object reference. - Autoboxing and unboxing make the distinction easy to miss.
- Unboxing a
nullIntegerthrowsNullPointerException. - Use
intfor required values andIntegeronly when absence is part of the model.

