What is the difference between int and Integer in Java ? I know that "Integer" is a class and "int" is a datatype but they both work the same. Can they be used interchangeably ?
Asked
Active
Viewed 140 times
1 Answers
1
An int
is a 32 bit integer that is represented internally as a 4 byte primitive. An Integer
is a Java object that represents an int
value. Integer
instances are needed when working with the Collections API, as the collections only work with objects and not primitives. Integer
also contains a bunch of useful methods that make working with them somewhat easier. The downside of using Integer
is that it uses roughly 4 times the memory of the primitive version.

Jonathan Vusich
- 143
- 2
- 11