int vs Integer comparison Java -
this question has answer here:
- why java not see integers equal? 6 answers
class datatype1 { public static void main(string args[]) { int i1 = 1; integer i2 = 1; integer i3 = new integer(1); system.out.println("i1 == i2"+(i1==i2)); system.out.println("i1 == i3"+(i1==i3)); system.out.println("i2 == i3"+(i2==i3)); } }
output
i1 == i2true i1 == i3true i2 == i3false
can explain why false when comparing i2 , i3 ?
i1 == i2
results in un-boxing , regular int comparison done. (see first point in jls 5.6.2)
i2 == i3
results in reference comparsion. remember, i2
, i3
2 different objects. (see jls 15.21.3)
Comments
Post a Comment