Explore Courses Blog Tutorials Interview Questions
0 votes
in Java by (7k points)

What is the difference between Integer and int in java?

1 Answer

0 votes
by (13.1k points)

int is a primitive data type. Variables pf int type store actual byte value for the integer you want to represent.

An integer is a class. Variables of type Integer store references to Integer objects, just as with any other reference type.

To be more specific, Integer is a class with a single field of type int. This class is used where you need an int to be treated like any other object, such as in generic types or situations where you need nullability.

Every primitive type in Java has an equivalent wrapper class:

  • byte has Byte

  • short has Short

  • int has Integer

  • long has Long

  • boolean has Boolean

  • char has Character

  • float has Float

  • double has Double

Wrapper classes inherit from Object class, and primitive don't. So it can be used in collections with Object reference or with Generics.

Want to learn Java? Check out the Java certification from Intellipaat.

Since java 5 we have autoboxing, and the conversion between primitive and wrapper class is done automatically.

Related questions

0 votes
1 answer
0 votes
2 answers
0 votes
1 answer
0 votes
1 answer

Browse Categories