Difference between an Integer and int in Java with Examples
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;
// Not 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 program to illustrate// difference between// int and IntegerpublicclassMain {publicstaticvoidmain(String args[]){Integer a =newInteger("123");// Casting not possible// int a = (int)"123";// Casting not possible// int c="123";// Casting possible using methods// from Integer Wrapper classintb = Integer.parseInt("123");System.out.print(a +newFloat("10.1"));}}chevron_rightfilter_noneOutput:133.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 program to illustrate// difference between// int and IntegerpublicclassMain {publicstaticvoidmain(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);}}chevron_rightfilter_noneOutput:1111011 173 7b
- 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 program to illustrate// difference between// int and IntegerpublicclassMain {publicstaticvoidmain(String args[]){// mainethods convert integer to its binary form,// apply the desired operation// and then returns the decimal form// of the newly formed binary number// (12)10->(1100)2 ->// rotate left by 2 units -> (110000)2->(48)10intrL = Integer.rotateLeft(12,2);// (12)10->(1100)2 ->// rotate right by 2 units -> (0011)2->(3)10intrR = Integer.rotateRight(12,2);//(12)10 -> (00000000000000000000000000001100)2// -> reverse ->(00110000000000000000000000000000)2// -> (805306368)10// int is of 32 bitsintrev = Integer.reverse(12);System.out.print("Left Rotate : "+ rL+"\nRight rotate : "+ rR +"\nReverse : "+ rev);}}chevron_rightfilter_noneOutput: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 program to illustrate// auto-boxingimportjava.util.function.Function;importjava.util.function.Function;publicclassMain {publicstaticvoidmain(String args[]){Integer a =newInteger("12");Integer d =newInteger("13");intb =2;doublec =3.1;Double f =newDouble("12.1");intd2 = a + d;System.out.println("Sum of 2 Integer objects :"+ (a + d));System.out.println("Sum of an Integer objectandintvalue :" + (a + b));System.out.println("Sum of an Integer objectanddoublevalue :" + (a + c));System.out.println("Sum of an Integer objectand Double object :" + (a + f));}}chevron_rightfilter_noneOutput: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

Recommended Posts:
- Java lang Integer.toHexString() Method with Examples
- Integer.valueOf() vs Integer.parseInt() with Examples
- Program to Convert Set of Integer to Array of Integer in Java
- Java.lang.Integer class in Java
- Integer sum() Method in Java
- Integer toString() in Java
- Integer doubleValue() Method in Java
- Convert Double to Integer in Java
- Java Integer compareTo() method
- Integer.numberOfLeadingZeros() Method in Java With Example
- Integer highestOneBit() Method in Java
- Integer lowestOneBit() Method in Java
- Integer.numberOfTrailingZeros() Method in Java with Example
- Integer valueOf() Method in Java
- Integer hashCode() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



