You are absolutely correct, with one caveat: the answer to the first part depends on the value of Integer i
.
- In the first scenario, one
Integer
is created in the constructor, and the other one is created when boxing theint
coming fromgetI()
- In the second scenario, there needs to be no boxing, so there's only one
Integer
object.
Note: if the value of the Integer i
is small (more precisely, between -128 and 127, inclusive), autoboxing will produce the same Integer
through interning.