int vs Integer comparison Java -


this question has answer here:

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

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -