Difference Between Integer and Int in Java

Haider Ali Oct 08, 2023
  1. Integer vs int in Java
  2. Wrapper Class
Difference Between Integer and Int in Java

In this article, we will learn about the difference between Integer and int in Java. Let’s get right into it.

Integer vs int in Java

In Java, there are some primitive types that store binary values. The actual binary value for the integer you want to represent is stored in an int. It’s not a class, and you can’t implement any methods using int.

On the other hand, Integer is just like any other class in Java. We store references to Integer objects through variables of the Integer type. There are multiple methods that you can use through Integer. For instance, we can use Integer.parseInt("1"). It’s a static method that will return an int.

The Integer is a java class with a single field type int. The idea here is that we can use this class whenever we need int to act and be treated as objects. In short, Integer is a wrapper class for int.

Wrapper Class

Unlike primitive types, the wrapper class can inherit from the Object class. So, developers can use this class with generics and object references in collections. Remember that every primitive type has its wrapper class, just like int has Integer.

  • byte has Byte
  • char has Character
  • float has Float
  • boolean has Boolean
  • short has Short
  • double has Double
  • long has Long
Author: Haider Ali
Haider Ali avatar Haider Ali avatar

Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.

LinkedIn

Related Article - Java Int

Related Article - Java Integer