Open In App

Difference between an Integer and int in Java with Examples

Last Updated : 29 Jul, 2024
Comments
Improve
Suggest changes
18 Likes
Like
Report

In Java, int is a primitive data type while Integer is a Wrapper class.

  • int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it.
  • Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipulating an int data.
  • Integer is a class and thus it can call various in-built methods defined in the class. Variables of type Integer store references to Integer objects, just as with any other reference (object) type.

Examples:

// Valid
int n = 20;
//valid
Integer n = 45;

// Valid
Integer.parseInt("10");
// Not Valid
int.parseInt("10");

Important Points of Difference: 

1. Casting to String Variable

We can't assign a String value (containing an integer only) directly or even by casting to an int variable. However, we can assign a String to an object of Integer type using the Integer(String) constructor. We can even use parseInt(String) to convert a String literal to an int value. 


Output
133.1

2. Direct Conversion of value to other base

We can directly convert an integer value stored in Integer class to Binary, Octal or Hexadecimal format using toBinaryString(), toOctalString() or toHexString() respectively. This is not possible in a variable of int type. 


Output
1111011
173
7b

3. Performing operations on data

Integer class also allows us to reverse our number or rotate it left or right using reverse(), rotateLeft() and rotateRight() respectively. We need to define our own logic to perform these operations on an int variable as its not an inbuilt class. 


Output
Left Rotate : 48
Right rotate : 3
Reverse : 805306368

4. Flexibility

Integer wrapper class provides us more flexibility to the existing int datatype. We are able to perform many operations on an int value besides the predefined operators. Integer class is used where we need to treat an int variable like an object. Since Wrapper classes inherit Object class, they can be used in collections with Object reference or generics. Thus we are adding the property of nullability to the existing int data type. Since Java 5, we have the concept of auto-boxing wherein a primitive data type is converted into a wrapper class and vice versa automatically. Hence, we can perform any arithmetic or logical operation between any primitive data type and any Wrapper class. 

Note: The new Integer(String) constructor is deprecated in Java 9 and removed in Java 16. However, it is still supported in earlier versions (though it is not recommended to use it due to its deprecation)

  • Java 9: The new Integer(String) constructor is deprecated.
  • Java 16: The new Integer(String) constructor is removed.

Running this Code

  • Java 8: The code will compile and run, but using new Integer(String) will generate a warning.
  • Java 9 to Java 15: The code will compile and run, but using new Integer(String) will generate a deprecation warning.
  • Java 16 and Later: Using new Integer(String) will cause a compilation error. You must use Integer.valueOf(String).

It's best to avoid using deprecated constructors and follow the current best practices to ensure compatibility with future Java versions.

In Java 16 and later versions, using new Integer("12") will result in a compilation error. The recommended approach is to use Integer.valueOf("12")


Output
Sum of 2 Integer objects: 25
Sum of an Integer object and int value: 14
Sum of an Integer object and double value: 15.1
Sum of an Integer object and Double object: 24.1

Besides Integer, we have more wrapper classes in Java corresponding to the data types. These are given as follows :

Equivalent Wrapper Classes of Primitive Types in Java

Primitive Data TypeWrapper Class
booleanBoolean
charCharacter
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble


Next Article

Similar Reads