Does working with primitives in Java create new Objects?

AFAIK the following code...

void foo () {

Integer i1 = new Integer(2);
Integer i2 = new Integer(2);
Integer i3 = new Integer(2);

i3 = i1 + i2;
}

... will actually create a new Integer Object when executing the + operator and assign its address to i3.

Does the same hold for primitive types also? I.e.:

void foo () {

int i1 = 2;
int i2 = 2;
int i3 = 2;

i3 = i1 + i2;
}

... Or will i3 in this case keep it's address in memory and get's the result of i1 + i2 copied to that address?

Thank you in advance.

Jon Skeet
people
quotationmark

Your terminology is a bit confused.... the value of i3 isn't an address (or reference) at all, it's just the integer value. But no, the example you've given won't create any Integer objects.

people

See more on this question at Stackoverflow