Difference between an Integer and int in Java

Java 8Object Oriented ProgrammingProgramming

A Java both int and Integer are used to store integer type data the major difference between both is type of int is primitive while Integer is of class type.This difference become significant when concept of OOPs comes in picture during development as int follows the principle of primitive data type while Integer behave as a wrapper class.

Following are the important differences between int and Integer.

Sr. No.KeyintInteger
1TypeA int is a data type that stores 32 bit signed two's compliment integer.On other hand Integer is a wrapper class which wraps a primitive type int into an object.
2Purposeint helps in storing integer value into memory.Integer helps in converting int into object and to convert an object into int as per requirement.
3Flexibilityint provides less flexibility as compare to Integer as it only allows binary value of an integer in it.Integer on other hand is more flexible in storing and manipulating an int data.Since Wrapper classes inherit Object class, they can be used in collections with Object reference or generics.
4Memory allocationAs already mentioned int is a primitive data type and takes 32 bits(4 bytes) to store.On other hand Integer is an object which takes 128 bits (16 bytes) to store its int value.
5CastingIn java one canâTMt assign a string value (containing an integer only) to an int variable directly or even by casting.In case of Integer we can assign string to an object of Integer type using the Integer(String) constructor or by even use parseInt(String) to convert a String literal to an int value.
6Direct Conversion to Other base.In case of int we can't convert its integer value to other base.However in Integer we can directly convert its integer value to other bases such as Binary, Octal or Hexadecimal format using toBinaryString(), toOctalString() or toHexString() respectively.
7Allowed operationsint do not allowed any of inbuilt functions to change its value or syntax.However in Integer we can reverse number or rotate it left or right using reverse(), rotateLeft() and rotateRight() respectively.

Example of int vs Integer



 Live Demo

public class JavaTester {
   public static void main(String args[]){
      Integer a = new Integer("456");
      // Casting not possible
      // int a = (int)"456";
      // Casting not possible
      // int c="456";
      // Casting possible using methods
      // from Integer Wrapper class
      int b = Integer.parseInt("456");


Published on 16-Sep-2019 14:56:55