In Java, java.lang.Integer does two jobs. First, it is the wrapper class for the primitive type int: an Integer object holds exactly one 32-bit int value, so numbers can live in collections and be null. Second, it is a toolbox of static methods that convert and manipulate int values: parseInt(), toBinaryString(), compare(), bitCount(), max(), and many more.
The class is final, extends Number, implements Comparable<Integer>, and lives in java.lang, so it needs no import. An Integer is immutable: once created, its value never changes. The class also defines constants such as Integer.MAX_VALUE (2147483647), MIN_VALUE (-2147483648), SIZE (32 bits), and BYTES (4). The Integer Methods index lists every guide.
Static helpers, instance basics.
Most methods are called on the class: Integer.parseInt(text). A handful are called on an Integer object: intValue(), compareTo(), equals(), hashCode(), and toString(). Create objects with valueOf() or autoboxing; the new Integer(n) constructor is deprecated since Java 9 and marked for removal since Java 16.
Syntax
Java Syntax
Use a primitive int for everyday arithmetic, and the static methods to convert or inspect numbers:
Java
int n = 42; // primitive, 32 bits
Integer boxed = n; // autoboxing: same as Integer.valueOf(n)
int back = boxed; // unboxing: same as boxed.intValue()
int parsed = Integer.parseInt("123"); // 123, from text
Integer obj = Integer.valueOf("123"); // an Integer object
String bits = Integer.toBinaryString(n); // "101010"
int larger = Integer.max(3, 9); // 9
// Integer x = new Integer(42); // deprecated since Java 9
Reference
int vs Integer
They hold the same kind of number, but behave differently:
Question
int
Integer
What is it?
A primitive 32-bit signed number
An object that wraps one int
Can it be null?
No; the default is 0
Yes, and unboxing null throws NullPointerException
Autoboxing converts in both directions for you: assigning an int to an Integer calls Integer.valueOf(), and using an Integer where an int is needed calls intValue(). Prefer plain int in loops and calculations, and use Integer only where an object is required, such as in a collection or when a value may be missing.
Concept
The Integer Cache and the == Trap
Integer.valueOf() and autoboxing reuse a pool of pre-built objects for every value from -128 to 127. Inside that range two boxed numbers with the same value are the same object; outside it, each boxing usually creates a new object:
Code
Result
Why
Integer a = 127, b = 127; a == b
true
Both refer to the cached object for 127
Integer c = 128, d = 128; c == d
false (default settings)
Two separate objects; == compares references
c.equals(d)
true
equals() compares the numbers inside
c.intValue() == d.intValue()
true
Both sides are primitive int values
The safe rule: compare Integer objects with equals() or Integer.compare(), never with ==. A bug that only appears for values above 127 is a classic sign of this trap.
Reference
Range, Overflow, and Unsigned Views
An int is a 32-bit signed number, so it covers -2,147,483,648 to 2,147,483,647. Arithmetic that passes either end silently wraps around instead of throwing an exception. The same 32 bits can also be read as an unsigned number (0 to 4,294,967,295) with the unsigned helpers:
Example 1 — Parse Text into Numbers and Format Them Back
Convert strings to numbers with parseInt() and valueOf(), read other bases, print a number in binary, hex, and octal, and handle bad input.
Java
public class IntegerHubParse {
public static void main(String[] args) {
int year = Integer.parseInt("2026");
Integer count = Integer.valueOf("42");
System.out.println("next year: " + (year + 1));
System.out.println("count + 8: " + (count + 8));
System.out.println("ff in base 16: " + Integer.parseInt("ff", 16));
System.out.println("255 in binary: " + Integer.toBinaryString(255));
System.out.println("255 in hex: " + Integer.toHexString(255));
System.out.println("255 in octal: " + Integer.toString(255, 8));
try {
Integer.parseInt("12a");
} catch (NumberFormatException e) {
System.out.println("bad input: " + e.getMessage());
}
}
}
Output
next year: 2027
count + 8: 50
ff in base 16: 255
255 in binary: 11111111
255 in hex: ff
255 in octal: 377
bad input: For input string: "12a"
How It Works
Text to number.parseInt("2026") returns a primitive int, while valueOf("42") returns an Integer object; Java unboxes it automatically in count + 8.
Other bases. The second argument of parseInt is the radix, so "ff" in base 16 is 255. Going the other way, toBinaryString, toHexString, and toString(n, radix) print the same number in base 2, 16, and 8.
Bad input."12a" is not a whole number, so parseInt throws NumberFormatException with the offending text in its message.
Example 2 — Compare Integer Objects Safely
See the cache in action, then use the correct ways to compare numbers: equals(), compare(), max(), and signum().
Java
public class IntegerHubCompare {
public static void main(String[] args) {
Integer a = 127, b = 127;
Integer c = 128, d = 128;
System.out.println("127 == 127: " + (a == b));
System.out.println("128 == 128: " + (c == d));
System.out.println("128 equals 128: " + c.equals(d));
System.out.println("compareTo: " + c.compareTo(d));
System.out.println("compare(5, 9): " + Integer.compare(5, 9));
System.out.println("max(3, 7): " + Integer.max(3, 7));
System.out.println("signum(-25): " + Integer.signum(-25));
}
}
Cached versus new objects. Autoboxing 127 returns the shared cached Integer, so a == b is true. Autoboxing 128 builds two separate objects, so c == d is false with default JVM settings.
Value comparison.c.equals(d) looks inside the objects and finds equal numbers, and compareTo returns 0 for equal values.
Static helpers.Integer.compare(5, 9) returns a negative number because 5 is smaller, max(3, 7) picks 7, and signum(-25) reports the sign as -1.
Example 3 — Watch an int Overflow and Inspect Its Bits
Add 1 to Integer.MAX_VALUE, widen to long to avoid the wrap, and use the bit methods to count and locate bits.
Java
public class IntegerHubOverflowBits {
public static void main(String[] args) {
int max = Integer.MAX_VALUE;
System.out.println("MAX_VALUE: " + max);
System.out.println("MAX_VALUE + 1: " + (max + 1));
System.out.println("as long: " + ((long) max + 1));
System.out.println("-1 as unsigned: " + Integer.toUnsignedString(-1));
System.out.println("-1 in hex: " + Integer.toHexString(-1));
System.out.println("42 in binary: " + Integer.toBinaryString(42));
System.out.println("bitCount(255): " + Integer.bitCount(255));
System.out.println("highestOneBit(100): " + Integer.highestOneBit(100));
System.out.println("trailing zeros of 40: " + Integer.numberOfTrailingZeros(40));
}
}
Output
MAX_VALUE: 2147483647
MAX_VALUE + 1: -2147483648
as long: 2147483648
-1 as unsigned: 4294967295
-1 in hex: ffffffff
42 in binary: 101010
bitCount(255): 8
highestOneBit(100): 64
trailing zeros of 40: 3
How It Works
Wrap-around.Integer.MAX_VALUE + 1 overflows the 32 bits and lands on MIN_VALUE. Casting one operand to long first makes the addition happen in 64 bits, so the real result 2147483648 survives.
Same bits, different reading. -1 is thirty-two 1 bits. Read as signed it is -1; read as unsigned it is 4294967295, and in hex it is ffffffff.
Bit methods. 255 has eight 1 bits, the highest set bit of 100 (binary 1100100) is worth 64, and 40 (binary 101000) ends in three zeros.
java.lang.Integer is the wrapper class for the primitive type int. An Integer object holds one int value, and the class also provides static methods to parse, format, compare, and manipulate the bits of int values, such as parseInt, toBinaryString, compare, and bitCount.
int is a primitive 32-bit signed number; it cannot be null and has no methods. Integer is an object that wraps an int; it can be null and can be stored in collections such as List<Integer>. Java converts between them automatically (autoboxing and unboxing), but unboxing a null Integer throws NullPointerException.
== compares object references, not values. Java reuses cached Integer objects for values from -128 to 127 when autoboxing, so two boxed 127s are the same object, but two boxed 128s are usually different objects. Always compare Integer objects with equals() or unbox them to int first.
Both convert a String to a number and throw NumberFormatException for bad input. parseInt returns a primitive int; valueOf returns an Integer object (using the cache for small values). Use parseInt when you need a plain number and valueOf when you need an object.
It silently overflows and wraps around: Integer.MAX_VALUE + 1 becomes Integer.MIN_VALUE (-2147483648). Java does not throw an exception for ordinary arithmetic. Use long for larger values, or Math.addExact and the other exact methods when you want an exception on overflow.