0

What is the difference between int and Integer? In Java

Does it have something to do with objects?

14th Apr 2020, 7:17 PM
Zoe
Zoe - avatar
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.
14th Apr 2020, 7:26 PM
Akshay Jain
Akshay Jain - avatar
+ 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.
14th Apr 2020, 7:24 PM
minirkk
minirkk - avatar
+ 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.
14th Apr 2020, 10:06 PM
Michal
Michal - avatar
0
Thanks for all the responses!
14th Apr 2020, 8:15 PM
Zoe
Zoe - avatar