What's the difference between int and Integer? Plz explain... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

What's the difference between int and Integer? Plz explain...

Integer a=10; Integer b=10; if (a==b) ; // this results false ! // if a and b are declared as int if stmt is true!

14th Apr 2017, 12:39 PM
Çhandra_Şhekar
Çhandra_Şhekar - avatar
9 Answers
+ 13
In this case, you are not comparing integers. You are comparing objects. Two object won't be equal even if they have same data.
14th Apr 2017, 1:33 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 7
a==b doesn't check integer values and doesn't check object equality, it checks object references. The two Integer objects are different objects so theirs references are different even if they are equals (equals method would return true)
14th Apr 2017, 3:39 PM
Tamás Barta
Tamás Barta - avatar
+ 5
for each primitive type there exists a wrapper type (Character, Integer, Double). These are objects as String or any other non-primitive type. with == you compare the reference of objects. the same reason why string comparison only works with equals instead of ==.
14th Apr 2017, 1:00 PM
Petja Boigk
Petja Boigk - avatar
+ 4
Then I'll nod If I'm rock 😗
14th Apr 2017, 5:12 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
14th Apr 2017, 1:27 PM
देवेंद्र महाजन (Devender)
देवेंद्र महाजन (Devender) - avatar
+ 2
int is just a keyword(or data type) but Integer is a class Integer a = 10; Integer b = 10; if(a==b) System.out.print("true"); And that "if statement" always be true,bro
14th Apr 2017, 1:22 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 2
In your case "a" and "b" are the two objects of class "Integer". Two objects can't be same as their reference is different. But "int" is data type not a class
14th Apr 2017, 5:30 PM
Rohan Rajak
0
thx for all of ur answers. .. u all rock ! I got the answer
14th Apr 2017, 5:11 PM
Çhandra_Şhekar
Çhandra_Şhekar - avatar
0
😂
14th Apr 2017, 5:17 PM
Çhandra_Şhekar
Çhandra_Şhekar - avatar