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 Differences :
- Casting to String Variable : We can’t assign a String value (containing an integer only) to an int variable directly or even by casting. 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.
Java
public class Main {
public static void main(String args[])
{
Integer a = new Integer( "123" );
int b = Integer.parseInt( "123" );
System.out.print(a + new Float( "10.1" ));
}
}
|
- 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.
Java
public class Main {
public static void main(String args[])
{
String bin = Integer.toBinaryString( 123 );
String oct = Integer.toOctalString( 123 );
String hex = Integer.toHexString( 123 );
System.out.print(bin + "\n" + oct + "\n" + hex);
}
}
|
- 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.
Java
public class Main {
public static void main(String args[])
{
int rL = Integer.rotateLeft( 12 , 2 );
int rR = Integer.rotateRight( 12 , 2 );
int rev = Integer.reverse( 12 );
System.out.print( "Left Rotate : " + rL
+ "\nRight rotate : " + rR + "\nReverse : " + rev);
}
}
|
Output:
Left Rotate : 48
Right rotate : 3
Reverse : 805306368
- 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.
Java
import java.util.function.Function;
import java.util.function.Function;
public class Main {
public static void main(String args[])
{
Integer a = new Integer( "12" );
Integer d = new Integer( "13" );
int b = 2 ;
double c = 3.1 ;
Double f = new Double( "12.1" );
int d2 = a + d;
System.out.println( "Sum of 2 Integer objects :"
+ (a + d));
System.out.println("Sum of an Integer object
and int value :" + (a + b));
System.out.println("Sum of an Integer object
and double value :" + (a + c));
System.out.println("Sum of an Integer object
and Double object :" + (a + f));
}
}
|
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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
21 Feb, 2023
Like Article
Save Article