What is the difference between int and Integer in Java?
int is primitive data type and Integer is wrapper class.
int is a wrapper type and Integer is a primitive type.
both are reference types.
int is a primitive type that comes as part of java platform.
Integer is a reference type. This is a wrapper class build around primitive "int" to make it act like an object.
To differentiate both, we have to understand what is primitive and what is reference type differences.
primitives will be the basic data types that comes along with the java platform.
primitive values will be stored in stack generally.
Though reference types are not part of basic framework, still java provides Integer as a wrapper class around primitive int data type to make it act like an object while passing to some methods.
reference types will be generally stored in heap.
Integer class has some important utility functions like parseInt() to convert a string to integer.
When to use int?:
Use int in all normal cases when ever you need an integer value to be stored. Below is the way to declare using int.
int i = 10;
When to use Integer?
If a function takes only Object type values, and if you want to pass an integer value, then assign that value to Integer variable and pass it. Below is the way to declare using Integer.
Integer i = 10;
Back To Top