This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
Hi Thanks for the reply actually i am not getting that when we say Integer i1 = 10; At this time what happen is first it makes an object of Integer class and assign value 10 to it ok??correct me if i am wrong.
case1 Integer i1 = 10; Integer i2 = 10; i1==i2 // it gives true at this time it check for the value why?though i1 and i2 are object and when i say case 2 Integer i1 = new Integer(10); Integer i2 = new Integer(10); i1==i2 //it gives false
i guess internally in case 1 the objcet gets created please help me for this topic
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