Java
int
programming
data types

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.

java
1public class PrimitiveExample {
2    public static void main(String[] args) {
3        int count = 42;
4        System.out.println(count);
5
6        // int invalid = null; // does not compile
7    }
8}

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.

java
1public class WrapperExample {
2    public static void main(String[] args) {
3        Integer count = null;
4        System.out.println(count);
5    }
6}

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.

java
1public class AutoBoxingExample {
2    public static void main(String[] args) {
3        Integer boxed = 10;   // boxing
4        int plain = boxed;    // unboxing
5        System.out.println(plain);
6    }
7}

The risky case is unboxing a null reference:

java
1public class NullUnboxingExample {
2    public static void main(String[] args) {
3        Integer boxed = null;
4        int plain = boxed; // throws NullPointerException
5        System.out.println(plain);
6    }
7}

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
  • '0 is 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.

java
1public class SafeNullHandling {
2    public static void main(String[] args) {
3        Integer maybeCount = null;
4        int count = maybeCount != null ? maybeCount : 0;
5        System.out.println(count);
6    }
7}

Another option is to model absence directly with OptionalInt or Optional<Integer> depending on the API shape you need.

java
1import java.util.OptionalInt;
2
3public class OptionalExample {
4    public static void main(String[] args) {
5        OptionalInt count = OptionalInt.empty();
6        System.out.println(count.isPresent());
7    }
8}

That makes the “maybe present” behavior more obvious than passing null around.

Common Pitfalls

  • Treating int and Integer as interchangeable when nullability matters.
  • Unboxing a null Integer and getting a runtime NullPointerException.
  • Using int for data that can legitimately be absent, such as nullable database columns.
  • Using Integer everywhere even when the value is required and null should never appear.

Summary

  • Primitive int cannot be null in Java.
  • Wrapper Integer can be null because it is an object reference.
  • Autoboxing and unboxing make the distinction easy to miss.
  • Unboxing a null Integer throws NullPointerException.
  • Use int for required values and Integer only when absence is part of the model.

Course illustration
Course illustration

All Rights Reserved.