The Integer class is the wrapper class for Java's primitive int type. It allows an integer value to be represented as an object and provides a rich collection of methods for conversion, comparison, parsing, and numeric operations.
You will encounter Integer frequently when working with Collections, Generics, APIs, configuration values, user input, and data received from external systems.
Why Do We Need Integer?
The primitive int is usually the right choice when you simply need to perform arithmetic. However, Java's object-oriented APIs sometimes require an object instead of a primitive.
List<Integer> numbers = new ArrayList<>(); numbers.add(10); numbers.add(20); numbers.add(30);
Here, the collection stores Integer objects. Java automatically converts the supplied int values into Integer objects through autoboxing.
Creating an Integer
Modern Java code should generally use autoboxing or Integer.valueOf() rather than the old Integer constructor.
Integer first = 100; Integer second = Integer.valueOf(200); System.out.println(first); System.out.println(second);
The first assignment uses autoboxing. The second explicitly requests an Integer object using valueOf().
Integer Constants
The Integer class provides useful constants that describe the limits of the int data type.
System.out.println(Integer.MIN_VALUE); System.out.println(Integer.MAX_VALUE); System.out.println(Integer.SIZE); System.out.println(Integer.BYTES);
| Constant | Meaning | Typical Value |
|---|---|---|
| MIN_VALUE | Smallest possible int value | -2147483648 |
| MAX_VALUE | Largest possible int value | 2147483647 |
| SIZE | Number of bits used by int | 32 |
| BYTES | Number of bytes used by int | 4 |
Converting Integer to int
The intValue() method returns the primitive int represented by an Integer object.
Integer number = 500; int value = number.intValue(); System.out.println(value);
In everyday Java code, you usually do not need to call intValue() manually because Java can perform unboxing automatically.
Integer number = 500; int value = number;
Parsing a String into an Integer
One of the most useful features of Integer is converting textual numeric data into an int.
String text = "250"; int number = Integer.parseInt(text); System.out.println(number + 50);
The string "250" becomes the numeric value 250. The result of parseInt() is a primitive int.
valueOf() Versus parseInt()
These two methods are often confused because both can convert a numeric string. The important difference is the type of result they produce.
| Method | Input | Returns |
|---|---|---|
| parseInt() | String | int |
| valueOf() | String | Integer |
int primitiveValue = Integer.parseInt("100");
Integer wrapperValue = Integer.valueOf("100");Choose parseInt() when you need a primitive integer. Choose valueOf() when an Integer object is useful or required.
Comparing Integer Values
The Integer class provides several ways to compare integer values. For wrapper objects, be especially careful with ==.
Integer first = 500; Integer second = 500; System.out.println(first.equals(second)); System.out.println(Integer.compare(first, second));
equals() checks whether the wrapped values are equal. The static compare() method returns a negative number, zero, or a positive number depending on whether the first value is smaller than, equal to, or greater than the second.
Useful Integer Methods
| Method | Purpose |
|---|---|
| parseInt() | Converts a String into a primitive int. |
| valueOf() | Creates or obtains an Integer representation of a value. |
| intValue() | Returns the wrapped value as an int. |
| compare() | Compares two int values. |
| compareTo() | Compares one Integer object with another. |
| toString() | Converts an integer value into its String representation. |
| max() | Returns the greater of two int values. |
| min() | Returns the smaller of two int values. |
| sum() | Returns the sum of two int values. |
Example: Useful Integer Operations
int a = 40; int b = 70; System.out.println(Integer.max(a, b)); System.out.println(Integer.min(a, b)); System.out.println(Integer.sum(a, b)); System.out.println(Integer.compare(a, b));
This example demonstrates how the Integer class can perform common operations without requiring you to write separate comparison logic.
Converting an Integer to a String
The Integer.toString() method converts an integer into textual form. This is useful when constructing messages, storing values as text, or preparing data for external systems.
int score = 95;
String text = Integer.toString(score);
System.out.println("Score: " + text);Working with Different Number Bases
The Integer class also provides methods for converting integers between decimal and other number systems such as binary, hexadecimal, and octal.
int number = 42; System.out.println(Integer.toBinaryString(number)); System.out.println(Integer.toOctalString(number)); System.out.println(Integer.toHexString(number));
For the decimal value 42, these methods produce its binary, octal, and hexadecimal representations. This is particularly useful in low-level programming, debugging, networking, and bit manipulation.
Integer and Null Values
Because Integer is a reference type, an Integer variable can contain null.
Integer count = null; System.out.println(count);
However, unboxing a null Integer is dangerous.
Integer count = null; int value = count;
The assignment attempts to convert the null reference into a primitive int, resulting in a NullPointerException.
Best Practices
- Prefer int when you need ordinary numeric computation and do not need an object.
- Use Integer when working with Collections, Generics, or APIs that require objects.
- Use Integer.parseInt() when converting text into a primitive integer.
- Use Integer.valueOf() when an Integer object is required.
- Use equals() when comparing wrapper values.
- Always consider the possibility of null before unboxing an Integer.
Interview Insight
A frequently asked interview question is: “What is the difference between Integer.parseInt() and Integer.valueOf()?” The key distinction is that parseInt() returns a primitive int, whereas valueOf() returns an Integer object.
Integer Class at a Glance
| Feature | Key Point |
|---|---|
| Wrapper type | Integer wraps the primitive int. |
| Range | From -2,147,483,648 to 2,147,483,647. |
| Parsing | parseInt() converts text to primitive int. |
| Object conversion | valueOf() provides an Integer object. |
| Comparison | equals(), compare(), and compareTo() support comparisons. |
| Number bases | Supports binary, octal, and hexadecimal conversion. |
| Null | An Integer reference can be null. |
The Integer class is much more than a simple wrapper around int. It connects primitive integer values with Java's object-oriented APIs and provides practical tools for parsing, comparison, conversion, and number-system handling. Once these methods become familiar, processing integer data from real-world applications becomes considerably easier.
