Skip to main content

How to Compare an Integer with an int in Java (Unboxing)

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

When a Java comparison has an int on one side and an Integer object on the other, Java unboxes the Integer to an int and compares the two numbers. With an Integer on both sides, == compares the objects instead, and an Integer that is null stops the program with a NullPointerException as soon as Java tries to unbox it.

Syntax​

Syntax
primitive == wrapper
wrapper.equals(otherWrapper)
PartWhat it does
primitive == wrapperCompares an int with an Integer. Java unboxes wrapper to an int, so == compares the numbers. !=, <, <=, > and >= compare the numbers too.
wrapper.equals(otherWrapper)Compares the numbers inside two Integer objects. Use it instead of == when both sides are Integer.
primitiveAn int value or variable.
wrapper, otherWrapperInteger objects. Unboxing one that is null throws a NullPointerException.

The Java Tutorials, Autoboxing and Unboxing: "Converting an object of a wrapper type (Integer) to its corresponding primitive (int) value is called unboxing." The Java Language Specification says when == does it: "If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed on the operands" (§15.21.1), and "binary numeric promotion may include unboxing conversion".

The Example​

The example compares an order amount of 120, an int, with a limit of 500, an Integer, using < and ==. Then it compares two Integer objects that both hold 1000, with ==, with equals, and with the int 1000. A changed version sets the limit to null.

Prerequisites​

  • A JDK 11 or later, which can run a single .java file directly. This guide uses OpenJDK 25.0.5.1.

Step 1: Write the Code​

Create a file named Main.java:

Main.java, which sets the Integer limit to 500 and the int amount to 120, prints amount less than limit and amount equals limit, then sets the Integers a and b to 1000 and prints a == b, a.equals(b) and a == 1000.

Main.java
public class Main {

public static void main(String[] args) {
Integer limit = 500; // an Integer object
int amount = 120; // a plain int

// One side is an int, so Java unboxes limit to an int and compares the numbers.
System.out.println(amount < limit);
System.out.println(amount == limit);

Integer a = 1000;
Integer b = 1000;
// Both sides are Integer objects, so == asks whether they're the same object.
System.out.println(a == b);
System.out.println(a.equals(b));
System.out.println(a == 1000);
}
}

Step 2: Run It​

Terminal
java Main.java
Output
true
false
false
true
true

java Main.java compiles the file in memory and runs it. The command is the same in zsh, bash and PowerShell.

Step 3: Compare with a null Integer​

Replace Main.java with a version whose limit is null:

Main.java
public class Main {

public static void main(String[] args) {
Integer limit = null; // no limit was set
int amount = 120;

System.out.println(amount < limit);
}
}
Terminal
java Main.java
Output
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "<local1>" is null
at Main.main(Main.java:7)

A zsh terminal where java Main.java prints true, false, false, true and true, then, for the changed file, stops with a NullPointerException: Cannot invoke java.lang.Integer.intValue() on a variable that is null.

What Happened​

  • amount < limit is true. Java unboxed limit to the int 500 and compared 120 with it.
  • amount == limit is false. With an int on one side, == compared the numbers too, and 120 isn't 500.
  • a == b is false, although both hold 1000. Both sides are Integer objects, so == asked whether they're the same object, and they're two objects. The specification: "At run time, the result of == is true if the operand values are both null or both refer to the same object or array; otherwise, the result is false."
  • a.equals(b) is true, because equals compares the numbers.
  • a == 1000 is true. The int 1000 on the right made Java unbox a and compare numbers.
  • With a null limit, the program stopped. To compare, Java had to unbox limit by calling its intValue() method, and limit was null. The specification: "If r is null, unboxing conversion throws a NullPointerException".

The message says <local1> instead of limit, because java Main.java compiles without the names of local variables. Compiled with javac -g instead, the message named the variable: because "limit" is null.

Common Mistakes​

MistakeWhat happensDo this instead
== between two Integer objectstrue for two objects holding 100, but false for two holding 1000Use equals, or compare with an int on one side
Comparing an Integer that may be null with an intNullPointerException: Cannot invoke "java.lang.Integer.intValue()"Check for null first: limit != null && amount < limit was false for a null limit, with no exception
equals between a Long and an Integerfalse, even when both hold 500, because a Long never equals an IntegerCompare each with a primitive number: big == 500 was true for the Long 500

Troubleshooting​

SymptomLikely causeFix
NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "<local1>" is nullAn Integer that is null was unboxed for a comparisonCheck it for null before you compare
error: incomparable types: Long and Integer== between a Long object and an Integer objectCompare each with a primitive number, or keep both values in the same type
Two Integer objects with the same number aren't ==== compared the objects, not the numbersUse equals

FAQ​

Why is == true for two Integer objects holding 100, but false for 1000?​

Boxing reuses objects for small numbers. The Java Language Specification guarantees it for int values from -128 to 127, and adds: "For other values, the rule disallows any assumptions about the identity of the boxed values on the programmer's part." Two Integer objects holding 100 were ==, and two holding 1000 weren't. Don't rely on either: use equals.

What's the difference between int and Integer?​

int is a primitive type that holds a number and can't be null. Integer is a class whose objects hold an int, and an Integer variable can be null. Collections such as List<Integer> and Map<String, Integer> hold Integer objects, so values that come out of them need these rules.

Conclusion​

With an int on one side, Java unboxes the Integer and compares numbers, with == as well as <. With Integer objects on both sides, == compares the objects, so use equals. And check an Integer for null before comparing it: unboxing null throws a NullPointerException.

Additional Resources​