The answer is 5 but my incorrect reasoning led me to say 6... It looks like Integer y=x; creates a 2nd object, whereas I interpreted it as assigning y to the same object that x points to.
Your reasoning for this line is actually correct. When you assign a reference to another reference, it doesn't create a new object.
It's the next line that is the issue. Integer objects are immutable, so when you do y++ on an Integer object, the value must be unboxed, incremented, and reboxed. It is this boxing that changes the reference to another Integer object.
Henry [ December 09, 2008: Message edited by: Henry Wong ]