How to convert int to Integer

Learn how to convert int to integer with practical examples, diagrams, and best practices. Covers android development techniques with visual explanations.

Mastering Integer Conversion: int to Integer in Java

Hero image for How to convert int to Integer

Learn the essential techniques for converting primitive int types to their wrapper class Integer counterparts in Java, covering autoboxing, constructor, and valueOf() methods.

In Java, int is a primitive data type, while Integer is a wrapper class that provides an object representation of an int. Understanding how to convert between these two is fundamental for various programming tasks, especially when working with collections, generics, or APIs that require objects. This article will guide you through the most common and efficient methods for converting an int to an Integer.

Autoboxing: The Simplest Approach

Java's autoboxing feature, introduced in Java 5, automatically converts a primitive int to an Integer object when needed. This simplifies code and reduces verbosity, allowing you to treat int values as Integer objects without explicit casting or method calls. The compiler handles the conversion behind the scenes.

int primitiveInt = 100;
Integer wrappedInteger = primitiveInt; // Autoboxing

System.out.println("Primitive int: " + primitiveInt); // Output: Primitive int: 100
System.out.println("Wrapped Integer: " + wrappedInteger); // Output: Wrapped Integer: 100

Example of autoboxing an int to an Integer.

Using the Integer.valueOf() Method

The Integer.valueOf(int i) method is the recommended explicit way to convert an int to an Integer. This method is more efficient than using the Integer constructor because it leverages a cache for Integer values between -128 and 127 (inclusive). If the int value falls within this range, valueOf() returns a cached Integer instance, avoiding the creation of a new object. For values outside this range, a new Integer object is created.

int primitiveInt = 250;
Integer wrappedInteger = Integer.valueOf(primitiveInt);

System.out.println("Primitive int: " + primitiveInt);
System.out.println("Wrapped Integer (valueOf): " + wrappedInteger);

// Demonstrating cache behavior
Integer cachedInt1 = Integer.valueOf(50);
Integer cachedInt2 = Integer.valueOf(50);
System.out.println("Cached Integers are same object: " + (cachedInt1 == cachedInt2)); // Output: true

Integer newInt1 = Integer.valueOf(200);
Integer newInt2 = Integer.valueOf(200);
System.out.println("New Integers are same object: " + (newInt1 == newInt2)); // Output: false

Converting int to Integer using Integer.valueOf() and demonstrating cache behavior.

Using the Integer Constructor (Deprecated)

Historically, you could create an Integer object from an int using its constructor: new Integer(int i). However, this constructor has been deprecated since Java 9. It is generally discouraged because it always creates a new Integer object, even for values that could be cached by Integer.valueOf(), leading to unnecessary object creation and potential performance issues. While it still works in older Java versions, it's best to avoid it in new code.

int primitiveInt = 75;
// Integer wrappedInteger = new Integer(primitiveInt); // Deprecated since Java 9

// If you must use it (e.g., legacy code), it would look like this:
// Integer wrappedInteger = new Integer(primitiveInt);
// System.out.println("Wrapped Integer (Constructor): " + wrappedInteger);

Example of the deprecated Integer constructor.

flowchart TD
    A[Start with int primitive] --> B{Need Integer object?}
    B -->|Yes| C{Is Autoboxing sufficient?}
    C -->|Yes| D[Assign int to Integer variable]
    D --> E[Autoboxing handles conversion]
    C -->|No| F{Explicit conversion needed?}
    F -->|Yes| G["Use Integer.valueOf(int)"]
    G --> H[Efficient, uses cache for -128 to 127]
    F -->|No| I[Continue with int primitive]
    E --> J[End]
    H --> J

Decision flow for converting int to Integer.