posted 17 years ago
well in case1 Integer i1=10 creates an Integer instance and places it in the pool the statement Integer i2=10 does not create an seperate object rather the reference i2 now points to the Integer object i1 which is placed on the pool ,now matter how many times we execute the statement like Integer <reference name> = 10 the result of their execution will be that each reference will point to the instance placed on the pool, so when we use the == operator in case 1 it will return true as i1 and i2 hold references of the same instance (which is placed on the pool)
But when we use the keyword "new" seperate instances are created with each instance placed on the heap rather than on the pool as a result i1 and i2 now hold reference of two different Integer instances which have the same value 10. so when we use the == operator it will return false as i1 and i2 are two entirely different objects
SCJP1.4 , gearing up for SCWCD 5