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
hasByte
char
hasCharacter
float
hasFloat
boolean
hasBoolean
short
hasShort
double
hasDouble
long
hasLong
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.
LinkedInRelated Article - Java Int
- How to Convert Int to Char in Java
- How to Convert Int to Double in Java
- List of Ints in Java
- How to Convert Integer to Int in Java
- How to Check if Int Is Null in Java
- How to Convert Int to Byte in Java