0
What is the difference between int and Integer? In Java
Does it have something to do with objects?
5 Answers
+ 3
In Java, int is a primitive data type while Integer is a Wrapper class.
1.Int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it.
2.Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipulating an int data.
3.Integer is a class and thus it can call various in-built methods defined in the class. Variables of type Integer store references to Integer objects, just as with any other reference (object) type.
+ 1
Integer a = new Integer("456"); Â Â Â
// Casting not possible   Â
// int a = (int)"456"; Â Â
Â
// Casting not possible   Â
// int c="456"; Â
 Â
// Casting possible using methods   Â
// from Integer Wrapper class   Â
int b = Integer.parseInt("456");
this line of codes should make it clear.
+ 1
There are few more things to consider.
int - 4 bytes
Integer - 16 bytes
int - defaults to 0
Integer defaults to null - which is another problem, because you have to do null check's.
You can compare int's simply like int1 == int2
But for comparing Integers you have to use .equals methods.
0
Thanks for all the responses!